Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\views\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
6 use Drupal\Core\Cache\Cache;
|
Chris@0
|
7 use Drupal\Core\Config\Entity\ConfigEntityBase;
|
Chris@0
|
8 use Drupal\Core\Entity\ContentEntityTypeInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
10 use Drupal\Core\Entity\FieldableEntityInterface;
|
Chris@0
|
11 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
12 use Drupal\views\Plugin\DependentWithRemovalPluginInterface;
|
Chris@0
|
13 use Drupal\views\Views;
|
Chris@0
|
14 use Drupal\views\ViewEntityInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Defines a View configuration entity class.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @ConfigEntityType(
|
Chris@0
|
20 * id = "view",
|
Chris@0
|
21 * label = @Translation("View", context = "View entity type"),
|
Chris@0
|
22 * admin_permission = "administer views",
|
Chris@0
|
23 * entity_keys = {
|
Chris@0
|
24 * "id" = "id",
|
Chris@0
|
25 * "label" = "label",
|
Chris@0
|
26 * "status" = "status"
|
Chris@0
|
27 * },
|
Chris@0
|
28 * config_export = {
|
Chris@0
|
29 * "id",
|
Chris@0
|
30 * "label",
|
Chris@0
|
31 * "module",
|
Chris@0
|
32 * "description",
|
Chris@0
|
33 * "tag",
|
Chris@0
|
34 * "base_table",
|
Chris@0
|
35 * "base_field",
|
Chris@0
|
36 * "core",
|
Chris@0
|
37 * "display",
|
Chris@0
|
38 * }
|
Chris@0
|
39 * )
|
Chris@0
|
40 */
|
Chris@0
|
41 class View extends ConfigEntityBase implements ViewEntityInterface {
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * The name of the base table this view will use.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var string
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $base_table = 'node';
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * The unique ID of the view.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var string
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $id = NULL;
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * The label of the view.
|
Chris@0
|
59 */
|
Chris@0
|
60 protected $label;
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * The description of the view, which is used only in the interface.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @var string
|
Chris@0
|
66 */
|
Chris@0
|
67 protected $description = '';
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * The "tags" of a view.
|
Chris@0
|
71 *
|
Chris@0
|
72 * The tags are stored as a single string, though it is used as multiple tags
|
Chris@0
|
73 * for example in the views overview.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @var string
|
Chris@0
|
76 */
|
Chris@0
|
77 protected $tag = '';
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * The core version the view was created for.
|
Chris@0
|
81 *
|
Chris@0
|
82 * @var string
|
Chris@0
|
83 */
|
Chris@0
|
84 protected $core = \Drupal::CORE_COMPATIBILITY;
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Stores all display handlers of this view.
|
Chris@0
|
88 *
|
Chris@0
|
89 * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase
|
Chris@0
|
90 * objects.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @var array
|
Chris@0
|
93 */
|
Chris@0
|
94 protected $display = [];
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * The name of the base field to use.
|
Chris@0
|
98 *
|
Chris@0
|
99 * @var string
|
Chris@0
|
100 */
|
Chris@0
|
101 protected $base_field = 'nid';
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Stores a reference to the executable version of this view.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @var \Drupal\views\ViewExecutable
|
Chris@0
|
107 */
|
Chris@0
|
108 protected $executable;
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * The module implementing this view.
|
Chris@0
|
112 *
|
Chris@0
|
113 * @var string
|
Chris@0
|
114 */
|
Chris@0
|
115 protected $module = 'views';
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * {@inheritdoc}
|
Chris@0
|
119 */
|
Chris@0
|
120 public function getExecutable() {
|
Chris@0
|
121 // Ensure that an executable View is available.
|
Chris@0
|
122 if (!isset($this->executable)) {
|
Chris@0
|
123 $this->executable = Views::executableFactory()->get($this);
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 return $this->executable;
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 /**
|
Chris@0
|
130 * {@inheritdoc}
|
Chris@0
|
131 */
|
Chris@0
|
132 public function createDuplicate() {
|
Chris@0
|
133 $duplicate = parent::createDuplicate();
|
Chris@0
|
134 unset($duplicate->executable);
|
Chris@0
|
135 return $duplicate;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * {@inheritdoc}
|
Chris@0
|
140 */
|
Chris@0
|
141 public function label() {
|
Chris@0
|
142 if (!$label = $this->get('label')) {
|
Chris@0
|
143 $label = $this->id();
|
Chris@0
|
144 }
|
Chris@0
|
145 return $label;
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * {@inheritdoc}
|
Chris@0
|
150 */
|
Chris@0
|
151 public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
|
Chris@0
|
152 if (empty($plugin_id)) {
|
Chris@0
|
153 return FALSE;
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 $plugin = Views::pluginManager('display')->getDefinition($plugin_id);
|
Chris@0
|
157
|
Chris@0
|
158 if (empty($plugin)) {
|
Chris@0
|
159 $plugin['title'] = t('Broken');
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 if (empty($id)) {
|
Chris@0
|
163 $id = $this->generateDisplayId($plugin_id);
|
Chris@0
|
164
|
Chris@0
|
165 // Generate a unique human-readable name by inspecting the counter at the
|
Chris@0
|
166 // end of the previous display ID, e.g., 'page_1'.
|
Chris@0
|
167 if ($id !== 'default') {
|
Chris@0
|
168 preg_match("/[0-9]+/", $id, $count);
|
Chris@0
|
169 $count = $count[0];
|
Chris@0
|
170 }
|
Chris@0
|
171 else {
|
Chris@0
|
172 $count = '';
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 if (empty($title)) {
|
Chris@0
|
176 // If there is no title provided, use the plugin title, and if there are
|
Chris@0
|
177 // multiple displays, append the count.
|
Chris@0
|
178 $title = $plugin['title'];
|
Chris@0
|
179 if ($count > 1) {
|
Chris@0
|
180 $title .= ' ' . $count;
|
Chris@0
|
181 }
|
Chris@0
|
182 }
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 $display_options = [
|
Chris@0
|
186 'display_plugin' => $plugin_id,
|
Chris@0
|
187 'id' => $id,
|
Chris@0
|
188 // Cast the display title to a string since it is an object.
|
Chris@0
|
189 // @see \Drupal\Core\StringTranslation\TranslatableMarkup
|
Chris@0
|
190 'display_title' => (string) $title,
|
Chris@0
|
191 'position' => $id === 'default' ? 0 : count($this->display),
|
Chris@0
|
192 'display_options' => [],
|
Chris@0
|
193 ];
|
Chris@0
|
194
|
Chris@0
|
195 // Add the display options to the view.
|
Chris@0
|
196 $this->display[$id] = $display_options;
|
Chris@0
|
197 return $id;
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Generates a display ID of a certain plugin type.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @param string $plugin_id
|
Chris@0
|
204 * Which plugin should be used for the new display ID.
|
Chris@0
|
205 *
|
Chris@0
|
206 * @return string
|
Chris@0
|
207 */
|
Chris@0
|
208 protected function generateDisplayId($plugin_id) {
|
Chris@0
|
209 // 'default' is singular and is unique, so just go with 'default'
|
Chris@0
|
210 // for it. For all others, start counting.
|
Chris@0
|
211 if ($plugin_id == 'default') {
|
Chris@0
|
212 return 'default';
|
Chris@0
|
213 }
|
Chris@0
|
214 // Initial ID.
|
Chris@0
|
215 $id = $plugin_id . '_1';
|
Chris@0
|
216 $count = 1;
|
Chris@0
|
217
|
Chris@0
|
218 // Loop through IDs based upon our style plugin name until
|
Chris@0
|
219 // we find one that is unused.
|
Chris@0
|
220 while (!empty($this->display[$id])) {
|
Chris@0
|
221 $id = $plugin_id . '_' . ++$count;
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 return $id;
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * {@inheritdoc}
|
Chris@0
|
229 */
|
Chris@0
|
230 public function &getDisplay($display_id) {
|
Chris@0
|
231 return $this->display[$display_id];
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 /**
|
Chris@0
|
235 * {@inheritdoc}
|
Chris@0
|
236 */
|
Chris@0
|
237 public function duplicateDisplayAsType($old_display_id, $new_display_type) {
|
Chris@0
|
238 $executable = $this->getExecutable();
|
Chris@0
|
239 $display = $executable->newDisplay($new_display_type);
|
Chris@0
|
240 $new_display_id = $display->display['id'];
|
Chris@0
|
241 $displays = $this->get('display');
|
Chris@0
|
242
|
Chris@0
|
243 // Let the display title be generated by the addDisplay method and set the
|
Chris@0
|
244 // right display plugin, but keep the rest from the original display.
|
Chris@0
|
245 $display_duplicate = $displays[$old_display_id];
|
Chris@0
|
246 unset($display_duplicate['display_title']);
|
Chris@0
|
247 unset($display_duplicate['display_plugin']);
|
Chris@0
|
248
|
Chris@0
|
249 $displays[$new_display_id] = NestedArray::mergeDeep($displays[$new_display_id], $display_duplicate);
|
Chris@0
|
250 $displays[$new_display_id]['id'] = $new_display_id;
|
Chris@0
|
251
|
Chris@0
|
252 // First set the displays.
|
Chris@0
|
253 $this->set('display', $displays);
|
Chris@0
|
254
|
Chris@0
|
255 // Ensure that we just copy display options, which are provided by the new
|
Chris@0
|
256 // display plugin.
|
Chris@0
|
257 $executable->setDisplay($new_display_id);
|
Chris@0
|
258
|
Chris@0
|
259 $executable->display_handler->filterByDefinedOptions($displays[$new_display_id]['display_options']);
|
Chris@0
|
260 // Update the display settings.
|
Chris@0
|
261 $this->set('display', $displays);
|
Chris@0
|
262
|
Chris@0
|
263 return $new_display_id;
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * {@inheritdoc}
|
Chris@0
|
268 */
|
Chris@0
|
269 public function calculateDependencies() {
|
Chris@0
|
270 parent::calculateDependencies();
|
Chris@0
|
271
|
Chris@0
|
272 // Ensure that the view is dependant on the module that implements the view.
|
Chris@0
|
273 $this->addDependency('module', $this->module);
|
Chris@0
|
274
|
Chris@0
|
275 $executable = $this->getExecutable();
|
Chris@0
|
276 $executable->initDisplay();
|
Chris@0
|
277 $executable->initStyle();
|
Chris@0
|
278
|
Chris@0
|
279 foreach ($executable->displayHandlers as $display) {
|
Chris@0
|
280 // Calculate the dependencies each display has.
|
Chris@0
|
281 $this->calculatePluginDependencies($display);
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 return $this;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * {@inheritdoc}
|
Chris@0
|
289 */
|
Chris@0
|
290 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
291 parent::preSave($storage);
|
Chris@0
|
292
|
Chris@0
|
293 $displays = $this->get('display');
|
Chris@0
|
294
|
Chris@0
|
295 $this->fixTableNames($displays);
|
Chris@0
|
296
|
Chris@0
|
297 // Sort the displays.
|
Chris@0
|
298 ksort($displays);
|
Chris@0
|
299 $this->set('display', ['default' => $displays['default']] + $displays);
|
Chris@0
|
300
|
Chris@0
|
301 // @todo Check whether isSyncing is needed.
|
Chris@0
|
302 if (!$this->isSyncing()) {
|
Chris@0
|
303 $this->addCacheMetadata();
|
Chris@0
|
304 }
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 /**
|
Chris@0
|
308 * Fixes table names for revision metadata fields of revisionable entities.
|
Chris@0
|
309 *
|
Chris@0
|
310 * Views for revisionable entity types using revision metadata fields might
|
Chris@0
|
311 * be using the wrong table to retrieve the fields after system_update_8300
|
Chris@0
|
312 * has moved them correctly to the revision table. This method updates the
|
Chris@0
|
313 * views to use the correct tables.
|
Chris@0
|
314 *
|
Chris@0
|
315 * @param array &$displays
|
Chris@0
|
316 * An array containing display handlers of a view.
|
Chris@0
|
317 *
|
Chris@0
|
318 * @deprecated in Drupal 8.3.0, will be removed in Drupal 9.0.0.
|
Chris@0
|
319 */
|
Chris@0
|
320 private function fixTableNames(array &$displays) {
|
Chris@0
|
321 // Fix wrong table names for entity revision metadata fields.
|
Chris@0
|
322 foreach ($displays as $display => $display_data) {
|
Chris@0
|
323 if (isset($display_data['display_options']['fields'])) {
|
Chris@0
|
324 foreach ($display_data['display_options']['fields'] as $property_name => $property_data) {
|
Chris@0
|
325 if (isset($property_data['entity_type']) && isset($property_data['field']) && isset($property_data['table'])) {
|
Chris@0
|
326 $entity_type = $this->entityTypeManager()->getDefinition($property_data['entity_type']);
|
Chris@0
|
327 // We need to update the table name only for revisionable entity
|
Chris@0
|
328 // types, otherwise the view is already using the correct table.
|
Chris@0
|
329 if (($entity_type instanceof ContentEntityTypeInterface) && is_subclass_of($entity_type->getClass(), FieldableEntityInterface::class) && $entity_type->isRevisionable()) {
|
Chris@0
|
330 $revision_metadata_fields = $entity_type->getRevisionMetadataKeys();
|
Chris@0
|
331 // @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage::initTableLayout()
|
Chris@0
|
332 $revision_table = $entity_type->getRevisionTable() ?: $entity_type->id() . '_revision';
|
Chris@0
|
333
|
Chris@0
|
334 // Check if this is a revision metadata field and if it uses the
|
Chris@0
|
335 // wrong table.
|
Chris@0
|
336 if (in_array($property_data['field'], $revision_metadata_fields) && $property_data['table'] != $revision_table) {
|
Chris@0
|
337 $displays[$display]['display_options']['fields'][$property_name]['table'] = $revision_table;
|
Chris@0
|
338 }
|
Chris@0
|
339 }
|
Chris@0
|
340 }
|
Chris@0
|
341 }
|
Chris@0
|
342 }
|
Chris@0
|
343 }
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@0
|
346 /**
|
Chris@0
|
347 * Fills in the cache metadata of this view.
|
Chris@0
|
348 *
|
Chris@0
|
349 * Cache metadata is set per view and per display, and ends up being stored in
|
Chris@0
|
350 * the view's configuration. This allows Views to determine very efficiently:
|
Chris@0
|
351 * - the max-age
|
Chris@0
|
352 * - the cache contexts
|
Chris@0
|
353 * - the cache tags
|
Chris@0
|
354 *
|
Chris@0
|
355 * In other words: this allows us to do the (expensive) work of initializing
|
Chris@0
|
356 * Views plugins and handlers to determine their effect on the cacheability of
|
Chris@0
|
357 * a view at save time rather than at runtime.
|
Chris@0
|
358 */
|
Chris@0
|
359 protected function addCacheMetadata() {
|
Chris@0
|
360 $executable = $this->getExecutable();
|
Chris@0
|
361
|
Chris@0
|
362 $current_display = $executable->current_display;
|
Chris@0
|
363 $displays = $this->get('display');
|
Chris@0
|
364 foreach (array_keys($displays) as $display_id) {
|
Chris@0
|
365 $display =& $this->getDisplay($display_id);
|
Chris@0
|
366 $executable->setDisplay($display_id);
|
Chris@0
|
367
|
Chris@0
|
368 $cache_metadata = $executable->getDisplay()->calculateCacheMetadata();
|
Chris@0
|
369 $display['cache_metadata']['max-age'] = $cache_metadata->getCacheMaxAge();
|
Chris@0
|
370 $display['cache_metadata']['contexts'] = $cache_metadata->getCacheContexts();
|
Chris@0
|
371 $display['cache_metadata']['tags'] = $cache_metadata->getCacheTags();
|
Chris@0
|
372 // Always include at least the 'languages:' context as there will most
|
Chris@0
|
373 // probably be translatable strings in the view output.
|
Chris@0
|
374 $display['cache_metadata']['contexts'] = Cache::mergeContexts($display['cache_metadata']['contexts'], ['languages:' . LanguageInterface::TYPE_INTERFACE]);
|
Chris@0
|
375 }
|
Chris@0
|
376 // Restore the previous active display.
|
Chris@0
|
377 $executable->setDisplay($current_display);
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 /**
|
Chris@0
|
381 * {@inheritdoc}
|
Chris@0
|
382 */
|
Chris@0
|
383 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
384 parent::postSave($storage, $update);
|
Chris@0
|
385
|
Chris@0
|
386 // @todo Remove if views implements a view_builder controller.
|
Chris@0
|
387 views_invalidate_cache();
|
Chris@0
|
388 $this->invalidateCaches();
|
Chris@0
|
389
|
Chris@0
|
390 // Rebuild the router if this is a new view, or it's status changed.
|
Chris@0
|
391 if (!isset($this->original) || ($this->status() != $this->original->status())) {
|
Chris@0
|
392 \Drupal::service('router.builder')->setRebuildNeeded();
|
Chris@0
|
393 }
|
Chris@0
|
394 }
|
Chris@0
|
395
|
Chris@0
|
396 /**
|
Chris@0
|
397 * {@inheritdoc}
|
Chris@0
|
398 */
|
Chris@0
|
399 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
|
Chris@0
|
400 parent::postLoad($storage, $entities);
|
Chris@0
|
401 foreach ($entities as $entity) {
|
Chris@0
|
402 $entity->mergeDefaultDisplaysOptions();
|
Chris@0
|
403 }
|
Chris@0
|
404 }
|
Chris@0
|
405
|
Chris@0
|
406 /**
|
Chris@0
|
407 * {@inheritdoc}
|
Chris@0
|
408 */
|
Chris@0
|
409 public static function preCreate(EntityStorageInterface $storage, array &$values) {
|
Chris@0
|
410 parent::preCreate($storage, $values);
|
Chris@0
|
411
|
Chris@0
|
412 // If there is no information about displays available add at least the
|
Chris@0
|
413 // default display.
|
Chris@0
|
414 $values += [
|
Chris@0
|
415 'display' => [
|
Chris@0
|
416 'default' => [
|
Chris@0
|
417 'display_plugin' => 'default',
|
Chris@0
|
418 'id' => 'default',
|
Chris@0
|
419 'display_title' => 'Master',
|
Chris@0
|
420 'position' => 0,
|
Chris@0
|
421 'display_options' => [],
|
Chris@0
|
422 ],
|
Chris@0
|
423 ]
|
Chris@0
|
424 ];
|
Chris@0
|
425 }
|
Chris@0
|
426
|
Chris@0
|
427 /**
|
Chris@0
|
428 * {@inheritdoc}
|
Chris@0
|
429 */
|
Chris@0
|
430 public function postCreate(EntityStorageInterface $storage) {
|
Chris@0
|
431 parent::postCreate($storage);
|
Chris@0
|
432
|
Chris@0
|
433 $this->mergeDefaultDisplaysOptions();
|
Chris@0
|
434 }
|
Chris@0
|
435
|
Chris@0
|
436 /**
|
Chris@0
|
437 * {@inheritdoc}
|
Chris@0
|
438 */
|
Chris@0
|
439 public static function preDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
440 parent::preDelete($storage, $entities);
|
Chris@0
|
441
|
Chris@0
|
442 // Call the remove() hook on the individual displays.
|
Chris@0
|
443 /** @var \Drupal\views\ViewEntityInterface $entity */
|
Chris@0
|
444 foreach ($entities as $entity) {
|
Chris@0
|
445 $executable = Views::executableFactory()->get($entity);
|
Chris@0
|
446 foreach ($entity->get('display') as $display_id => $display) {
|
Chris@0
|
447 $executable->setDisplay($display_id);
|
Chris@0
|
448 $executable->getDisplay()->remove();
|
Chris@0
|
449 }
|
Chris@0
|
450 }
|
Chris@0
|
451 }
|
Chris@0
|
452
|
Chris@0
|
453 /**
|
Chris@0
|
454 * {@inheritdoc}
|
Chris@0
|
455 */
|
Chris@0
|
456 public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
457 parent::postDelete($storage, $entities);
|
Chris@0
|
458
|
Chris@0
|
459 $tempstore = \Drupal::service('user.shared_tempstore')->get('views');
|
Chris@0
|
460 foreach ($entities as $entity) {
|
Chris@0
|
461 $tempstore->delete($entity->id());
|
Chris@0
|
462 }
|
Chris@0
|
463 }
|
Chris@0
|
464
|
Chris@0
|
465 /**
|
Chris@0
|
466 * {@inheritdoc}
|
Chris@0
|
467 */
|
Chris@0
|
468 public function mergeDefaultDisplaysOptions() {
|
Chris@0
|
469 $displays = [];
|
Chris@0
|
470 foreach ($this->get('display') as $key => $options) {
|
Chris@0
|
471 $options += [
|
Chris@0
|
472 'display_options' => [],
|
Chris@0
|
473 'display_plugin' => NULL,
|
Chris@0
|
474 'id' => NULL,
|
Chris@0
|
475 'display_title' => '',
|
Chris@0
|
476 'position' => NULL,
|
Chris@0
|
477 ];
|
Chris@0
|
478 // Add the defaults for the display.
|
Chris@0
|
479 $displays[$key] = $options;
|
Chris@0
|
480 }
|
Chris@0
|
481 $this->set('display', $displays);
|
Chris@0
|
482 }
|
Chris@0
|
483
|
Chris@0
|
484 /**
|
Chris@0
|
485 * {@inheritdoc}
|
Chris@0
|
486 */
|
Chris@0
|
487 public function isInstallable() {
|
Chris@0
|
488 $table_definition = \Drupal::service('views.views_data')->get($this->base_table);
|
Chris@0
|
489 // Check whether the base table definition exists and contains a base table
|
Chris@0
|
490 // definition. For example, taxonomy_views_data_alter() defines
|
Chris@0
|
491 // node_field_data even if it doesn't exist as a base table.
|
Chris@0
|
492 return $table_definition && isset($table_definition['table']['base']);
|
Chris@0
|
493 }
|
Chris@0
|
494
|
Chris@0
|
495 /**
|
Chris@0
|
496 * {@inheritdoc}
|
Chris@0
|
497 */
|
Chris@0
|
498 public function __sleep() {
|
Chris@0
|
499 $keys = parent::__sleep();
|
Chris@0
|
500 unset($keys[array_search('executable', $keys)]);
|
Chris@0
|
501 return $keys;
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 /**
|
Chris@0
|
505 * Invalidates cache tags.
|
Chris@0
|
506 */
|
Chris@0
|
507 public function invalidateCaches() {
|
Chris@0
|
508 // Invalidate cache tags for cached rows.
|
Chris@0
|
509 $tags = $this->getCacheTags();
|
Chris@0
|
510 \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
|
Chris@0
|
511 }
|
Chris@0
|
512
|
Chris@0
|
513 /**
|
Chris@0
|
514 * {@inheritdoc}
|
Chris@0
|
515 */
|
Chris@0
|
516 public function onDependencyRemoval(array $dependencies) {
|
Chris@0
|
517 $changed = FALSE;
|
Chris@0
|
518
|
Chris@0
|
519 // Don't intervene if the views module is removed.
|
Chris@0
|
520 if (isset($dependencies['module']) && in_array('views', $dependencies['module'])) {
|
Chris@0
|
521 return FALSE;
|
Chris@0
|
522 }
|
Chris@0
|
523
|
Chris@0
|
524 // If the base table for the View is provided by a module being removed, we
|
Chris@0
|
525 // delete the View because this is not something that can be fixed manually.
|
Chris@0
|
526 $views_data = Views::viewsData();
|
Chris@0
|
527 $base_table = $this->get('base_table');
|
Chris@0
|
528 $base_table_data = $views_data->get($base_table);
|
Chris@0
|
529 if (!empty($base_table_data['table']['provider']) && in_array($base_table_data['table']['provider'], $dependencies['module'])) {
|
Chris@0
|
530 return FALSE;
|
Chris@0
|
531 }
|
Chris@0
|
532
|
Chris@0
|
533 $current_display = $this->getExecutable()->current_display;
|
Chris@0
|
534 $handler_types = Views::getHandlerTypes();
|
Chris@0
|
535
|
Chris@0
|
536 // Find all the handlers and check whether they want to do something on
|
Chris@0
|
537 // dependency removal.
|
Chris@0
|
538 foreach ($this->display as $display_id => $display_plugin_base) {
|
Chris@0
|
539 $this->getExecutable()->setDisplay($display_id);
|
Chris@0
|
540 $display = $this->getExecutable()->getDisplay();
|
Chris@0
|
541
|
Chris@0
|
542 foreach (array_keys($handler_types) as $handler_type) {
|
Chris@0
|
543 $handlers = $display->getHandlers($handler_type);
|
Chris@0
|
544 foreach ($handlers as $handler_id => $handler) {
|
Chris@0
|
545 if ($handler instanceof DependentWithRemovalPluginInterface) {
|
Chris@0
|
546 if ($handler->onDependencyRemoval($dependencies)) {
|
Chris@0
|
547 // Remove the handler and indicate we made changes.
|
Chris@0
|
548 unset($this->display[$display_id]['display_options'][$handler_types[$handler_type]['plural']][$handler_id]);
|
Chris@0
|
549 $changed = TRUE;
|
Chris@0
|
550 }
|
Chris@0
|
551 }
|
Chris@0
|
552 }
|
Chris@0
|
553 }
|
Chris@0
|
554 }
|
Chris@0
|
555
|
Chris@0
|
556 // Disable the View if we made changes.
|
Chris@0
|
557 // @todo https://www.drupal.org/node/2832558 Give better feedback for
|
Chris@0
|
558 // disabled config.
|
Chris@0
|
559 if ($changed) {
|
Chris@0
|
560 // Force a recalculation of the dependencies if we made changes.
|
Chris@0
|
561 $this->getExecutable()->current_display = NULL;
|
Chris@0
|
562 $this->calculateDependencies();
|
Chris@0
|
563 $this->disable();
|
Chris@0
|
564 }
|
Chris@0
|
565
|
Chris@0
|
566 $this->getExecutable()->setDisplay($current_display);
|
Chris@0
|
567 return $changed;
|
Chris@0
|
568 }
|
Chris@0
|
569
|
Chris@0
|
570 }
|