entityreference.views.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. );
  76. }
  77. }
  78. }
  79. /**
  80. * Implements hook_views_plugins().
  81. */
  82. function entityreference_views_plugins() {
  83. $plugins = array(
  84. 'display' => array(
  85. 'entityreference' => array(
  86. 'title' => t('Entity Reference'),
  87. 'admin' => t('Entity Reference Source'),
  88. 'help' => 'Selects referenceable entities for an entity reference field',
  89. 'handler' => 'entityreference_plugin_display',
  90. 'uses hook menu' => FALSE,
  91. 'use ajax' => FALSE,
  92. 'use pager' => FALSE,
  93. 'accept attachments' => FALSE,
  94. // Custom property, used with views_get_applicable_views() to retrieve
  95. // all views with a 'Entity Reference' display.
  96. 'entityreference display' => TRUE,
  97. ),
  98. ),
  99. 'style' => array(
  100. 'entityreference_style' => array(
  101. 'title' => t('Entity Reference list'),
  102. 'help' => 'Returns results as a PHP array of labels and rendered rows.',
  103. 'handler' => 'entityreference_plugin_style',
  104. 'theme' => 'views_view_unformatted',
  105. 'uses row plugin' => TRUE,
  106. 'uses fields' => TRUE,
  107. 'uses options' => TRUE,
  108. 'type' => 'entityreference',
  109. 'even empty' => TRUE,
  110. ),
  111. ),
  112. 'row' => array(
  113. 'entityreference_fields' => array(
  114. 'title' => t('Inline fields'),
  115. 'help' => t('Displays the fields with an optional template.'),
  116. 'handler' => 'entityreference_plugin_row_fields',
  117. 'theme' => 'views_view_fields',
  118. 'theme path' => drupal_get_path('module', 'views') . '/theme',
  119. 'theme file' => 'theme.inc',
  120. 'uses fields' => TRUE,
  121. 'uses options' => TRUE,
  122. 'type' => 'entityreference',
  123. ),
  124. ),
  125. );
  126. return $plugins;
  127. }