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/aggregator/src/Tests/AggregatorTestBase.php b/core/modules/aggregator/src/Tests/AggregatorTestBase.php index 15be144..a889905 100644 --- a/core/modules/aggregator/src/Tests/AggregatorTestBase.php +++ b/core/modules/aggregator/src/Tests/AggregatorTestBase.php @@ -147,7 +147,7 @@ public function getFeedEditObject($feed_url = NULL, array $values = array()) { */ public function getDefaultFeedItemCount() { // Our tests are based off of rss.xml, so let's find out how many elements should be related. - $feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField(); + $feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status__value = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField(); return $feed_count > 10 ? 10 : $feed_count; } 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/CommentViewsData.php b/core/modules/comment/src/CommentViewsData.php index 2d9b021..4fde17b 100644 --- a/core/modules/comment/src/CommentViewsData.php +++ b/core/modules/comment/src/CommentViewsData.php @@ -146,10 +146,10 @@ public function getViewsData() { ), ); - $data['comment_field_data']['status']['title'] = $this->t('Approved status'); - $data['comment_field_data']['status']['help'] = $this->t('Whether the comment is approved (or still in the moderation queue).'); - $data['comment_field_data']['status']['filter']['label'] = $this->t('Approved comment status'); - $data['comment_field_data']['status']['filter']['type'] = 'yes-no'; + $data['comment_field_data']['status__value']['title'] = $this->t('Approved status'); + $data['comment_field_data']['status__value']['help'] = $this->t('Whether the comment is approved (or still in the moderation queue).'); + $data['comment_field_data']['status__value']['filter']['label'] = $this->t('Approved comment status'); + $data['comment_field_data']['status__value']['filter']['type'] = 'yes-no'; $data['comment']['approve_comment'] = array( 'field' => array( 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/forum/src/ForumManager.php b/core/modules/forum/src/ForumManager.php index 9fe49a8..a6bf530 100644 --- a/core/modules/forum/src/ForumManager.php +++ b/core/modules/forum/src/ForumManager.php @@ -341,7 +341,7 @@ protected function getLastPost($tid) { $topic = $query ->fields('ces', array('last_comment_timestamp', 'last_comment_uid')) - ->condition('n.status', 1) + ->condition('n.status__value', 1) ->orderBy('last_comment_timestamp', 'DESC') ->range(0, 1) ->addTag('node_access') @@ -379,7 +379,7 @@ protected function getForumStatistics($tid) { $query->addExpression('SUM(ces.comment_count)', 'comment_count'); $this->forumStatistics = $query ->fields('f', array('tid')) - ->condition('n.status', 1) + ->condition('n.status__status', 1) ->condition('n.default_langcode', 1) ->groupBy('tid') ->addTag('node_access') 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/NodeViewsData.php b/core/modules/node/src/NodeViewsData.php index 21e1e44..6526fa0 100644 --- a/core/modules/node/src/NodeViewsData.php +++ b/core/modules/node/src/NodeViewsData.php @@ -34,10 +34,10 @@ public function getViewsData() { $data['node_field_data']['langcode']['help'] = $this->t('The language of the content or translation.'); - $data['node_field_data']['status']['filter']['label'] = $this->t('Published status'); - $data['node_field_data']['status']['filter']['type'] = 'yes-no'; + $data['node_field_data']['status__value']['filter']['label'] = $this->t('Published status'); + $data['node_field_data']['status__value']['filter']['type'] = 'yes-no'; // Use status = 1 instead of status <> 0 in WHERE statement. - $data['node_field_data']['status']['filter']['use_equal'] = TRUE; + $data['node_field_data']['status__value']['filter']['use_equal'] = TRUE; $data['node_field_data']['status_extra'] = array( 'title' => $this->t('Published status or admin user'), diff --git a/core/modules/node/src/Plugin/Search/NodeSearch.php b/core/modules/node/src/Plugin/Search/NodeSearch.php index d161a0a..ecfe513 100644 --- a/core/modules/node/src/Plugin/Search/NodeSearch.php +++ b/core/modules/node/src/Plugin/Search/NodeSearch.php @@ -229,7 +229,7 @@ protected function findResults() { ->extend('Drupal\search\SearchQuery') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); $query->join('node_field_data', 'n', 'n.nid = i.sid AND n.langcode = i.langcode'); - $query->condition('n.status', 1) + $query->condition('n.status__value', 1) ->addTag('node_access') ->searchExpression($keys, $this->getPluginId()); 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/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index 922ed57..870a460 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -103,7 +103,7 @@ function statistics_title_list($dbfield, $dbrows) { ->fields('n', array('nid', 'title')) ->fields('u', array('uid', 'name')) ->condition($dbfield, 0, '<>') - ->condition('n.status', 1) + ->condition('n.status__value', 1) // @todo This should be actually filtering on the desired node status // field language and just fall back to the default language. ->condition('n.default_langcode', 1) diff --git a/core/modules/views/src/EntityViewsData.php b/core/modules/views/src/EntityViewsData.php index 61def5a..9fc11d0 100644 --- a/core/modules/views/src/EntityViewsData.php +++ b/core/modules/views/src/EntityViewsData.php @@ -459,6 +459,13 @@ protected function mapSingleFieldViewsData($table, $field_name, $field_type, $co $views_field['sort']['id'] = 'standard'; break; + case 'status': + $views_field['field']['id'] = 'field'; + $views_field['argument']['id'] = 'numeric'; + $views_field['filter']['id'] = 'boolean'; + $views_field['sort']['id'] = 'standard'; + break; + case 'text': case 'text_with_summary': // Treat these three long text fields the same. 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); + + } + +}