EntityReference_SelectionHandler_Views.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Entity handler for Views.
  4. */
  5. class EntityReference_SelectionHandler_Views implements EntityReference_SelectionHandler {
  6. /**
  7. * Implements EntityReferenceHandler::getInstance().
  8. */
  9. public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
  10. return new EntityReference_SelectionHandler_Views($field, $instance);
  11. }
  12. protected function __construct($field, $instance) {
  13. $this->field = $field;
  14. $this->instance = $instance;
  15. }
  16. /**
  17. * Implements EntityReferenceHandler::settingsForm().
  18. */
  19. public static function settingsForm($field, $instance) {
  20. $view_settings = empty($field['settings']['handler_settings']['view']) ? '' : $field['settings']['handler_settings']['view'];
  21. $displays = views_get_applicable_views('entityreference display');
  22. // Filter views that list the entity type we want, and group the separate
  23. // displays by view.
  24. $entity_info = entity_get_info($field['settings']['target_type']);
  25. $options = array();
  26. foreach ($displays as $data) {
  27. list($view, $display_id) = $data;
  28. if ($view->base_table == $entity_info['base table']) {
  29. $options[$view->name . ':' . $display_id] = $view->name . ' - ' . $view->display[$display_id]->display_title;
  30. }
  31. }
  32. // The value of the 'view_and_display' select below will need to be split
  33. // into 'view_name' and 'view_display' in the final submitted values, so
  34. // we massage the data at validate time on the wrapping element (not
  35. // ideal).
  36. $form['view']['#element_validate'] = array('entityreference_view_settings_validate');
  37. if ($options) {
  38. $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
  39. $form['view']['view_and_display'] = array(
  40. '#type' => 'select',
  41. '#title' => t('View used to select the entities'),
  42. '#required' => TRUE,
  43. '#options' => $options,
  44. '#default_value' => $default,
  45. '#description' => '<p>' . t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>',
  46. );
  47. $default = !empty($view_settings['args']) ? implode(', ', $view_settings['args']) : '';
  48. $form['view']['args'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('View arguments'),
  51. '#default_value' => $default,
  52. '#required' => FALSE,
  53. '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
  54. );
  55. }
  56. else {
  57. $form['view']['no_view_help'] = array(
  58. '#markup' => '<p>' . t('No eligible views were found. <a href="@create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href="@existing">existing view</a>.', array(
  59. '@create' => url('admin/structure/views/add'),
  60. '@existing' => url('admin/structure/views'),
  61. )) . '</p>',
  62. );
  63. }
  64. return $form;
  65. }
  66. protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
  67. $view_name = $this->field['settings']['handler_settings']['view']['view_name'];
  68. $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
  69. $args = $this->field['settings']['handler_settings']['view']['args'];
  70. $entity_type = $this->field['settings']['target_type'];
  71. // Check that the view is valid and the display still exists.
  72. $this->view = views_get_view($view_name);
  73. if (!$this->view || !isset($this->view->display[$display_name]) || !$this->view->access($display_name)) {
  74. watchdog('entityreference', 'The view %view_name is no longer eligible for the %field_name field.', array('%view_name' => $view_name, '%field_name' => $this->instance['label']), WATCHDOG_WARNING);
  75. return FALSE;
  76. }
  77. $this->view->set_display($display_name);
  78. // Make sure the query is not cached.
  79. $this->view->is_cacheable = FALSE;
  80. // Pass options to the display handler to make them available later.
  81. $entityreference_options = array(
  82. 'match' => $match,
  83. 'match_operator' => $match_operator,
  84. 'limit' => $limit,
  85. 'ids' => $ids,
  86. );
  87. $this->view->display_handler->set_option('entityreference_options', $entityreference_options);
  88. return TRUE;
  89. }
  90. /**
  91. * Implements EntityReferenceHandler::getReferencableEntities().
  92. */
  93. public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  94. $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
  95. $args = $this->field['settings']['handler_settings']['view']['args'];
  96. $result = array();
  97. if ($this->initializeView($match, $match_operator, $limit)) {
  98. // Get the results.
  99. $result = $this->view->execute_display($display_name, $args);
  100. }
  101. $return = array();
  102. if ($result) {
  103. $target_type = $this->field['settings']['target_type'];
  104. $entities = entity_load($target_type, array_keys($result));
  105. foreach($entities as $entity) {
  106. list($id,, $bundle) = entity_extract_ids($target_type, $entity);
  107. $return[$bundle][$id] = $result[$id];
  108. }
  109. }
  110. return $return;
  111. }
  112. /**
  113. * Implements EntityReferenceHandler::countReferencableEntities().
  114. */
  115. function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
  116. $this->getReferencableEntities($match, $match_operator);
  117. return $this->view->total_items;
  118. }
  119. function validateReferencableEntities(array $ids) {
  120. $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
  121. $args = $this->field['settings']['handler_settings']['view']['args'];
  122. $result = array();
  123. if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) {
  124. // Get the results.
  125. $entities = $this->view->execute_display($display_name, $args);
  126. $result = array_keys($entities);
  127. }
  128. return $result;
  129. }
  130. /**
  131. * Implements EntityReferenceHandler::validateAutocompleteInput().
  132. */
  133. public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
  134. return NULL;
  135. }
  136. /**
  137. * Implements EntityReferenceHandler::getLabel().
  138. */
  139. public function getLabel($entity) {
  140. return entity_label($this->field['settings']['target_type'], $entity);
  141. }
  142. /**
  143. * Implements EntityReferenceHandler::entityFieldQueryAlter().
  144. */
  145. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  146. }
  147. }
  148. function entityreference_view_settings_validate($element, &$form_state, $form) {
  149. // Split view name and display name from the 'view_and_display' value.
  150. if (!empty($element['view_and_display']['#value'])) {
  151. list($view, $display) = explode(':', $element['view_and_display']['#value']);
  152. }
  153. else {
  154. form_error($element, t('The views entity selection mode requires a view.'));
  155. return;
  156. }
  157. // Explode the 'args' string into an actual array. Beware, explode() turns an
  158. // empty string into an array with one empty string. We'll need an empty array
  159. // instead.
  160. $args_string = trim($element['args']['#value']);
  161. if ($args_string === '') {
  162. $args = array();
  163. }
  164. else {
  165. // array_map is called to trim whitespaces from the arguments.
  166. $args = array_map('trim', explode(',', $args_string));
  167. }
  168. $value = array('view_name' => $view, 'display_name' => $display, 'args' => $args);
  169. form_set_value($element, $value, $form_state);
  170. }