taxonomy.post_update.php 4.1 KB

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