Skip to content
Permalink
Browse files

Use EntityPublishedTrait (#3991)

* Extends EntityPublishedInterface

* Use EntityPublishedTrait
  • Loading branch information...
el7cosmos authored and LOBsTerr committed Apr 5, 2019
1 parent aeeb3a3 commit 9cff0df03ba52a23052765b2bbe767a981cde499
@@ -18,6 +18,7 @@ use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\ContentEntityBase;
{% endif %}
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
{% endblock %}
@@ -78,7 +79,7 @@ use Drupal\user\UserInterface;
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* "published" = "status",
* },
* links = {
* "canonical" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}",
@@ -113,6 +114,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB

{% block use_trait %}
use EntityChangedTrait;
use EntityPublishedTrait;
{% endblock %}

{% block class_methods %}
@@ -229,27 +231,15 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
return $this;
}

/**
* {@inheritdoc}
*/
public function isPublished() {
return (bool) $this->getEntityKey('status');
}

/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? TRUE : FALSE);
return $this;
}

/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);

// Add the published field.
$fields += static::publishedBaseFieldDefinitions($entity_type);

$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the {{ label }} entity.'))
@@ -301,13 +291,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);

$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the {{ label }} is published.'))
{% if revisionable %}
->setRevisionable(TRUE)
{% endif %}
->setDefaultValue(TRUE)
$fields['status']->setDescription(t('A boolean indicating whether the {{ label }} is published.'))
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => -3,
@@ -14,6 +14,7 @@ use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\RevisionLogInterface;
{% endif %}
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\user\EntityOwnerInterface;
{% endblock %}

@@ -23,7 +24,7 @@ use Drupal\user\EntityOwnerInterface;
*
* @ingroup {{module}}
*/
interface {{ entity_class }}Interface extends ContentEntityInterface{% if revisionable %}, RevisionLogInterface{% endif %}, EntityChangedInterface, EntityOwnerInterface {% endblock %}
interface {{ entity_class }}Interface extends ContentEntityInterface{% if revisionable %}, RevisionLogInterface{% endif %}, EntityChangedInterface, EntityPublishedInterface, EntityOwnerInterface {% endblock %}
{% block class_methods %}
// Add get/set methods for your configuration properties here.

@@ -65,26 +66,6 @@ interface {{ entity_class }}Interface extends ContentEntityInterface{% if revisi
*/
public function setCreatedTime($timestamp);

/**
* Returns the {{ label }} published status indicator.
*
* Unpublished {{ label }} are only visible to restricted users.
*
* @return bool
* TRUE if the {{ label }} is published.
*/
public function isPublished();

/**
* Sets the published status of a {{ label }}.
*
* @param bool $published
* TRUE to set this {{ label }} to published, FALSE to set it to unpublished.
*
* @return \Drupal\{{ module }}\Entity\{{ entity_class }}Interface
* The called {{ label }} entity.
*/
public function setPublished($published);
{% if revisionable %}

/**

0 comments on commit 9cff0df

Please sign in to comment.
You can’t perform that action at this time.