5 * Post update functions for Custom Block.
8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
9 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
12 * Adds a 'reusable' filter to all Custom Block views.
14 function block_content_post_update_add_views_reusable_filter(&$sandbox = NULL) {
15 $entity_type = \Drupal::entityTypeManager()->getDefinition('block_content');
16 $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
21 if ($storage instanceof SqlContentEntityStorage) {
22 $table = $entity_type->isTranslatable() ? $storage->getDataTable() : $storage->getBaseTable();
25 $table = $entity_type->isTranslatable() ? $entity_type->getDataTable() : $entity_type->getBaseTable();
27 // If we were not able to get a table name we can not update the views.
32 \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function ($view) use ($table) {
33 /** @var \Drupal\views\ViewEntityInterface $view */
34 if ($view->get('base_table') !== $table) {
38 $displays = $view->get('display');
39 foreach ($displays as $display_name => &$display) {
40 // Update the default display and displays that have overridden filters.
41 if (!isset($display['display_options']['filters']['reusable']) &&
42 ($display_name === 'default' || isset($display['display_options']['filters']))) {
43 $display['display_options']['filters']['reusable'] = [
46 'field' => 'reusable',
47 'relationship' => 'none',
48 'group_type' => 'group',
58 'use_operator' => FALSE,
65 'is_grouped' => FALSE,
74 'default_group' => 'All',
75 'default_group_multiple' => [],
78 'entity_type' => 'block_content',
79 'entity_field' => 'reusable',
80 'plugin_id' => 'boolean',
86 $view->set('display', $displays);