Pull merge.
[yaffs-website] / web / core / modules / taxonomy / taxonomy.post_update.php
1 <?php
2
3 /**
4  * @file
5  * Post update functions for Taxonomy.
6  */
7
8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
9 use Drupal\views\ViewExecutable;
10
11 /**
12  * Clear caches due to updated taxonomy entity views data.
13  */
14 function taxonomy_post_update_clear_views_data_cache() {
15   // An empty update will flush caches.
16 }
17
18 /**
19  * Clear entity_bundle_field_definitions cache for new parent field settings.
20  */
21 function taxonomy_post_update_clear_entity_bundle_field_definitions_cache() {
22   // An empty update will flush caches.
23 }
24
25 /**
26  * Add a 'published' = TRUE filter for all Taxonomy term views and converts
27  * existing ones that were using the 'content_translation_status' field.
28  */
29 function taxonomy_post_update_handle_publishing_status_addition_in_views(&$sandbox = NULL) {
30   $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
31   $entity_type = $definition_update_manager->getEntityType('taxonomy_term');
32   $published_key = $entity_type->getKey('published');
33
34   $status_filter = [
35     'id' => 'status',
36     'table' => 'taxonomy_term_field_data',
37     'field' => $published_key,
38     'relationship' => 'none',
39     'group_type' => 'group',
40     'admin_label' => '',
41     'operator' => '=',
42     'value' => '1',
43     'group' => 1,
44     'exposed' => FALSE,
45     'expose' => [
46       'operator_id' => '',
47       'label' => '',
48       'description' => '',
49       'use_operator' => FALSE,
50       'operator' => '',
51       'identifier' => '',
52       'required' => FALSE,
53       'remember' => FALSE,
54       'multiple' => FALSE,
55       'remember_roles' => [
56         'authenticated' => 'authenticated',
57         'anonymous' => '0',
58         'administrator' => '0',
59       ],
60     ],
61     'is_grouped' => FALSE,
62     'group_info' => [
63       'label' => '',
64       'description' => '',
65       'identifier' => '',
66       'optional' => TRUE,
67       'widget' => 'select',
68       'multiple' => FALSE,
69       'remember' => FALSE,
70       'default_group' => 'All',
71       'default_group_multiple' => [],
72       'group_items' => [],
73     ],
74     'entity_type' => 'taxonomy_term',
75     'entity_field' => $published_key,
76     'plugin_id' => 'boolean',
77   ];
78
79   \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function ($view) use ($published_key, $status_filter) {
80     /** @var \Drupal\views\ViewEntityInterface $view */
81     // Only alter taxonomy term views.
82     if ($view->get('base_table') !== 'taxonomy_term_field_data') {
83       return FALSE;
84     }
85
86     $displays = $view->get('display');
87     foreach ($displays as $display_name => &$display) {
88       // Update any existing 'content_translation_status fields.
89       $fields = isset($display['display_options']['fields']) ? $display['display_options']['fields'] : [];
90       foreach ($fields as $id => $field) {
91         if (isset($field['field']) && $field['field'] == 'content_translation_status') {
92           $fields[$id]['field'] = $published_key;
93         }
94       }
95       $display['display_options']['fields'] = $fields;
96
97       // Update any existing 'content_translation_status sorts.
98       $sorts = isset($display['display_options']['sorts']) ? $display['display_options']['sorts'] : [];
99       foreach ($sorts as $id => $sort) {
100         if (isset($sort['field']) && $sort['field'] == 'content_translation_status') {
101           $sorts[$id]['field'] = $published_key;
102         }
103       }
104       $display['display_options']['sorts'] = $sorts;
105
106       // Update any existing 'content_translation_status' filters or add a new
107       // one if necessary.
108       $filters = isset($display['display_options']['filters']) ? $display['display_options']['filters'] : [];
109       $has_status_filter = FALSE;
110       foreach ($filters as $id => $filter) {
111         if (isset($filter['field']) && $filter['field'] == 'content_translation_status') {
112           $filters[$id]['field'] = $published_key;
113           $has_status_filter = TRUE;
114         }
115       }
116
117       if (!$has_status_filter) {
118         $status_filter['id'] = ViewExecutable::generateHandlerId($published_key, $filters);
119         $filters[$status_filter['id']] = $status_filter;
120       }
121       $display['display_options']['filters'] = $filters;
122     }
123     $view->set('display', $displays);
124
125     return TRUE;
126   });
127 }