references.module 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @file
  4. * Defines common base features for the various reference field types.
  5. */
  6. /**
  7. * Menu access callback for reference autocomplete paths.
  8. *
  9. * Check for both 'edit' and 'view' access in the unlikely event
  10. * a user has edit but not view access.
  11. */
  12. function reference_autocomplete_access($entity_type, $bundle, $field_name, $entity = NULL, $account = NULL) {
  13. return user_access('access content', $account)
  14. && field_info_instance($entity_type, $field_name, $bundle)
  15. && ($field = field_info_field($field_name))
  16. && field_access('view', $field, $entity_type, $entity, $account)
  17. && field_access('edit', $field, $entity_type, $entity, $account);
  18. }
  19. /**
  20. * Implements hook_init().
  21. */
  22. function references_init() {
  23. // Include feeds.module integration.
  24. if (module_exists('feeds')) {
  25. module_load_include('inc','references','references.feeds');
  26. }
  27. }
  28. /**
  29. * Implements hook_views_api().
  30. */
  31. function references_views_api() {
  32. return array(
  33. 'api' => 3,
  34. 'path' => drupal_get_path('module', 'references') . '/views',
  35. );
  36. }
  37. /**
  38. * Implements hook_views_plugins().
  39. *
  40. * Defines some plugins used by the Views modes for
  41. * user_reference.
  42. */
  43. function references_views_plugins() {
  44. $plugins = array(
  45. 'display' => array(
  46. 'references' => array(
  47. 'title' => t('References'),
  48. 'admin' => t('References'),
  49. 'help' => 'Selects referenceable entities for a reference field (node_reference, user_reference...)',
  50. 'handler' => 'references_plugin_display',
  51. 'uses hook menu' => FALSE,
  52. 'use ajax' => FALSE,
  53. 'use pager' => FALSE,
  54. 'accept attachments' => FALSE,
  55. // Custom property, used with views_get_applicable_views() to retrieve
  56. // all views with a 'References' display.
  57. 'references display' => TRUE,
  58. ),
  59. ),
  60. 'style' => array(
  61. 'references_style' => array(
  62. 'title' => t('References list'),
  63. 'help' => 'Returns results as a PHP array of names + rendered rows.',
  64. 'handler' => 'references_plugin_style',
  65. 'theme' => 'views_view_unformatted',
  66. 'uses row plugin' => TRUE,
  67. 'uses fields' => TRUE,
  68. 'uses options' => TRUE,
  69. 'uses grouping' => TRUE,
  70. 'type' => 'references',
  71. 'even empty' => TRUE,
  72. ),
  73. ),
  74. 'row' => array(
  75. 'references_fields' => array(
  76. 'title' => t('Inline fields'),
  77. 'help' => t('Displays the fields with an optional template.'),
  78. 'handler' => 'references_plugin_row_fields',
  79. 'theme' => 'views_view_fields',
  80. 'theme path' => drupal_get_path('module', 'views') . '/theme',
  81. 'theme file' => 'theme.inc',
  82. 'uses fields' => TRUE,
  83. 'uses options' => TRUE,
  84. 'type' => 'references',
  85. ),
  86. ),
  87. );
  88. return $plugins;
  89. }
  90. /**
  91. * Retrieves the list of views with a 'references' display, in a format suitable for a 'select' form element..
  92. *
  93. * @param $entity_type
  94. * The entity type.
  95. *
  96. * @return
  97. * An array of eligible views displays.
  98. */
  99. function references_get_views_options($entity_type) {
  100. // Filter views that contain a 'references' display. This actually returns a
  101. // list of displays (the same view appears several times).
  102. $displays = views_get_applicable_views('references display');
  103. // Filter views that list the entity type we want, and group the separate
  104. // displays by view.
  105. $entity_info = entity_get_info($entity_type);
  106. $options = array();
  107. foreach ($displays as $data) {
  108. list($view, $display_id) = $data;
  109. if ($view->base_table == $entity_info['base table']) {
  110. $options[$view->name . ':' . $display_id] = $view->name .' - ' . $view->display[$display_id]->display_title;
  111. }
  112. }
  113. return $options;
  114. }
  115. /**
  116. * Retrieves an array of candidate referenceable entities, defined by a view.
  117. *
  118. * @param $entity_type
  119. * The entity type.
  120. * @param $view_name
  121. * The name of the view.
  122. * @param $display_name
  123. * The name of the view's display. This has to be a 'References' display.
  124. * @param $args
  125. * The array of arguments ("contextual filters") for the view.
  126. * @param $options
  127. * Array of options to limit the scope of the returned list. This parameter
  128. * is similar to the $options parameter for
  129. * node_reference_potential_references(). An additional key is required:
  130. * - title_field: the name of the column holding entities 'titles' within the
  131. * entity base table.
  132. *
  133. * @return
  134. * An array of entities, in the format expected by
  135. * node_reference_potential_references().
  136. *
  137. * @see node_reference_potential_references()
  138. * @see _node_reference_potential_references_views()
  139. */
  140. function references_potential_references_view($entity_type, $view_name, $display_name, $args, $options) {
  141. $entity_info = entity_get_info($entity_type);
  142. // Check that the view is valid and the display still exists.
  143. $view = views_get_view($view_name);
  144. if (!$view || $view->base_table != $entity_info['base table'] || !isset($view->display[$display_name])) {
  145. return FALSE;
  146. }
  147. // If we have no access to the View an empty result should be returned to
  148. // avoid triggering the fallback results.
  149. if (!$view->access(array($display_name))) {
  150. return array();
  151. }
  152. // Temporary backwards compatibility for fields migrated from CCK D6: accept
  153. // 'default' display, but dynamically add a 'references' display out of it.
  154. if ($display_name == 'default') {
  155. $display_name = $view->add_display('references');
  156. }
  157. $view->set_display($display_name);
  158. // @todo From merlinofchaos on IRC : arguments using summary view can defeat
  159. // the style setting.
  160. // We might also need to check if there's an argument, and set its
  161. // style_plugin as well.
  162. // Set additional options to let references_plugin_display::query() narrow
  163. // the results.
  164. $references_options = array(
  165. 'ids' => $options['ids'],
  166. 'title_field' => $options['title_field'],
  167. 'string' => $options['string'],
  168. 'match' => $options['match'],
  169. );
  170. $view->display_handler->set_option('references_options', $references_options);
  171. // We need the title field for autocomplete widgets, so add it (hidden) if not
  172. // present.
  173. $fields = $view->get_items('field', $display_name);
  174. if (!isset($fields[$options['title_field']])) {
  175. $label_options = array(
  176. 'exclude' => 1,
  177. );
  178. $view->add_item($display_name, 'field', $entity_info['base table'], $options['title_field'], $label_options);
  179. }
  180. // Limit result set size.
  181. $limit = !empty($options['limit']) ? $options['limit'] : 0;
  182. $view->display_handler->set_option('pager', array('type' => 'some', 'options' => array('items_per_page' => $limit)));
  183. // Make sure the query is not cached
  184. $view->is_cacheable = FALSE;
  185. // Get the results.
  186. $results = $view->execute_display($display_name, $args);
  187. return $results;
  188. }