RevisionLogEntityTrait.php 4.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php

namespace Drupal\Core\Entity;

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;

/**
 * Provides a trait for accessing revision logging and ownership information.
 *
 * @ingroup entity_api
 */
trait RevisionLogEntityTrait {

  /**
   * Provides revision-related base field definitions for an entity type.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
   *   An array of base field definitions for the entity type, keyed by field
   *   name.
   *
   * @see \Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()
   */
  public static function revisionLogBaseFieldDefinitions(EntityTypeInterface $entity_type) {
28
    $fields[static::getRevisionMetadataKey($entity_type, 'revision_created')] = BaseFieldDefinition::create('created')
29 30 31 32
      ->setLabel(t('Revision create time'))
      ->setDescription(t('The time that the current revision was created.'))
      ->setRevisionable(TRUE);

33
    $fields[static::getRevisionMetadataKey($entity_type, 'revision_user')] = BaseFieldDefinition::create('entity_reference')
34 35 36 37 38
      ->setLabel(t('Revision user'))
      ->setDescription(t('The user ID of the author of the current revision.'))
      ->setSetting('target_type', 'user')
      ->setRevisionable(TRUE);

39
    $fields[static::getRevisionMetadataKey($entity_type, 'revision_log_message')] = BaseFieldDefinition::create('string_long')
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      ->setLabel(t('Revision log message'))
      ->setDescription(t('Briefly describe the changes you have made.'))
      ->setRevisionable(TRUE)
      ->setDefaultValue('')
      ->setDisplayOptions('form', [
        'type' => 'string_textarea',
        'weight' => 25,
        'settings' => [
          'rows' => 4,
        ],
      ]);

    return $fields;
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionCreationTime().
   */
  public function getRevisionCreationTime() {
59
    return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_created')}->value;
60 61 62 63 64 65
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionCreationTime().
   */
  public function setRevisionCreationTime($timestamp) {
66
    $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_created')}->value = $timestamp;
67 68 69 70 71 72 73
    return $this;
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUser().
   */
  public function getRevisionUser() {
74
    return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->entity;
75 76 77 78 79 80
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUser().
   */
  public function setRevisionUser(UserInterface $account) {
81
    $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->entity = $account;
82 83 84 85 86 87 88
    return $this;
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUserId().
   */
  public function getRevisionUserId() {
89
    return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->target_id;
90 91 92 93 94 95
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUserId().
   */
  public function setRevisionUserId($user_id) {
96
    $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->target_id = $user_id;
97 98 99 100 101 102 103
    return $this;
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionLogMessage().
   */
  public function getRevisionLogMessage() {
104
    return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_log_message')}->value;
105 106 107 108 109 110
  }

  /**
   * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionLogMessage().
   */
  public function setRevisionLogMessage($revision_log_message) {
111
    $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_log_message')}->value = $revision_log_message;
112 113 114
    return $this;
  }

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  /**
   * Gets the name of a revision metadata field.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   A content entity type definition.
   * @param string $key
   *   The revision metadata key to get, must be one of 'revision_created',
   *   'revision_user' or 'revision_log_message'.
   *
   * @return string
   *   The name of the field for the specified $key.
   */
  protected static function getRevisionMetadataKey(EntityTypeInterface $entity_type, $key) {
    // We need to prevent ContentEntityType::getRevisionMetadataKey() from
    // providing fallback as that requires fetching the entity type's field
    // definition leading to an infinite recursion.
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
    $revision_metadata_keys = $entity_type->getRevisionMetadataKeys(FALSE) + [
      'revision_created' => 'revision_created',
      'revision_user' => 'revision_user',
      'revision_log_message' => 'revision_log_message',
    ];

    return $revision_metadata_keys[$key];
  }

141
}