diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 4e2ecaf..24fc9d8 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,25 +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'); + $manager->updateFieldStorageDefinition($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..8c60f32 100644 --- a/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php +++ b/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php @@ -2,6 +2,8 @@ namespace Drupal\block_content\Tests\Update; +use Drupal\block_content\Entity\BlockContent; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\system\Tests\Update\UpdatePathTestBase; /** @@ -26,16 +28,46 @@ protected function setDatabaseDumpFiles() { * @see block_content_update_8300() */ public function testStatusField() { + $storage_definition = BaseFieldDefinition::create('boolean') + ->setLabel(t('Translation status')) + ->setDescription(t('A boolean indicating whether the translation is visible to non-translators.')) + ->setDefaultValue(TRUE) + ->setRevisionable(TRUE) + ->setTranslatable(TRUE); + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('content_translation_status', 'block_content', 'block_content', $storage_definition); + + $schema = \Drupal::database()->schema(); + $spec = ['type' => 'int', 'not null' => FALSE]; + $schema->addField('block_content_field_revision', 'content_translation_status', $spec); + $schema->addField('block_content_field_data', 'content_translation_status', $spec); + // Check that the 'status' field does not exist prior to the update. - $field = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('status', 'block_content'); + $field = \Drupal::entityDefinitionUpdateManager() + ->getFieldStorageDefinition('status', 'block_content'); $this->assertNull($field); + $content_translation_status = \Drupal::entityDefinitionUpdateManager() + ->getFieldStorageDefinition('content_translation_status', 'block_content'); + $this->assertEqual('Translation status', $content_translation_status->getLabel()); + + $this->assertTrue($schema->fieldExists('block_content_field_revision', 'content_translation_status')); + $this->assertTrue($schema->fieldExists('block_content_field_data', 'content_translation_status')); + // Run updates. $this->runUpdates(); // Check that the field exists and has the correct label. - $updated_field = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('status', 'block_content'); + $updated_field = \Drupal::entityDefinitionUpdateManager() + ->getFieldStorageDefinition('status', 'block_content'); $this->assertEqual('Publishing status', $updated_field->getLabel()); + + $content_translation_status = \Drupal::entityDefinitionUpdateManager() + ->getFieldStorageDefinition('content_translation_status', 'block_content'); + $this->assertNull($content_translation_status); + + $this->assertFalse($schema->fieldExists('block_content_field_revision', 'content_translation_status')); + $this->assertFalse($schema->fieldExists('block_content_field_data', 'content_translation_status')); } }