5 * Post update functions for Taxonomy.
8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
9 use Drupal\views\ViewExecutable;
12 * Clear caches due to updated taxonomy entity views data.
14 function taxonomy_post_update_clear_views_data_cache() {
15 // An empty update will flush caches.
19 * Clear entity_bundle_field_definitions cache for new parent field settings.
21 function taxonomy_post_update_clear_entity_bundle_field_definitions_cache() {
22 // An empty update will flush caches.
26 * Add a 'published' = TRUE filter for all Taxonomy term views and converts
27 * existing ones that were using the 'content_translation_status' field.
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');
36 'table' => 'taxonomy_term_field_data',
37 'field' => $published_key,
38 'relationship' => 'none',
39 'group_type' => 'group',
49 'use_operator' => FALSE,
56 'authenticated' => 'authenticated',
58 'administrator' => '0',
61 'is_grouped' => FALSE,
70 'default_group' => 'All',
71 'default_group_multiple' => [],
74 'entity_type' => 'taxonomy_term',
75 'entity_field' => $published_key,
76 'plugin_id' => 'boolean',
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') {
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;
95 $display['display_options']['fields'] = $fields;
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;
104 $display['display_options']['sorts'] = $sorts;
106 // Update any existing 'content_translation_status' filters or add a new
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;
117 if (!$has_status_filter) {
118 $status_filter['id'] = ViewExecutable::generateHandlerId($published_key, $filters);
119 $filters[$status_filter['id']] = $status_filter;
121 $display['display_options']['filters'] = $filters;
123 $view->set('display', $displays);