diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 4e2ecaf..8dd925f 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -74,21 +74,24 @@ function block_content_update_8003() { * Add and populate publishing status fields. */ function block_content_update_8300() { - $db = \Drupal::database(); - $db_schema = $db->schema(); - if ($db_schema->fieldExists('block_content_field_data', 'content_translation_status')) { - $db->update('block_content_field_data', 'bcfd') + $database = \Drupal::database(); + $schema = $database->schema(); + // Content Translation adds the content_translation_status field, to use this + // as an initial_from_field it can't be null and needs to be set to 1. + if ($schema->fieldExists('block_content_field_data', 'content_translation_status')) { + $database->update('block_content_field_data') ->fields(['content_translation_status' => 1]) ->isNull('content_translation_status') ->execute(); } - if ($db_schema->fieldExists('block_content_field_revision', 'content_translation_status')) { - $db->update('block_content_field_revision', 'bcfd') + if ($schema->fieldExists('block_content_field_revision', 'content_translation_status')) { + $database->update('block_content_field_revision') ->fields(['content_translation_status' => 1]) ->isNull('content_translation_status') ->execute(); } + // Add the published entity key to the block_content entity type. $manager = \Drupal::entityDefinitionUpdateManager(); $entity_type = $manager->getEntityType('block_content'); $entity_keys = $entity_type->getKeys(); @@ -97,26 +100,32 @@ function block_content_update_8300() { $entity_type->setHandlerClass('storage_schema', BlockContentStorageSchema::class); $manager->updateEntityType($entity_type); + // Install the publishing status field to the block_content entity type. $status = BaseFieldDefinition::create('boolean') ->setLabel(new TranslatableMarkup('Publishing status')) ->setDescription(new TranslatableMarkup('A boolean indicating the published state.')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) ->setDefaultValue(TRUE); - $manager->installFieldStorageDefinition('status', 'block_content', 'block_content', $status); + // Empty the content_translation_status field before uninstalling. + if ($schema->fieldExists('block_content_field_data', 'content_translation_status')) { + $database->update('block_content_field_data') + ->fields(['content_translation_status' => NULL]) + ->execute(); + } + if ($schema->fieldExists('block_content_field_revision', 'content_translation_status')) { + $database->update('block_content_field_revision') + ->fields(['content_translation_status' => NULL]) + ->execute(); + } + + // Uninstall the content_translation_status field if it existed. $content_translation_status = $manager->getFieldStorageDefinition('content_translation_status', 'block_content'); if ($content_translation_status instanceof FieldStorageDefinitionInterface) { $manager->uninstallFieldStorageDefinition($content_translation_status); } - - if ($db_schema->fieldExists('block_content_field_data', 'content_translation_status')) { - $db_schema->dropField('block_content_field_data', 'content_translation_status'); - } - if ($db_schema->fieldExists('block_content_field_revision', 'content_translation_status')) { - $db_schema->dropField('block_content_field_revision', 'content_translation_status'); - } } /** diff --git a/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php b/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php index 8c5bc0d..8ac9da9 100644 --- a/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php +++ b/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php @@ -17,6 +17,7 @@ class BlockContentUpdateTest extends UpdatePathTestBase { protected function setDatabaseDumpFiles() { $this->databaseDumpFiles = [ __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8-rc1.filled.standard.php.gz', + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.block-content-publishing-status-upgrade.php', ]; } diff --git a/core/modules/system/tests/fixtures/update/drupal-8.block-content-publishing-status-upgrade.php b/core/modules/system/tests/fixtures/update/drupal-8.block-content-publishing-status-upgrade.php index e69de29..e2e01d5 100644 --- a/core/modules/system/tests/fixtures/update/drupal-8.block-content-publishing-status-upgrade.php +++ b/core/modules/system/tests/fixtures/update/drupal-8.block-content-publishing-status-upgrade.php @@ -0,0 +1,17 @@ + 'int', + 'not null' => FALSE, +]; +$connection->schema()->addField('block_content_field_revision', 'content_translation_status', $spec); +$connection->schema()->addField('block_content_field_data', 'content_translation_status', $spec);