diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 7c0a458..8ce6762 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -5,6 +5,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Entity\Plugin\DataType\EntityReference; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\Field\Plugin\Field\FieldType\StatusItem; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; @@ -815,6 +816,30 @@ public function isNewTranslation() { /** * {@inheritdoc} */ + public function isArchived() { + $status_key = $this->getEntityType()->getKey('status'); + if ($this->hasField($status_key)) { + $status = $this->$status_key->getValue(); + if ($status[0]['state'] == StatusItem::STATUS_ARCHIVED) { + return TRUE; + } + } + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function setArchived($archived) { + $status_key = $this->getEntityType()->getKey('status'); + if ($this->hasField($status_key) && $archived) { + $this->$status_key->state = StatusItem::STATUS_ARCHIVED; + } + } + + /** + * {@inheritdoc} + */ public function addTranslation($langcode, array $values = array()) { // Make sure we do not attempt to create a translation if an invalid // language is specified or the entity cannot be translated. diff --git a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php index 0c3f04b..e257066 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@ -53,4 +53,11 @@ public function setRevisionTranslationAffected($affected); */ public function isRevisionTranslationAffected(); + /** + * Checks whether the entity is archived. + * + * @return bool + * True is the entity is archived, FALSE otherwise. + */ + public function isArchived(); } diff --git a/core/lib/Drupal/Core/Entity/Sql/DefaultTableMapping.php b/core/lib/Drupal/Core/Entity/Sql/DefaultTableMapping.php index 5f20c3f..b8fd1fa 100644 --- a/core/lib/Drupal/Core/Entity/Sql/DefaultTableMapping.php +++ b/core/lib/Drupal/Core/Entity/Sql/DefaultTableMapping.php @@ -201,7 +201,12 @@ public function getFieldColumnName(FieldStorageDefinitionInterface $storage_defi $field_name = $storage_definition->getName(); if ($this->allowsSharedTableStorage($storage_definition)) { - $column_name = count($storage_definition->getColumns()) == 1 ? $field_name : $field_name . '__' . $property_name; + if (count($storage_definition->getColumns()) == 1 || $property_name == 'value') { + $column_name = $field_name; + } + else { + $column_name = $field_name . '__' . $property_name; + } } elseif ($this->requiresDedicatedTableStorage($storage_definition)) { if ($property_name == TableMappingInterface::DELTA) { diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StatusItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StatusItem.php new file mode 100644 index 0000000..19e1f7c --- /dev/null +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StatusItem.php @@ -0,0 +1,86 @@ + 'int', + 'unsigned' => $field_definition->getSetting('unsigned'), + 'size' => $field_definition->getSetting('size'), + ]; + return $schema; + } + + /** + * @inheritDoc + */ + public function onChange($property_name, $notify = TRUE) { + parent::onChange($property_name, $notify); + $property_value = $this->get($property_name)->getValue(); + if ($property_name == 'value') { + if ($property_value == self::STATUS_PUBLISHED) { + $this->writePropertyValue('state', self::STATUS_PUBLISHED); + } + else { + $this->writePropertyValue('state', self::STATUS_UNPUBLISHED); + } + } + elseif ($property_name == 'state') { + if ($property_value == self::STATUS_PUBLISHED) { + $this->writePropertyValue('value', self::STATUS_PUBLISHED); + } + else { + $this->writePropertyValue('value', self::STATUS_UNPUBLISHED); + } + } + } + + /** + * @inheritDoc + */ + public function setValue($values, $notify = TRUE) { + if ($values === 0) { + $values = [ + 'value' => 0, + 'state' => 0, + ]; + } + parent::setValue($values, $notify); + if (is_array($values) && isset($values['value']) && !isset($values['state'])) { + $this->onChange('value', FALSE); + } + elseif (is_array($values) && !isset($values['value']) && isset($values['state'])) { + $this->onChange('state', FALSE); + } + } + + public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { + $properties = parent::propertyDefinitions($field_definition); + + $properties['state'] = DataDefinition::create('integer') + ->setLabel(t('Status state')) + ->setRequired(TRUE); + + return $properties; + } +} diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index b4fd6aa..b3124d0 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\field\Entity\FieldStorageConfig; @@ -179,3 +180,18 @@ function comment_update_8200() { /** * @} End of "addtogroup updates-8.2.x". */ + +/** + * Switch status from boolean to integer. + */ +function comment_update_8300() { + $manager = \Drupal::entityDefinitionUpdateManager(); + $definition = BaseFieldDefinition::create('status') + ->setName('status') + ->setTargetEntityTypeId('comment') + ->setLabel(t('Publishing status')) + ->setDescription(t('An integer indicating the comment status.')) + ->setTranslatable(TRUE) + ->setDefaultValue(1); + $manager->updateFieldStorageDefinition($definition); +} \ No newline at end of file diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 1981c0f..cb42b7a 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -291,11 +291,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDescription(t('The time that the comment was last edited.')) ->setTranslatable(TRUE); - $fields['status'] = BaseFieldDefinition::create('boolean') + $fields['status'] = BaseFieldDefinition::create('status') ->setLabel(t('Publishing status')) - ->setDescription(t('A boolean indicating whether the comment is published.')) + ->setDescription(t('An integer indicating the comment status.')) ->setTranslatable(TRUE) - ->setDefaultValue(TRUE); + ->setDefaultValue(1); $fields['thread'] = BaseFieldDefinition::create('string') ->setLabel(t('Thread place')) diff --git a/core/modules/node/node.install b/core/modules/node/node.install index c754880..191039c 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -218,3 +218,19 @@ function node_update_8003() { $manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition($field_name, 'node')); } } + +/** + * Switch status from boolean to integer. + */ +function node_update_8004() { + $manager = \Drupal::entityDefinitionUpdateManager(); + $definition = BaseFieldDefinition::create('status') + ->setName('status') + ->setTargetEntityTypeId('node') + ->setLabel(t('Publishing status')) + ->setDescription(t('A integer indicating the node status.')) + ->setRevisionable(TRUE) + ->setTranslatable(TRUE) + ->setDefaultValue(1); + $manager->updateFieldStorageDefinition($definition); +} diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index bdb8050..e4555d4 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -408,12 +408,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { )) ->setDisplayConfigurable('form', TRUE); - $fields['status'] = BaseFieldDefinition::create('boolean') + $fields['status'] = BaseFieldDefinition::create('status') ->setLabel(t('Publishing status')) - ->setDescription(t('A boolean indicating whether the node is published.')) + ->setDescription(t('A integer indicating the node status.')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) - ->setDefaultValue(TRUE); + ->setDefaultValue(1); $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Authored on')) diff --git a/core/tests/Drupal/KernelTests/Core/Field/StatusItemFieldTest.php b/core/tests/Drupal/KernelTests/Core/Field/StatusItemFieldTest.php new file mode 100644 index 0000000..dae57d1 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Field/StatusItemFieldTest.php @@ -0,0 +1,56 @@ +installEntitySchema('user'); + $this->installEntitySchema('node'); + + $this->installSchema('node', 'node_access'); + + NodeType::create(['type' => 'example'])->save(); + $node = Node::create([ + 'type' => 'example', + 'title' => 'Please Please Me']); + $node->save(); + + $this->assertEquals(TRUE, $node->isPublished()); + $this->assertEquals(StatusItem::STATUS_PUBLISHED, $node->status->value); + $this->assertEquals(StatusItem::STATUS_PUBLISHED, $node->status->state); + + $node->setArchived(TRUE); + $node->save(); + + $this->assertEquals(FALSE, $node->isPublished()); + $this->assertEquals(StatusItem::STATUS_UNPUBLISHED, $node->status->value); + $this->assertEquals(StatusItem::STATUS_ARCHIVED, $node->status->state); + + $node->setPublished(FALSE); + $node->save(); + + $this->assertEquals(FALSE, $node->isPublished()); + $this->assertEquals(StatusItem::STATUS_UNPUBLISHED, $node->status->value); + $this->assertEquals(StatusItem::STATUS_UNPUBLISHED, $node->status->state); + + } + +}