Commit d2af1169 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2789315 by amateescu, timmillwood, claudiu.cristea, sandervd,...

Issue #2789315 by amateescu, timmillwood, claudiu.cristea, sandervd, GroovyCarrot, catch, Wim Leers, pfrenssen, Berdir, twistor, xjm: Create EntityPublishedInterface and use for Node and Comment
parent c628bd5d
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Entity;

/**
 * Provides an interface for access to an entity's published state.
 */
interface EntityPublishedInterface {

  /**
   * Returns whether or not the entity is published.
   *
   * @return bool
   *   TRUE if the entity is published, FALSE otherwise.
   */
  public function isPublished();

  /**
   * Sets the entity as published.
   *
   * @param bool|null $published
   *   (optional and deprecated) TRUE to set this entity to published, FALSE to
   *   set it to unpublished. Defaults to NULL. This parameter is deprecated in
   *   Drupal 8.3.0 and will be removed before Drupal 9.0.0. Use this method,
   *   without any parameter, to set the entity as published and
   *   setUnpublished() to set the entity as unpublished.
   *
   * @return $this
   *
   * @see \Drupal\Core\Entity\EntityPublishedInterface::setUnpublished()
   */
  public function setPublished($published = NULL);

  /**
   * Sets the entity as unpublished.
   *
   * @return $this
   */
  public function setUnpublished();

}
+39 −22
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Core\Entity;

use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;

@@ -17,11 +18,21 @@ trait EntityPublishedTrait {
   *   The entity type to add the publishing status field to.
   *
   * @return \Drupal\Core\Field\BaseFieldDefinition[]
   *   Array of base field definitions.
   *   An array of base field definitions.
   *
   * @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
   *   Thrown when the entity type does not implement EntityPublishedInterface
   *   or if it does not have a "published" entity key.
   */
  public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity_type) {
    $key = $entity_type->hasKey('status') ? $entity_type->getKey('status') : 'status';
    return [$key => BaseFieldDefinition::create('boolean')
    if (!is_subclass_of($entity_type->getClass(), EntityPublishedInterface::class)) {
      throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\Core\Entity\EntityPublishedInterface.');
    }
    if (!$entity_type->hasKey('published')) {
      throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have a "published" entity key.');
    }

    return [$entity_type->getKey('published') => BaseFieldDefinition::create('boolean')
      ->setLabel(new TranslatableMarkup('Publishing status'))
      ->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
      ->setRevisionable(TRUE)
@@ -30,31 +41,37 @@ public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity
  }

  /**
   * Returns the published status of the entity.
   *
   * @return bool
   *   The published status of the entity.
   * {@inheritdoc}
   */
  public function isPublished() {
    $status = $this->getEntityKey('status');
    return (bool) (isset($status) ? $status : $this->get('status')->value);
    $key = $this->getEntityType()->getKey('published');
    return (bool) $this->get($key)->value;
  }

  /**
   * Sets the entity as published or not published.
   *
   * @param bool $published
   *   A boolean value denoting the published status.
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface $this
   *   The Content Entity object.
   * {@inheritdoc}
   */
  public function setPublished($published = NULL) {
    if ($published !== NULL) {
      @trigger_error('The $published parameter is deprecated since version 8.3.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
      $value = (bool) $published;
    }
    else {
      $value = TRUE;
    }
    $key = $this->getEntityType()->getKey('published');
    $this->set($key, $value);

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setPublished($published) {
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
    $key = $this->getEntityType()->getKey('status') ?: 'status';
    // @todo: Replace values with constants from EntityPublishedInterface or
    // similar when introduced. https://www.drupal.org/node/2811667
    $this->set($key, $published ? 1 : 0);
  public function setUnpublished() {
    $key = $this->getEntityType()->getKey('published');
    $this->set($key, FALSE);

    return $this;
  }

+8 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Entity\Exception;

/**
 * Defines an exception thrown when an entity type definition is invalid.
 */
class UnsupportedEntityTypeDefinitionException extends \Exception { }
+12 −0
Original line number Diff line number Diff line
@@ -193,6 +193,18 @@ function comment_update_8300() {
  \Drupal::service('entity.definition_update_manager')->updateFieldStorageDefinition($field_definitions['status']);
}

/**
 * Set the 'published' entity key.
 */
function comment_update_8301() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $definition_update_manager->getEntityType('comment');
  $keys = $entity_type->getKeys();
  $keys['published'] = 'status';
  $entity_type->set('entity_keys', $keys);
  $definition_update_manager->updateEntityType($entity_type);
}

/**
 * @} End of "addtogroup updates-8.3.x".
 */
+5 −20
Original line number Diff line number Diff line
@@ -3,13 +3,14 @@
namespace Drupal\comment;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\Core\Entity\EntityChangedInterface;

/**
 * Provides an interface defining a comment entity.
 */
interface CommentInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
interface CommentInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface, EntityPublishedInterface {

  /**
   * Comment is awaiting approval.
@@ -191,32 +192,16 @@ public function getCreatedTime();
   */
  public function setCreatedTime($created);

  /**
   * Checks if the comment is published.
   *
   * @return bool
   *   TRUE if the comment is published.
   */
  public function isPublished();

  /**
   * Returns the comment's status.
   *
   * @return int
   *   One of CommentInterface::PUBLISHED or CommentInterface::NOT_PUBLISHED
   */
  public function getStatus();

  /**
   * Sets the published status of the comment entity.
   *
   * @param bool $status
   *   Set to TRUE to publish the comment, FALSE to unpublish.
   *
   * @return \Drupal\comment\CommentInterface
   *   The class instance that this method is called on.
   * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use
   *   \Drupal\Core\Entity\EntityPublishedInterface::isPublished() instead.
   */
  public function setPublished($status);
  public function getStatus();

  /**
   * Returns the alphadecimal representation of the comment's place in a thread.
Loading