diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 4279cde..2f9e49e 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,21 @@ 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() { + $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); + + \Drupal::database()->update('block_content_field_data')->fields(['status' => 1])->execute(); + \Drupal::database()->update('block_content_field_revision')->fields(['status' => 1])->execute(); +} diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 13af342..e7df512 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; @@ -63,6 +64,7 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface { use EntityChangedTrait; + use EntityPublishedTrait; /** * The theme the block is being created in. @@ -153,6 +155,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..8d55658 100644 --- a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php +++ b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php @@ -158,7 +158,11 @@ protected function blockAccess(AccountInterface $account) { */ public function build() { if ($block = $this->getEntity()) { - return $this->entityManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']); + if ($block->isPublished()) { + return $this->entityManager + ->getViewBuilder($block->getEntityTypeId()) + ->view($block, $this->configuration['view_mode']); + } } else { return array( 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..f6e4c4d --- /dev/null +++ b/core/modules/block_content/tests/src/Functional/UnpublishedBlockTest.php @@ -0,0 +1,48 @@ + 'basic']); + $block_content->save(); + + $this->placeBlock('block_content:' . $block_content->uuid()); + + $this->drupalGet(''); + $page = $this->getSession()->getPage(); + $html = $page->getHtml(); + $this->assertTrue($page->has('css', '.block-block-content' . $block_content->uuid())); + + $block_content->setPublished(FALSE); + $block_content->save(); + + $this->drupalGet(''); + $page = $this->getSession()->getPage(); + $html = $page->getHtml(); + $this->assertFalse($page->has('css', '.block-block-content' . $block_content->uuid())); + } +}