diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index b0ca1c2ef9b13d7ae26eef53e25211647e6edc0f..fe4526e41f00749ec3b6ba36285294ef872d532c 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -5,6 +5,7 @@ * Install, update and uninstall functions for the block_content module. */ +use Drupal\block_content\BlockContentStorageSchema; use Drupal\Core\Entity\Form\RevisionDeleteForm; use Drupal\Core\Entity\Form\RevisionRevertForm; use Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider; @@ -50,3 +51,15 @@ function block_content_update_10200() { $definition_update_manager->updateFieldStorageDefinition($field_storage_definition); } } + +/** + * Apply index to reusable column. + */ +function block_content_update_10300(): void { + $manager = \Drupal::entityDefinitionUpdateManager(); + $entity_type = $manager->getEntityType('block_content') + ->setHandlerClass('storage_schema', BlockContentStorageSchema::class); + $manager->updateEntityType($entity_type); + $manager->updateFieldStorageDefinition(\Drupal::service('entity_field.manager') + ->getBaseFieldDefinitions('block_content')['reusable']); +} diff --git a/core/modules/block_content/src/BlockContentStorageSchema.php b/core/modules/block_content/src/BlockContentStorageSchema.php new file mode 100644 index 0000000000000000000000000000000000000000..362a158b2959597233a2cb6597ff7eb123c18407 --- /dev/null +++ b/core/modules/block_content/src/BlockContentStorageSchema.php @@ -0,0 +1,27 @@ +<?php + +namespace Drupal\block_content; + +use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema; +use Drupal\Core\Field\FieldStorageDefinitionInterface; + +/** + * Defines the block content schema handler. + */ +class BlockContentStorageSchema extends SqlContentEntityStorageSchema { + + /** + * {@inheritdoc} + */ + protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping): array { + $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping); + $field_name = $storage_definition->getName(); + + if ($table_name === $this->storage->getDataTable() && $field_name === 'reusable') { + $this->addSharedTableFieldIndex($storage_definition, $schema); + } + + return $schema; + } + +} diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index b94a847ad72fe2456270c2e70b8b6eb4849f1294..81e6bfcdaeaa29bcb24d4bb15a4063f861dab9b6 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -25,6 +25,7 @@ * bundle_label = @Translation("Block type"), * handlers = { * "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage", + * "storage_schema" = "Drupal\block_content\BlockContentStorageSchema", * "access" = "Drupal\block_content\BlockContentAccessControlHandler", * "list_builder" = "Drupal\block_content\BlockContentListBuilder", * "view_builder" = "Drupal\block_content\BlockContentViewBuilder", diff --git a/core/modules/block_content/tests/src/Functional/Update/BlockContentReusableIndexUpdatePathTest.php b/core/modules/block_content/tests/src/Functional/Update/BlockContentReusableIndexUpdatePathTest.php new file mode 100644 index 0000000000000000000000000000000000000000..2f50e34a2b660d9d28016af3e44216066c5074c9 --- /dev/null +++ b/core/modules/block_content/tests/src/Functional/Update/BlockContentReusableIndexUpdatePathTest.php @@ -0,0 +1,34 @@ +<?php + +namespace Drupal\Tests\block_content\Functional\Update; + +use Drupal\Core\Database\Database; +use Drupal\FunctionalTests\Update\UpdatePathTestBase; + +/** + * Tests the upgrade path for Block Content reusable column index. + * + * @group block_content + */ +class BlockContentReusableIndexUpdatePathTest extends UpdatePathTestBase { + + /** + * {@inheritdoc} + */ + protected function setDatabaseDumpFiles(): void { + $this->databaseDumpFiles = [ + __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.bare.standard.php.gz', + ]; + } + + /** + * Tests the upgrade path for Block Content reusable index. + */ + public function testRunUpdates(): void { + $connection = Database::getConnection(); + $this->assertFalse($connection->schema()->indexExists('block_content_field_data', 'block_content_field__reusable'), 'Block Content reusable index not yet added.'); + $this->runUpdates(); + $this->assertTrue($connection->schema()->indexExists('block_content_field_data', 'block_content_field__reusable'), 'Block Content reusable index has been added.'); + } + +}