block_content.post_update.php 2.9 KB

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