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/Field/Plugin/Field/FieldType/StatusItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StatusItem.php new file mode 100644 index 0000000..0d7a4a3 --- /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 (!is_array($values)) { + $values = [ + 'value' => $values, + 'state' => $values, + ]; + } + 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/CommentStorageSchema.php b/core/modules/comment/src/CommentStorageSchema.php index ee663c0..07e374f 100644 --- a/core/modules/comment/src/CommentStorageSchema.php +++ b/core/modules/comment/src/CommentStorageSchema.php @@ -18,12 +18,12 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res $schema = parent::getEntitySchema($entity_type, $reset); $schema['comment_field_data']['indexes'] += array( - 'comment__status_pid' => array('pid', 'status'), + 'comment__status_pid' => array('pid', 'status__value'), 'comment__num_new' => array( 'entity_id', 'entity_type', 'comment_type', - 'status', + 'status__value', 'created', 'cid', 'thread', 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/modules/node/src/NodeStorageSchema.php b/core/modules/node/src/NodeStorageSchema.php index 43bd465..61928ff 100644 --- a/core/modules/node/src/NodeStorageSchema.php +++ b/core/modules/node/src/NodeStorageSchema.php @@ -18,8 +18,8 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res $schema = parent::getEntitySchema($entity_type, $reset); $schema['node_field_data']['indexes'] += array( - 'node__frontpage' => array('promote', 'status', 'sticky', 'created'), - 'node__status_type' => array('status', 'type', 'nid'), + 'node__frontpage' => array('promote', 'status__value', 'sticky', 'created'), + 'node__status_type' => array('status__value', 'type', 'nid'), 'node__title_type' => array('title', array('type', 4)), ); @@ -48,7 +48,6 @@ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $st if ($table_name == 'node_field_data') { switch ($field_name) { case 'promote': - case 'status': case 'sticky': case 'title': // Improves the performance of the indexes defined diff --git a/core/modules/node/src/Plugin/views/filter/Status.php b/core/modules/node/src/Plugin/views/filter/Status.php index 194df7a..afaeb0e 100644 --- a/core/modules/node/src/Plugin/views/filter/Status.php +++ b/core/modules/node/src/Plugin/views/filter/Status.php @@ -22,7 +22,7 @@ public function canExpose() { return FALSE; } public function query() { $table = $this->ensureMyTable(); - $this->query->addWhereExpression($this->options['group'], "$table.status = 1 OR ($table.uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_NODES*** = 1) OR ***BYPASS_NODE_ACCESS*** = 1"); + $this->query->addWhereExpression($this->options['group'], "$table.status__value = 1 OR ($table.uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_NODES*** = 1) OR ***BYPASS_NODE_ACCESS*** = 1"); } /** diff --git a/core/modules/node/src/Plugin/views/wizard/Node.php b/core/modules/node/src/Plugin/views/wizard/Node.php index ce267e6..00cf3e3 100644 --- a/core/modules/node/src/Plugin/views/wizard/Node.php +++ b/core/modules/node/src/Plugin/views/wizard/Node.php @@ -32,7 +32,7 @@ class Node extends WizardPluginBase { 'status' => array( 'value' => TRUE, 'table' => 'node_field_data', - 'field' => 'status', + 'field' => 'status__value', 'plugin_id' => 'boolean', 'entity_type' => 'node', 'entity_field' => 'status', diff --git a/core/modules/node/src/Plugin/views/wizard/NodeRevision.php b/core/modules/node/src/Plugin/views/wizard/NodeRevision.php index 64bc7be..27aba73 100644 --- a/core/modules/node/src/Plugin/views/wizard/NodeRevision.php +++ b/core/modules/node/src/Plugin/views/wizard/NodeRevision.php @@ -31,7 +31,7 @@ class NodeRevision extends WizardPluginBase { 'status' => array( 'value' => TRUE, 'table' => 'node_field_revision', - 'field' => 'status', + 'field' => 'status__value', 'plugin_id' => 'boolean', 'entity_type' => 'node', 'entity_field' => 'status', 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); + + } + +}