diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index b5fccbd..8e4c78f 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -332,6 +332,17 @@ public function getRevisionId() { return $this->getEntityKey('revision'); } + public function isArchived() { + return ($this->hasField('archived')) ? $this->get('archived')->value : FALSE; + } + + public function setArchived($archived) { + if ($this->hasField('archived')) { + $this->set('archived', $archived); + } + return $this; + } + /** * {@inheritdoc} */ @@ -1132,6 +1143,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(new TranslatableMarkup('Revision ID')) ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); + + $fields['archived'] = BaseFieldDefinition::create('boolean') + ->setLabel(new TranslatableMarkup('Archive flag')) + ->setRevisionable(TRUE) + ->setRequired(TRUE) + ->setDefaultValue([0 => ['value' => FALSE]]); } if ($entity_type->hasKey('langcode')) { $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language') diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index 391e3444..6ab4d32 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -302,6 +302,7 @@ public function getTableMapping(array $storage_definitions = NULL) { 'revision_timestamp', 'revision_uid', 'revision_log', + 'archived', ), $all_fields); $revisionable = $this->entityType->isRevisionable(); @@ -635,7 +636,7 @@ protected function buildPropertyQuery(QueryInterface $entity_query, array $value * @return \Drupal\Core\Database\Query\Select * A SelectQuery object for loading the entity. */ - protected function buildQuery($ids, $revision_id = FALSE) { + protected function buildQuery($ids, $revision_id = FALSE, $include_archived = FALSE) { $query = $this->database->select($this->entityType->getBaseTable(), 'base'); $query->addTag($this->entityTypeId . '_load_multiple'); @@ -671,6 +672,13 @@ protected function buildQuery($ids, $revision_id = FALSE) { // Compare revision ID of the base and revision table, if equal then this // is the default revision. $query->addExpression('CASE base.' . $this->revisionKey . ' WHEN revision.' . $this->revisionKey . ' THEN 1 ELSE 0 END', 'isDefaultRevision'); + + if (!$include_archived && isset($entity_revision_fields['archived'])) { + $group = $query->orConditionGroup() + ->condition('revision.archived', 0) + ->condition('revision.archived', NULL, 'IS'); + $query->condition($group); + } } $query->fields('base', $entity_fields); diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 4279cde..681017c 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -61,3 +61,16 @@ function block_content_update_8003() { \Drupal::entityDefinitionUpdateManager() ->installFieldStorageDefinition('revision_user', 'block_content', 'block_content', $revision_user); } + +/** + * Add 'archived' field to 'block_content' entities. + */ +function block_content_update_8004() { + $archived = BaseFieldDefinition::create('boolean') + ->setLabel(t('Archive flag')) + ->setRevisionable(TRUE) + ->setRequired(TRUE) + ->setDefaultValue([0 => ['value' => FALSE]]); + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('archived', 'block_content', 'block_content', $archived); +} diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentArchiveTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentArchiveTest.php new file mode 100644 index 0000000..484092e --- /dev/null +++ b/core/modules/block_content/tests/src/Kernel/BlockContentArchiveTest.php @@ -0,0 +1,51 @@ +installSchema('system', 'sequences'); + $this->installSchema('user', 'users_data'); + $this->installEntitySchema('user'); + $this->installEntitySchema('block_content'); + } + + /** + * Tests blockContent archive field. + */ + public function testArchiveField() { + BlockContentType::create(['id' => 'test']); + $block_content = BlockContent::create(['type' => 'test', 'title' => 'BlockContentOne']); + $block_content->save(); + $this->assertFalse($block_content->isArchived()); + + $loaded_block_content = BlockContent::load($block_content->id()); + $this->assertEquals($block_content->uuid(), $loaded_block_content->uuid()); + + $block_content->setArchived(TRUE); + $block_content->save(); + $this->assertTrue($block_content->isArchived()); + + $loaded_archive_block_content = BlockContent::load($block_content->id()); + $this->assertNull($loaded_archive_block_content); + } +} diff --git a/core/modules/node/node.install b/core/modules/node/node.install index c754880..5fb8cd6 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -218,3 +218,16 @@ function node_update_8003() { $manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition($field_name, 'node')); } } + +/** + * Add 'archived' field to 'node' entities. + */ +function node_update_8004() { + $archived = BaseFieldDefinition::create('boolean') + ->setLabel(t('Archive flag')) + ->setRevisionable(TRUE) + ->setRequired(TRUE) + ->setDefaultValue([0 => ['value' => FALSE]]); + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('archived', 'node', 'node', $archived); +} \ No newline at end of file diff --git a/core/modules/node/tests/src/Kernel/NodeArchiveTest.php b/core/modules/node/tests/src/Kernel/NodeArchiveTest.php new file mode 100644 index 0000000..ff78ba2 --- /dev/null +++ b/core/modules/node/tests/src/Kernel/NodeArchiveTest.php @@ -0,0 +1,51 @@ +installSchema('system', 'sequences'); + $this->installSchema('user', 'users_data'); + $this->installSchema('node', 'node_access'); + $this->installEntitySchema('user'); + $this->installEntitySchema('node'); + } + + /** + * Tests node archive field. + */ + public function testArchiveField() { + NodeType::create(['id' => 'test']); + $node = Node::create(['type' => 'test', 'title' => 'NodeOne']); + $node->save(); + $this->assertFalse($node->isArchived()); + + $loaded_node = Node::load($node->id()); + $this->assertEquals($node->uuid(), $loaded_node->uuid()); + + $node->setArchived(TRUE); + $node->save(); + $this->assertTrue($node->isArchived()); + + $loaded_archive_node = Node::load($node->id()); + $this->assertNull($loaded_archive_node); + } +} diff --git a/core/modules/serialization/src/Tests/EntitySerializationTest.php b/core/modules/serialization/src/Tests/EntitySerializationTest.php index 00ea14f..aa463ef 100644 --- a/core/modules/serialization/src/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/src/Tests/EntitySerializationTest.php @@ -119,6 +119,9 @@ public function testNormalize() { 'revision_id' => array( array('value' => 1), ), + 'archived' => array( + array('value' => FALSE) + ), 'default_langcode' => array( array('value' => TRUE), ), @@ -190,6 +193,7 @@ public function testSerialize() { 'created' => '' . $this->entity->created->value . '', 'user_id' => '' . $this->user->id() . '' . $this->user->getEntityTypeId() . '' . $this->user->uuid() . '' . $this->user->url() . '', 'revision_id' => '' . $this->entity->getRevisionId() . '', + 'archived' => '0', 'default_langcode' => '1', 'non_rev_field' => '', 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '', diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index 23cf034..c92093c 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -102,6 +102,7 @@ public function testEntityTypeUpdateWithoutData() { $expected = array( 'entity_test_update' => array( t('The %entity_type entity type needs to be updated.', ['%entity_type' => $this->entityManager->getDefinition('entity_test_update')->getLabel()]), + t('The %field_name field needs to be installed.', ['%field_name' => $this->entityManager->getFieldDefinitions('entity_test_update', NULL)['archived']->getLabel()]), ), ); $this->assertEqual($this->entityDefinitionUpdateManager->getChangeSummary(), $expected); //, 'EntityDefinitionUpdateManager reports the expected change summary.');