entityreference.views.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @file
  4. * Views integration for Entity Reference.
  5. */
  6. /**
  7. * Implements hook_field_views_data().
  8. */
  9. function entityreference_field_views_data($field) {
  10. $data = field_views_field_default_views_data($field);
  11. $entity_info = entity_get_info($field['settings']['target_type']);
  12. foreach ($data as $table_name => $table_data) {
  13. if (isset($entity_info['base table'])) {
  14. $entity = $entity_info['label'];
  15. if ($entity == t('Node')) {
  16. $entity = t('Content');
  17. }
  18. $field_name = $field['field_name'] . '_target_id';
  19. $parameters = array('@entity' => $entity, '!field_name' => $field['field_name']);
  20. $data[$table_name][$field_name]['relationship'] = array(
  21. 'handler' => 'views_handler_relationship',
  22. 'base' => $entity_info['base table'],
  23. 'base field' => $entity_info['entity keys']['id'],
  24. 'label' => t('@entity entity referenced from !field_name', $parameters),
  25. 'group' => t('Entity Reference'),
  26. 'title' => t('Referenced Entity'),
  27. 'help' => t('A bridge to the @entity entity that is referenced via !field_name', $parameters),
  28. );
  29. }
  30. }
  31. // Invoke the behaviors to allow them to change the properties.
  32. foreach (entityreference_get_behavior_handlers($field) as $handler) {
  33. $handler->views_data_alter($data, $field);
  34. }
  35. return $data;
  36. }
  37. /**
  38. * Options callback for Views handler views_handler_filter_in_operator.
  39. */
  40. function entityreference_views_handler_options_list($field_name) {
  41. $field = field_info_field($field_name);
  42. return entityreference_options_list($field);
  43. }
  44. /**
  45. * Implements hook_field_views_data_views_data_alter().
  46. *
  47. * Views integration to provide reverse relationships on entityreference fields.
  48. */
  49. function entityreference_field_views_data_views_data_alter(&$data, $field) {
  50. foreach ($field['bundles'] as $entity_type => $bundles) {
  51. $target_entity_info = entity_get_info($field['settings']['target_type']);
  52. if (isset($target_entity_info['base table'])) {
  53. $entity_info = entity_get_info($entity_type);
  54. $entity = $entity_info['label'];
  55. if ($entity == t('Node')) {
  56. $entity = t('Content');
  57. }
  58. $target_entity = $target_entity_info['label'];
  59. if ($target_entity == t('Node')) {
  60. $target_entity = t('Content');
  61. }
  62. $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type;
  63. $replacements = array('@entity' => $entity, '@target_entity' => $target_entity, '!field_name' => $field['field_name']);
  64. $data[$target_entity_info['base table']][$pseudo_field_name]['relationship'] = array(
  65. 'handler' => 'views_handler_relationship_entity_reverse',
  66. 'field_name' => $field['field_name'],
  67. 'field table' => _field_sql_storage_tablename($field),
  68. 'field field' => $field['field_name'] . '_target_id',
  69. 'base' => $entity_info['base table'],
  70. 'base field' => $entity_info['entity keys']['id'],
  71. 'label' => t('@entity referencing @target_entity from !field_name', $replacements),
  72. 'group' => t('Entity Reference'),
  73. 'title' => t('Referencing entity'),
  74. 'help' => t('A bridge to the @entity entity that is referencing @target_entity via !field_name', $replacements),
  75. 'join_extra' => array(
  76. 0 => array(
  77. 'field' => 'entity_type',
  78. 'value' => $entity_type,
  79. ),
  80. 1 => array(
  81. 'field' => 'deleted',
  82. 'value' => 0,
  83. 'numeric' => TRUE,
  84. ),
  85. ),
  86. );
  87. }
  88. }
  89. }
  90. /**
  91. * Implements hook_views_plugins().
  92. */
  93. function entityreference_views_plugins() {
  94. $plugins = array(
  95. 'display' => array(
  96. 'entityreference' => array(
  97. 'title' => t('Entity Reference'),
  98. 'admin' => t('Entity Reference Source'),
  99. 'help' => 'Selects referenceable entities for an entity reference field',
  100. 'handler' => 'entityreference_plugin_display',
  101. 'uses hook menu' => FALSE,
  102. 'use ajax' => FALSE,
  103. 'use pager' => FALSE,
  104. 'accept attachments' => FALSE,
  105. // Custom property, used with views_get_applicable_views() to retrieve
  106. // all views with a 'Entity Reference' display.
  107. 'entityreference display' => TRUE,
  108. ),
  109. ),
  110. 'style' => array(
  111. 'entityreference_style' => array(
  112. 'title' => t('Entity Reference list'),
  113. 'help' => 'Returns results as a PHP array of labels and rendered rows.',
  114. 'handler' => 'entityreference_plugin_style',
  115. 'theme' => 'views_view_unformatted',
  116. 'uses row plugin' => TRUE,
  117. 'uses fields' => TRUE,
  118. 'uses options' => TRUE,
  119. 'type' => 'entityreference',
  120. 'even empty' => TRUE,
  121. ),
  122. ),
  123. 'row' => array(
  124. 'entityreference_fields' => array(
  125. 'title' => t('Inline fields'),
  126. 'help' => t('Displays the fields with an optional template.'),
  127. 'handler' => 'entityreference_plugin_row_fields',
  128. 'theme' => 'views_view_fields',
  129. 'theme path' => drupal_get_path('module', 'views') . '/theme',
  130. 'theme file' => 'theme.inc',
  131. 'uses fields' => TRUE,
  132. 'uses options' => TRUE,
  133. 'type' => 'entityreference',
  134. ),
  135. ),
  136. );
  137. return $plugins;
  138. }