entityreference_plugin_display.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Handler for entityreference_plugin_display.
  5. */
  6. class entityreference_plugin_display extends views_plugin_display {
  7. function option_definition() {
  8. $options = parent::option_definition();
  9. // Force the style plugin to 'entityreference_style' and the row plugin to
  10. // 'fields'.
  11. $options['style_plugin']['default'] = 'entityreference_style';
  12. $options['defaults']['default']['style_plugin'] = FALSE;
  13. $options['defaults']['default']['style_options'] = FALSE;
  14. $options['row_plugin']['default'] = 'entityreference_fields';
  15. $options['defaults']['default']['row_plugin'] = FALSE;
  16. $options['defaults']['default']['row_options'] = FALSE;
  17. // Set the display title to an empty string (not used in this display type).
  18. $options['title']['default'] = '';
  19. $options['defaults']['default']['title'] = FALSE;
  20. return $options;
  21. }
  22. function get_style_type() {
  23. return 'entityreference';
  24. }
  25. function execute() {
  26. return $this->view->render($this->display->id);
  27. }
  28. function render() {
  29. if (!empty($this->view->result) || !empty($this->view->style_plugin->definition['even empty'])) {
  30. return $this->view->style_plugin->render($this->view->result);
  31. }
  32. return '';
  33. }
  34. function uses_exposed() {
  35. return FALSE;
  36. }
  37. function query() {
  38. $options = $this->get_option('entityreference_options');
  39. // Play nice with Views UI 'preview' : if the view is not executed through
  40. // EntityReference_SelectionHandler_Views::getReferencableEntities(),
  41. // don't alter the query.
  42. if (empty($options)) {
  43. return;
  44. }
  45. // Make sure the id field is included in the results, and save its alias
  46. // so that references_plugin_style can retrieve it.
  47. $this->id_field_alias = $id_field = $this->view->query->add_field($this->view->base_table, $this->view->base_field);
  48. if (strpos($id_field, '.') === FALSE) {
  49. $id_field = $this->view->base_table . '.' . $this->id_field_alias;
  50. }
  51. // Restrict the autocomplete options based on what's been typed already.
  52. if (isset($options['match'])) {
  53. $style_options = $this->get_option('style_options');
  54. $value = db_like($options['match']) . '%';
  55. if ($options['match_operator'] != 'STARTS_WITH') {
  56. $value = '%' . $value;
  57. }
  58. // Multiple search fields are OR'd together
  59. $conditions = db_or();
  60. // Build the condition using the selected search fields
  61. foreach ($style_options['search_fields'] as $field_alias) {
  62. if (!empty($field_alias)) {
  63. // Get the table and field names for the checked field.
  64. if (empty($this->view->field[$field_alias]->field_info)) {
  65. $field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
  66. }
  67. else {
  68. $field_table = $this->view->query->ensure_table($this->view->field[$field_alias]->table, $this->view->field[$field_alias]->relationship);
  69. $this->view->query->add_field($field_table, $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->field, array());
  70. $field = $this->view->query->fields[$this->view->field[$field_alias]->field];
  71. }
  72. // Add an OR condition for the field
  73. $conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
  74. }
  75. }
  76. $this->view->query->add_where(NULL, $conditions);
  77. }
  78. // Add an IN condition for validation.
  79. if (!empty($options['ids'])) {
  80. $this->view->query->add_where(NULL, $id_field, $options['ids']);
  81. }
  82. $this->view->set_items_per_page($options['limit']);
  83. }
  84. /**
  85. * Extend the default validation.
  86. */
  87. function validate() {
  88. $errors = parent::validate();
  89. // Verify that search fields are set up.
  90. $style_options = $this->get_option('style_options');
  91. if (!isset($style_options['search_fields'])) {
  92. $errors[] = t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title));
  93. }
  94. else {
  95. // Verify that the search fields used actually exist.
  96. //$fields = array_keys($this->view->get_items('field'));
  97. $fields = array_keys($this->handlers['field']);
  98. foreach ($style_options['search_fields'] as $field_alias => $enabled) {
  99. if ($enabled && !in_array($field_alias, $fields)) {
  100. $errors[] = t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title, '%field' => $field_alias));
  101. }
  102. }
  103. }
  104. return $errors;
  105. }
  106. }