block_content.post_update.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // If Views is not installed, there is nothing to do.
  13. if (!\Drupal::moduleHandler()->moduleExists('views')) {
  14. return;
  15. }
  16. $entity_type = \Drupal::entityTypeManager()->getDefinition('block_content');
  17. $storage = \Drupal::entityTypeManager()->getStorage('block_content');
  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. \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function ($view) use ($table) {
  32. /** @var \Drupal\views\ViewEntityInterface $view */
  33. if ($view->get('base_table') !== $table) {
  34. return FALSE;
  35. }
  36. $save_view = FALSE;
  37. $displays = $view->get('display');
  38. foreach ($displays as $display_name => &$display) {
  39. // Update the default display and displays that have overridden filters.
  40. if (!isset($display['display_options']['filters']['reusable']) &&
  41. ($display_name === 'default' || isset($display['display_options']['filters']))) {
  42. $display['display_options']['filters']['reusable'] = [
  43. 'id' => 'reusable',
  44. 'table' => $table,
  45. 'field' => 'reusable',
  46. 'relationship' => 'none',
  47. 'group_type' => 'group',
  48. 'admin_label' => '',
  49. 'operator' => '=',
  50. 'value' => '1',
  51. 'group' => 1,
  52. 'exposed' => FALSE,
  53. 'expose' => [
  54. 'operator_id' => '',
  55. 'label' => '',
  56. 'description' => '',
  57. 'use_operator' => FALSE,
  58. 'operator' => '',
  59. 'identifier' => '',
  60. 'required' => FALSE,
  61. 'remember' => FALSE,
  62. 'multiple' => FALSE,
  63. ],
  64. 'is_grouped' => FALSE,
  65. 'group_info' => [
  66. 'label' => '',
  67. 'description' => '',
  68. 'identifier' => '',
  69. 'optional' => TRUE,
  70. 'widget' => 'select',
  71. 'multiple' => FALSE,
  72. 'remember' => FALSE,
  73. 'default_group' => 'All',
  74. 'default_group_multiple' => [],
  75. 'group_items' => [],
  76. ],
  77. 'entity_type' => 'block_content',
  78. 'entity_field' => 'reusable',
  79. 'plugin_id' => 'boolean',
  80. ];
  81. $save_view = TRUE;
  82. }
  83. }
  84. if ($save_view) {
  85. $view->set('display', $displays);
  86. }
  87. return $save_view;
  88. });
  89. }