references.module 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * Get Views Options.
  92. *
  93. * Retrieves the list of views with a 'references' display, in a format suitable
  94. * for a 'select' form element..
  95. *
  96. * @param string $entity_type
  97. * The entity type.
  98. *
  99. * @return array
  100. * An array of eligible views displays.
  101. */
  102. function references_get_views_options($entity_type) {
  103. // Filter views that contain a 'references' display. This actually returns a
  104. // list of displays (the same view appears several times).
  105. $displays = views_get_applicable_views('references display');
  106. // Filter views that list the entity type we want, and group the separate
  107. // displays by view.
  108. $entity_info = entity_get_info($entity_type);
  109. $options = array();
  110. foreach ($displays as $data) {
  111. list($view, $display_id) = $data;
  112. if ($view->base_table == $entity_info['base table']) {
  113. $options[$view->name . ':' . $display_id] = $view->name . ' - ' . $view->display[$display_id]->display_title;
  114. }
  115. }
  116. return $options;
  117. }
  118. /**
  119. * Retrieves an array of candidate referenceable entities, defined by a view.
  120. *
  121. * @param string $entity_type
  122. * The entity type.
  123. * @param string $view_name
  124. * The name of the view.
  125. * @param string $display_name
  126. * The name of the view's display. This has to be a 'References' display.
  127. * @param array $args
  128. * The array of arguments ("contextual filters") for the view.
  129. * @param array $options
  130. * Array of options to limit the scope of the returned list. This parameter
  131. * is similar to the $options parameter for
  132. * node_reference_potential_references(). An additional key is required:
  133. * - title_field: the name of the column holding entities 'titles' within the
  134. * entity base table.
  135. *
  136. * @return array
  137. * An array of entities, in the format expected by
  138. * node_reference_potential_references().
  139. *
  140. * @see node_reference_potential_references()
  141. * @see _node_reference_potential_references_views()
  142. *
  143. * @codingStandardsIgnoreStart
  144. */
  145. function references_potential_references_view($entity_type, $view_name, $display_name, $args, $options) {
  146. // @codingStandardsIgnoreEnd
  147. $entity_info = entity_get_info($entity_type);
  148. // Check that the view is valid and the display still exists.
  149. $view = views_get_view($view_name);
  150. if (!$view || $view->base_table != $entity_info['base table'] || !isset($view->display[$display_name])) {
  151. return FALSE;
  152. }
  153. // If we have no access to the View an empty result should be returned to
  154. // avoid triggering the fallback results.
  155. if (!$view->access(array($display_name))) {
  156. return array();
  157. }
  158. // Temporary backwards compatibility for fields migrated from CCK D6: accept
  159. // 'default' display, but dynamically add a 'references' display out of it.
  160. if ($display_name == 'default') {
  161. $display_name = $view->add_display('references');
  162. }
  163. $view->set_display($display_name);
  164. // @todo From merlinofchaos on IRC : arguments using summary view can defeat
  165. // the style setting.
  166. // We might also need to check if there's an argument, and set its
  167. // style_plugin as well.
  168. // Set additional options to let references_plugin_display::query() narrow
  169. // the results.
  170. $references_options = array(
  171. 'ids' => $options['ids'],
  172. 'title_field' => $options['title_field'],
  173. 'string' => $options['string'],
  174. 'match' => $options['match'],
  175. );
  176. $view->display_handler->set_option('references_options', $references_options);
  177. // We need the title field for autocomplete widgets, so add it (hidden) if not
  178. // present.
  179. $fields = $view->get_items('field', $display_name);
  180. if (!isset($fields[$options['title_field']])) {
  181. $label_options = array(
  182. 'exclude' => 1,
  183. );
  184. $view->add_item($display_name, 'field', $entity_info['base table'], $options['title_field'], $label_options);
  185. }
  186. // Make sure the query is not cached.
  187. $view->is_cacheable = FALSE;
  188. // Get the results.
  189. $results = $view->execute_display($display_name, $args);
  190. return $results;
  191. }
  192. /**
  193. * Implements hook_help().
  194. */
  195. function references_help($path, $arg) {
  196. switch ($path) {
  197. case 'admin/help#references':
  198. $output = '<h3>' . t('About') . '</h3>';
  199. $output .= '<p>' . t('The References project contains straight ports of the node_reference and user_reference modules to the Drupal 7 API.') . '</p>';
  200. $output .= '<h3>' . t('Usage') . '</h3>';
  201. $output .= '<ul>';
  202. $output .= '<li>' . t('Access: Structure » Content types and edit one Content type.') . '</li>';
  203. $output .= '<li>' . t('Go to Manage Fields and add new field with field type = "Node Reference" or "User Reference".') . '</li>';
  204. $output .= '<li>' . t('Access onde with this type and see new field with reference.') . '</li>';
  205. $output .= '</ul>';
  206. return $output;
  207. }
  208. }