Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / entity_reference_revisions / src / Plugin / views / display / EntityReferenceRevisions.php
1 <?php
2
3 namespace Drupal\entity_reference_revisions\Plugin\views\display;
4
5 use Drupal\views\Plugin\views\display\DisplayPluginBase;
6
7 /**
8  * The plugin that handles an EntityReferenceRevisions display.
9  *
10  * "entity_reference_revisions_display" is a custom property, used with
11  * \Drupal\views\Views::getApplicableViews() to retrieve all views with a
12  * 'Entity Reference Revisions' display.
13  *
14  * @ingroup views_display_plugins
15  *
16  * @ViewsDisplay(
17  *   id = "entity_reference_revisions",
18  *   title = @Translation("Entity Reference Revisions"),
19  *   admin = @Translation("Entity Reference Revisions Source"),
20  *   help = @Translation("Selects referenceable entities for an entity reference revisions field."),
21  *   theme = "views_view",
22  *   register_theme = FALSE,
23  *   uses_menu_links = FALSE,
24  *   entity_reference_revisions_display = TRUE
25  * )
26  */
27 class EntityReferenceRevisions extends DisplayPluginBase {
28
29   /**
30    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$useAJAX.
31    */
32   protected $usesAJAX = FALSE;
33
34   /**
35    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesPager.
36    */
37   protected $usesPager = FALSE;
38
39   /**
40    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAttachments.
41    */
42   protected $usesAttachments = FALSE;
43
44   /**
45    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::defineOptions().
46    */
47   protected function defineOptions() {
48     $options = parent::defineOptions();
49
50     // Force the style plugin to 'entity_reference_style' and the row plugin to
51     // 'fields'.
52     $options['style']['contains']['type'] = array('default' => 'entity_reference_revisions');
53     $options['defaults']['default']['style'] = FALSE;
54     $options['row']['contains']['type'] = array('default' => 'entity_reference_revisions');
55     $options['defaults']['default']['row'] = FALSE;
56
57     // Make sure the query is not cached.
58     $options['defaults']['default']['cache'] = FALSE;
59
60     // Set the display title to an empty string (not used in this display type).
61     $options['title']['default'] = '';
62     $options['defaults']['default']['title'] = FALSE;
63
64     return $options;
65   }
66
67   /**
68    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::optionsSummary().
69    *
70    * Disable 'cache' and 'title' so it won't be changed.
71    */
72   public function optionsSummary(&$categories, &$options) {
73     parent::optionsSummary($categories, $options);
74     unset($options['query']);
75     unset($options['title']);
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getType() {
82     return 'entity_reference_revisions';
83   }
84
85   /**
86    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::execute().
87    */
88   public function execute() {
89     return $this->view->render($this->display['id']);
90   }
91
92   /**
93    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::render().
94    */
95   public function render() {
96     if (!empty($this->view->result) && $this->view->style_plugin->evenEmpty()) {
97       return $this->view->style_plugin->render($this->view->result);
98     }
99     return '';
100   }
101
102   /**
103    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::usesExposed().
104    */
105   public function usesExposed() {
106     return FALSE;
107   }
108
109   /**
110    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::query().
111    */
112   public function query() {
113     if (!empty($this->view->live_preview)) {
114       return;
115     }
116
117     // Make sure the id field is included in the results.
118     $id_field = $this->view->storage->get('base_field');
119     $this->id_field_alias = $this->view->query->addField($this->view->storage->get('base_table'), $id_field);
120
121     $options = $this->getOption('entity_reference_revisions_options');
122
123     // Restrict the autocomplete options based on what's been typed already.
124     if (isset($options['match'])) {
125       $style_options = $this->getOption('style');
126       $value = db_like($options['match']) . '%';
127       if ($options['match_operator'] != 'STARTS_WITH') {
128         $value = '%' . $value;
129       }
130
131       // Multiple search fields are OR'd together.
132       $conditions = db_or();
133
134       // Build the condition using the selected search fields.
135       foreach ($style_options['options']['search_fields'] as $field_alias) {
136         if (!empty($field_alias)) {
137           // Get the table and field names for the checked field.
138           $field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
139           // Add an OR condition for the field.
140           $conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
141         }
142       }
143
144       $this->view->query->addWhere(0, $conditions);
145     }
146
147     // Add an IN condition for validation.
148     if (!empty($options['ids'])) {
149       $this->view->query->addWhere(0, $id_field, $options['ids']);
150     }
151
152     $this->view->setItemsPerPage($options['limit']);
153   }
154
155   /**
156    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::validate().
157    */
158   public function validate() {
159     $errors = parent::validate();
160     // Verify that search fields are set up.
161     $style = $this->getOption('style');
162     if (!isset($style['options']['search_fields'])) {
163       $errors[] = $this->t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference Revisions list format.', array('@display' => $this->display['display_title']));
164     }
165     else {
166       // Verify that the search fields used actually exist.
167       $fields = array_keys($this->handlers['field']);
168       foreach ($style['options']['search_fields'] as $field_alias => $enabled) {
169         if ($enabled && !in_array($field_alias, $fields)) {
170           $errors[] = $this->t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference Revisions list format.', array('@display' => $this->display['display_title'], '%field' => $field_alias));
171         }
172       }
173     }
174     return $errors;
175   }
176 }