Skip to content

Commit

Permalink
Use EntityPublishedTrait (#3991)
Browse files Browse the repository at this point in the history
* Extends EntityPublishedInterface

* Use EntityPublishedTrait
  • Loading branch information
el7cosmos authored and LOBsTerr committed Apr 5, 2019
1 parent aeeb3a3 commit 9cff0df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 44 deletions.
30 changes: 7 additions & 23 deletions templates/module/src/Entity/entity-content.php.twig
Expand Up @@ -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 %}
Expand Down Expand Up @@ -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~'}' }}",
Expand Down Expand Up @@ -113,6 +114,7 @@ class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityB

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

{% block class_methods %}
Expand Down Expand Up @@ -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.'))
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 2 additions & 21 deletions templates/module/src/Entity/interface-entity-content.php.twig
Expand Up @@ -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 %}

Expand All @@ -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.

Expand Down Expand Up @@ -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 %}

/**
Expand Down

0 comments on commit 9cff0df

Please sign in to comment.