entityreference_plugin_display.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. else {
  67. $this->view->query->add_field($this->view->field[$field_alias]->options['table'], $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->options['field'], array());
  68. $field = $this->view->query->fields[$this->view->field[$field_alias]->options['field']];
  69. }
  70. // Add an OR condition for the field
  71. $conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
  72. }
  73. }
  74. $this->view->query->add_where(NULL, $conditions);
  75. }
  76. // Add an IN condition for validation.
  77. if (!empty($options['ids'])) {
  78. $this->view->query->add_where(NULL, $id_field, $options['ids']);
  79. }
  80. $this->view->set_items_per_page($options['limit']);
  81. }
  82. /**
  83. * Extend the default validation.
  84. */
  85. function validate() {
  86. $errors = parent::validate();
  87. // Verify that search fields are set up.
  88. $style_options = $this->get_option('style_options');
  89. if (!isset($style_options['search_fields'])) {
  90. $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));
  91. }
  92. else {
  93. // Verify that the search fields used actually exist.
  94. //$fields = array_keys($this->view->get_items('field'));
  95. $fields = array_keys($this->handlers['field']);
  96. foreach ($style_options['search_fields'] as $field_alias => $enabled) {
  97. if ($enabled && !in_array($field_alias, $fields)) {
  98. $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));
  99. }
  100. }
  101. }
  102. return $errors;
  103. }
  104. }