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..42dbd53 100644 --- a/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php +++ b/core/modules/block_content/src/Plugin/Block/BlockContentBlock.php @@ -147,7 +147,8 @@ public function blockSubmit($form, FormStateInterface $form_state) { * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { - if ($this->getEntity()) { + $block = $this->getEntity(); + if ($block->isPublished()) { return $this->getEntity()->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..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())); + } +}