content.menu.inc

  1. cis7 sites/all/modules/ulmus/ctools/includes/content.menu.inc
  2. cle7 sites/all/modules/ulmus/ctools/includes/content.menu.inc
  3. ecd7 sites/all/modules/ulmus/ctools/includes/content.menu.inc
  4. elmsmedia7 sites/all/modules/ulmus/ctools/includes/content.menu.inc
  5. harmony7 sites/all/modules/ulmus/ctools/includes/content.menu.inc
  6. icor7 sites/all/modules/ulmus/ctools/includes/content.menu.inc
  7. meedjum_blog7 sites/all/modules/ulmus/ctools/includes/content.menu.inc
  8. mooc7 sites/all/modules/ulmus/ctools/includes/content.menu.inc

Contains menu item registration for the content tool.

The menu items registered are AJAX callbacks for the things like autocomplete and other tools needed by the content types.

Functions

Namesort descending Description
ctools_content_autocomplete_entity Helper function for autocompletion of entity titles.
ctools_content_menu
_ctools_buildQuery
_ctools_getReferencableEntities Private function to get referencable entities. Based on code from the Entity Reference module.

File

sites/all/modules/ulmus/ctools/includes/content.menu.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains menu item registration for the content tool.
  5. *
  6. * The menu items registered are AJAX callbacks for the things like
  7. * autocomplete and other tools needed by the content types.
  8. */
  9. function ctools_content_menu(&$items) {
  10. $base = array(
  11. 'access arguments' => array('access content'),
  12. 'type' => MENU_CALLBACK,
  13. 'file' => 'includes/content.menu.inc',
  14. );
  15. $items['ctools/autocomplete/%'] = array(
  16. 'page callback' => 'ctools_content_autocomplete_entity',
  17. 'page arguments' => array(2),
  18. ) + $base;
  19. }
  20. /**
  21. * Helper function for autocompletion of entity titles.
  22. */
  23. function ctools_content_autocomplete_entity($type, $string = '') {
  24. if ($string != '') {
  25. global $user;
  26. $entity_info = entity_get_info($type);
  27. // We must query all ids, because if every one of the 10 don't have access
  28. // the user may never be able to autocomplete a node title.
  29. $preg_matches = array();
  30. $matches = array();
  31. $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
  32. if (!$match) {
  33. $match = preg_match('/^id: (\d+)/', $string, $preg_matches);
  34. }
  35. // If an ID match was found, use that ID rather than the whole string.
  36. if ($match) {
  37. $entity_id = $preg_matches[1];
  38. $entity = entity_load($type, array($entity_id));
  39. // Format results in an array so later we could add attributes to the
  40. // autocomplete text that is returned.
  41. $results = array($entity_id => array(
  42. 'label' => $entity[$entity_id]->$entity_info['entity keys']['label'],
  43. ));
  44. }
  45. else {
  46. $results = _ctools_getReferencableEntities($type, $entity_info, $string, 'LIKE', 10);
  47. }
  48. foreach($results as $entity_id => $result) {
  49. if (!$entity_info['entity keys']['label']) {
  50. $matches["[id: $entity_id]"] = '<span class="autocomplete_title">' . $entity_id . '</span>';
  51. }
  52. else {
  53. $matches[$result['label'] . " [id: $entity_id]"] = '<span class="autocomplete_title">' . check_plain($result['label']) . '</span>';
  54. }
  55. }
  56. drupal_json_output($matches);
  57. }
  58. }
  59. /*
  60. * Use well known/tested entity reference code to build our search query
  61. * From EntityReference_SelectionHandler_Generic class
  62. */
  63. function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') {
  64. $base_table = $entity_info['base table'];
  65. $query = db_select($base_table)
  66. ->fields($base_table, array($entity_info['entity keys']['id']));
  67. if (isset($match)) {
  68. if (isset($entity_info['entity keys']['label'])) {
  69. $query->condition($base_table .'.'. $entity_info['entity keys']['label'], '%' . $match . '%' , $match_operator);
  70. }
  71. }
  72. // Add a label to the query, if the label exists
  73. if (isset($entity_info['entity keys']['label'])) {
  74. $query->fields($base_table, array($entity_info['entity keys']['label']));
  75. }
  76. // Add a generic entity access tag to the query.
  77. $query->addTag('ctools');
  78. if($entity_type == 'comment') {
  79. // Adding the 'comment_access' tag is sadly insufficient for comments: core
  80. // requires us to also know about the concept of 'published' and
  81. // 'unpublished'.
  82. if (!user_access('administer comments')) {
  83. $query->condition('comment.status', COMMENT_PUBLISHED);
  84. }
  85. // Join to a node if the user does not have node access bypass permissions
  86. // to obey node published permissions
  87. if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
  88. $node_alias = $query->innerJoin('node', 'n', '%alias.nid = comment.nid');
  89. $query->condition($node_alias . '.status', NODE_PUBLISHED);
  90. }
  91. $query->addTag('node_access');
  92. }
  93. else {
  94. $query->addTag($entity_type . '_access');
  95. }
  96. // Add the sort option.
  97. if(isset($entity_info['entity keys']['label'])) {
  98. $query->orderBy($base_table .'.'. $entity_info['entity keys']['label'], 'ASC');
  99. }
  100. return $query;
  101. }
  102. /**
  103. * Private function to get referencable entities. Based on code from the
  104. * Entity Reference module.
  105. */
  106. function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) {
  107. $options = array();
  108. $query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator);
  109. if ($limit > 0) {
  110. $query->range(0, $limit);
  111. }
  112. $results = $query->execute();
  113. if (!empty($results)) {
  114. foreach ($results as $record) {
  115. $options[$record->{$entity_info['entity keys']['id']}] = array(
  116. 'label' => isset($entity_info['entity keys']['label']) ? check_plain($record->{$entity_info['entity keys']['label']}) : $record->{$entity_info['entity keys']['id']},
  117. );
  118. }
  119. }
  120. return $options;
  121. }