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