diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 4279cde..f3441ec 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Add 'revision_translation_affected' field to 'block_content' entities. @@ -61,3 +62,26 @@ function block_content_update_8003() { \Drupal::entityDefinitionUpdateManager() ->installFieldStorageDefinition('revision_user', 'block_content', 'block_content', $revision_user); } + +/** + * Add and populate publishing status fields. + */ +function block_content_update_8004() { + $entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('block_content'); + $entity_keys = $entity_type->getKeys(); + $entity_keys['published'] = 'status'; + $entity_type->set('entity_keys', $entity_keys); + $entity_type->setHandlerClass('storage_schema', 'Drupal\block_content\BlockContentStorageSchema'); + \Drupal::entityDefinitionUpdateManager()->updateEntityType($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); + + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('status', 'block_content', 'block_content', $status); + +} diff --git a/core/modules/block_content/src/BlockContentInterface.php b/core/modules/block_content/src/BlockContentInterface.php index 130cae1..75fdc59 100644 --- a/core/modules/block_content/src/BlockContentInterface.php +++ b/core/modules/block_content/src/BlockContentInterface.php @@ -4,12 +4,13 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityChangedInterface; +use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Entity\RevisionLogInterface; /** * Provides an interface defining a custom block entity. */ -interface BlockContentInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface { +interface BlockContentInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface, EntityPublishedInterface { /** * Returns the block revision log message. diff --git a/core/modules/block_content/src/BlockContentStorageSchema.php b/core/modules/block_content/src/BlockContentStorageSchema.php new file mode 100644 index 0000000..4d10586 --- /dev/null +++ b/core/modules/block_content/src/BlockContentStorageSchema.php @@ -0,0 +1,41 @@ +getName() == 'status') { + $schema['fields']['status']['initial'] = 1; + } + + return $schema; + } + + /** + * {@inheritdoc} + */ + protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) { + $schema = parent::getEntitySchema($entity_type, $reset); + + $schema['block_content_field_data']['indexes'] += [ + 'block_content__status' => ['status'] + ]; + $schema['block_content_field_revision']['indexes'] += [ + 'block_content__status' => ['status'] + ]; + + return $schema; + } +} \ No newline at end of file diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 13af342..5207c4f 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -4,6 +4,7 @@ use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityChangedTrait; +use Drupal\Core\Entity\EntityPublishedTrait; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; @@ -19,6 +20,7 @@ * bundle_label = @Translation("Custom 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", @@ -48,7 +50,8 @@ * "bundle" = "type", * "label" = "info", * "langcode" = "langcode", - * "uuid" = "uuid" + * "uuid" = "uuid", + * "published" = "status", * }, * bundle_entity_type = "block_content_type", * field_ui_base_route = "entity.block_content_type.edit_form", @@ -63,6 +66,7 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface { use EntityChangedTrait; + use EntityPublishedTrait; /** * The theme the block is being created in. @@ -153,6 +157,7 @@ public function delete() { public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ $fields = parent::baseFieldDefinitions($entity_type); + $fields += static::publishedBaseFieldDefinitions($entity_type); $fields['id']->setLabel(t('Custom block ID')) ->setDescription(t('The custom block ID.')); diff --git a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php index 2afd040..4f3ad57 100644 --- a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php +++ b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php @@ -147,8 +147,8 @@ public function blockSubmit($form, FormStateInterface $form_state) { * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { - if ($this->getEntity()) { - return $this->getEntity()->access('view', $account, TRUE); + if ($block = $this->getEntity()) { + return AccessResult::allowedIf($block->isPublished() && $block->access('view', $account, TRUE)); } return AccessResult::forbidden(); } diff --git a/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php b/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php new file mode 100644 index 0000000..5557b77 --- /dev/null +++ b/core/modules/block_content/src/Tests/Update/BlockContentUpdateTest.php @@ -0,0 +1,41 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8-rc1.bare.standard.php.gz', + ]; + } + + /** + * Tests adding a status field to the block content entuty type. + * + * @see block_content_update_8004() + */ + public function testStatusField() { + // Check that the 'status' field does not exist prior to the update. + $field = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('status', 'block_content'); + $this->assertIdentical(NULL, $field); + + // Run updates. + $this->runUpdates(); + + // Check that the field exists and has the correct label. + $updated_field = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('status', 'block_content'); + $this->assertEqual('Publishing status', $updated_field->getLabel()); + } + +} diff --git a/core/modules/block_content/tests/src/Functional/UnpublishedBlockTest.php b/core/modules/block_content/tests/src/Functional/UnpublishedBlockTest.php new file mode 100644 index 0000000..9aa8091 --- /dev/null +++ b/core/modules/block_content/tests/src/Functional/UnpublishedBlockTest.php @@ -0,0 +1,46 @@ + 'basic']); + $block_content->save(); + + $this->placeBlock('block_content:' . $block_content->uuid()); + + $this->drupalGet(''); + $page = $this->getSession()->getPage(); + $this->assertTrue($page->has('css', '.block-block-content' . $block_content->uuid())); + + $block_content->setPublished(FALSE); + $block_content->save(); + + $this->drupalGet(''); + $page = $this->getSession()->getPage(); + $this->assertFalse($page->has('css', '.block-block-content' . $block_content->uuid())); + } +}