Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 /**
|
Chris@17
|
4 * @file
|
Chris@17
|
5 * Post update functions for Custom Block.
|
Chris@17
|
6 */
|
Chris@17
|
7
|
Chris@17
|
8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
|
Chris@17
|
9 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
|
Chris@17
|
10
|
Chris@17
|
11 /**
|
Chris@17
|
12 * Adds a 'reusable' filter to all Custom Block views.
|
Chris@17
|
13 */
|
Chris@17
|
14 function block_content_post_update_add_views_reusable_filter(&$sandbox = NULL) {
|
Chris@17
|
15 $entity_type = \Drupal::entityTypeManager()->getDefinition('block_content');
|
Chris@17
|
16 $storage = \Drupal::entityTypeManager()->getStorage('block_content');
|
Chris@17
|
17
|
Chris@17
|
18 // If the storage class is an instance SqlContentEntityStorage we can use it
|
Chris@17
|
19 // to determine the table to use, otherwise we have to get the table from the
|
Chris@17
|
20 // entity type.
|
Chris@17
|
21 if ($storage instanceof SqlContentEntityStorage) {
|
Chris@17
|
22 $table = $entity_type->isTranslatable() ? $storage->getDataTable() : $storage->getBaseTable();
|
Chris@17
|
23 }
|
Chris@17
|
24 else {
|
Chris@17
|
25 $table = $entity_type->isTranslatable() ? $entity_type->getDataTable() : $entity_type->getBaseTable();
|
Chris@17
|
26 }
|
Chris@17
|
27 // If we were not able to get a table name we can not update the views.
|
Chris@17
|
28 if (empty($table)) {
|
Chris@17
|
29 return;
|
Chris@17
|
30 }
|
Chris@17
|
31
|
Chris@17
|
32 \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function ($view) use ($table) {
|
Chris@17
|
33 /** @var \Drupal\views\ViewEntityInterface $view */
|
Chris@17
|
34 if ($view->get('base_table') !== $table) {
|
Chris@17
|
35 return FALSE;
|
Chris@17
|
36 }
|
Chris@17
|
37 $save_view = FALSE;
|
Chris@17
|
38 $displays = $view->get('display');
|
Chris@17
|
39 foreach ($displays as $display_name => &$display) {
|
Chris@17
|
40 // Update the default display and displays that have overridden filters.
|
Chris@17
|
41 if (!isset($display['display_options']['filters']['reusable']) &&
|
Chris@17
|
42 ($display_name === 'default' || isset($display['display_options']['filters']))) {
|
Chris@17
|
43 $display['display_options']['filters']['reusable'] = [
|
Chris@17
|
44 'id' => 'reusable',
|
Chris@17
|
45 'table' => $table,
|
Chris@17
|
46 'field' => 'reusable',
|
Chris@17
|
47 'relationship' => 'none',
|
Chris@17
|
48 'group_type' => 'group',
|
Chris@17
|
49 'admin_label' => '',
|
Chris@17
|
50 'operator' => '=',
|
Chris@17
|
51 'value' => '1',
|
Chris@17
|
52 'group' => 1,
|
Chris@17
|
53 'exposed' => FALSE,
|
Chris@17
|
54 'expose' => [
|
Chris@17
|
55 'operator_id' => '',
|
Chris@17
|
56 'label' => '',
|
Chris@17
|
57 'description' => '',
|
Chris@17
|
58 'use_operator' => FALSE,
|
Chris@17
|
59 'operator' => '',
|
Chris@17
|
60 'identifier' => '',
|
Chris@17
|
61 'required' => FALSE,
|
Chris@17
|
62 'remember' => FALSE,
|
Chris@17
|
63 'multiple' => FALSE,
|
Chris@17
|
64 ],
|
Chris@17
|
65 'is_grouped' => FALSE,
|
Chris@17
|
66 'group_info' => [
|
Chris@17
|
67 'label' => '',
|
Chris@17
|
68 'description' => '',
|
Chris@17
|
69 'identifier' => '',
|
Chris@17
|
70 'optional' => TRUE,
|
Chris@17
|
71 'widget' => 'select',
|
Chris@17
|
72 'multiple' => FALSE,
|
Chris@17
|
73 'remember' => FALSE,
|
Chris@17
|
74 'default_group' => 'All',
|
Chris@17
|
75 'default_group_multiple' => [],
|
Chris@17
|
76 'group_items' => [],
|
Chris@17
|
77 ],
|
Chris@17
|
78 'entity_type' => 'block_content',
|
Chris@17
|
79 'entity_field' => 'reusable',
|
Chris@17
|
80 'plugin_id' => 'boolean',
|
Chris@17
|
81 ];
|
Chris@17
|
82 $save_view = TRUE;
|
Chris@17
|
83 }
|
Chris@17
|
84 }
|
Chris@17
|
85 if ($save_view) {
|
Chris@17
|
86 $view->set('display', $displays);
|
Chris@17
|
87 }
|
Chris@17
|
88 return $save_view;
|
Chris@17
|
89 });
|
Chris@17
|
90 }
|