Commit bea23d7c authored by larowlan's avatar larowlan

Issue #2949964 by Sam152, longwave, Berdir, amateescu: Add an EntityOwnerTrait...

Issue #2949964 by Sam152, longwave, Berdir, amateescu: Add an EntityOwnerTrait to standardize the base field needed by EntityOwnerInterface
parent 57dfd4ba
......@@ -207,3 +207,16 @@ function comment_update_8600() {
$field_storage_definition->setDefaultValueCallback(Comment::class . '::getDefaultHostname');
$entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
}
/**
* Set the 'owner' entity key and update the field.
*/
function comment_update_8601() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('comment');
$keys = $entity_type->getKeys();
$keys['owner'] = 'uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
$definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'comment'));
}
......@@ -13,7 +13,7 @@
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the comment entity class.
......@@ -52,6 +52,7 @@
* "langcode" = "langcode",
* "uuid" = "uuid",
* "published" = "status",
* "owner" = "uid",
* },
* links = {
* "canonical" = "/comment/{comment}",
......@@ -70,6 +71,7 @@
class Comment extends ContentEntityBase implements CommentInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
use EntityPublishedTrait;
/**
......@@ -221,6 +223,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['cid']->setLabel(t('Comment ID'))
->setDescription(t('The comment ID.'));
......@@ -256,12 +259,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
])
->setDisplayConfigurable('form', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID of the comment author.'))
->setTranslatable(TRUE)
->setSetting('target_type', 'user')
->setDefaultValue(0);
$fields['uid']
->setDescription(t('The user ID of the comment author.'));
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
......@@ -320,6 +319,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}
/**
* {@inheritdoc}
*/
public static function getDefaultEntityOwner() {
return 0;
}
/**
* {@inheritdoc}
*/
......@@ -524,29 +530,6 @@ public function getOwner() {
return $user;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* Get the comment type ID for this comment.
*
......
......@@ -42,8 +42,8 @@ abstract class CommentHalJsonTestBase extends CommentResourceTestBase {
'thread' => NULL,
'entity_type' => NULL,
'field_name' => NULL,
'entity_id' => NULL,
'uid' => "The 'administer comments' permission is required.",
'entity_id' => NULL,
];
/**
......
......@@ -31,9 +31,9 @@ abstract class CommentResourceTestBase extends EntityResourceTestBase {
*/
protected static $patchProtectedFieldNames = [
'status' => "The 'administer comments' permission is required.",
'uid' => "The 'administer comments' permission is required.",
'pid' => NULL,
'entity_id' => NULL,
'uid' => "The 'administer comments' permission is required.",
'name' => "The 'administer comments' permission is required.",
'homepage' => "The 'administer comments' permission is required.",
'created' => "The 'administer comments' permission is required.",
......
......@@ -72,4 +72,22 @@ public function testPublishedEntityKey() {
$this->assertTrue(\Drupal::database()->schema()->indexExists('comment_field_data', 'comment__status_comment_type'));
}
/**
* Tests that the comment entity type has an 'owner' entity key.
*
* @see comment_update_8600()
*/
public function testOwnerEntityKey() {
// Check that the 'owner' entity key does not exist prior to the update.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('comment');
$this->assertFalse($entity_type->getKey('owner'));
// Run updates.
$this->runUpdates();
// Check that the entity key exists and it has the correct value.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('comment');
$this->assertEquals('uid', $entity_type->getKey('owner'));
}
}
......@@ -29,3 +29,16 @@ function content_moderation_update_8401() {
$database_schema->dropTable('content_revision_tracker');
}
}
/**
* Set the 'owner' entity key and update the field.
*/
function content_moderation_update_8600() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('content_moderation_state');
$keys = $entity_type->getKeys();
$keys['owner'] = 'uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
$definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'content_moderation_state'));
}
......@@ -7,7 +7,7 @@
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\user\UserInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the Content moderation state entity.
......@@ -37,6 +37,7 @@
* "revision" = "revision_id",
* "uuid" = "uuid",
* "uid" = "uid",
* "owner" = "uid",
* "langcode" = "langcode",
* }
* )
......@@ -48,18 +49,18 @@
*/
class ContentModerationState extends ContentEntityBase implements ContentModerationStateInterface {
use EntityOwnerTrait;
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
$fields['uid']
->setLabel(t('User'))
->setDescription(t('The username of the entity creator.'))
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\content_moderation\Entity\ContentModerationState::getCurrentUserId')
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['workflow'] = BaseFieldDefinition::create('entity_reference')
......@@ -98,36 +99,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->getEntityKey('uid');
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* Creates or updates an entity's moderation state whilst saving that entity.
*
......@@ -185,10 +156,14 @@ public static function loadFromModeratedEntity(EntityInterface $entity) {
*
* @see \Drupal\content_moderation\Entity\ContentModerationState::baseFieldDefinitions()
*
* @deprecated The ::getCurrentUserId method is deprecated in 8.6.x and will
* be removed before 9.0.0.
*
* @return array
* An array of default values.
*/
public static function getCurrentUserId() {
@trigger_error('The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0.', E_USER_DEPRECATED);
return [\Drupal::currentUser()->id()];
}
......
<?php
namespace Drupal\Tests\content_moderation\Functional\Update;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests that content moderation settings are updated during database updates.
*
* @group content_moderation
* @group legacy
*/
class ContentModerationUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.4.0.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.4.0-content_moderation_installed.php',
];
}
/**
* Tests that the content moderation state entity has an 'owner' entity key.
*
* @see content_moderation_update_8600()
*/
public function testOwnerEntityKey() {
// Check that the 'owner' entity key does not exist prior to the update.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('content_moderation_state');
$this->assertFalse($entity_type->getKey('owner'));
// Run updates.
$this->runUpdates();
// Check that the entity key exists and it has the correct value.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('content_moderation_state');
$this->assertEquals('uid', $entity_type->getKey('owner'));
}
}
......@@ -616,6 +616,16 @@ public function testRevisionDefaultState($entity_type_id) {
$this->assertEquals($entity->getLoadedRevisionId(), $cms_entity->get('content_entity_revision_id')->value);
}
/**
* Tests the legacy method used as the default entity owner.
*
* @group legacy
* @expectedDeprecation The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0.
*/
public function testGetCurrentUserId() {
$this->assertEquals(['0'], ContentModerationState::getCurrentUserId());
}
/**
* Creates an entity.
*
......
......@@ -164,3 +164,16 @@ function file_update_8001() {
}
}
}
/**
* Set the 'owner' entity key and update the field.
*/
function file_update_8600() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('file');
$keys = $entity_type->getKeys();
$keys['owner'] = 'uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
$definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'file'));
}
......@@ -8,7 +8,7 @@
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\file\FileInterface;
use Drupal\user\UserInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the file entity class.
......@@ -36,13 +36,15 @@
* "id" = "fid",
* "label" = "filename",
* "langcode" = "langcode",
* "uuid" = "uuid"
* "uuid" = "uuid",
* "owner" = "uid",
* }
* )
*/
class File extends ContentEntityBase implements FileInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
/**
* {@inheritdoc}
......@@ -116,36 +118,6 @@ public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -232,6 +204,7 @@ public static function preDelete(EntityStorageInterface $storage, array $entitie
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['fid']->setLabel(t('File ID'))
->setDescription(t('The file ID.'));
......@@ -241,10 +214,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['langcode']->setLabel(t('Language code'))
->setDescription(t('The file language code.'));
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID of the file.'))
->setSetting('target_type', 'user');
$fields['uid']
->setDescription(t('The user ID of the file.'));
$fields['filename'] = BaseFieldDefinition::create('string')
->setLabel(t('Filename'))
......@@ -284,4 +255,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}
/**
* {@inheritdoc}
*/
public static function getDefaultEntityOwner() {
return NULL;
}
}
......@@ -26,6 +26,11 @@ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $st
break;
}
}
// Entity keys automatically have not null assigned to TRUE, but for the
// file entity, NULL is a valid value for uid.
if ($field_name === 'uid') {
$schema['fields']['uid']['not null'] = FALSE;
}
return $schema;
}
......
......@@ -56,4 +56,22 @@ public function testPostUpdate8001() {
$this->assertEqual($formatter_settings, ['use_description_as_link_text' => FALSE]);
}
/**
* Tests that the file entity type has an 'owner' entity key.
*
* @see file_update_8600()
*/
public function testOwnerEntityKey() {
// Check that the 'owner' entity key does not exist prior to the update.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('file');
$this->assertFalse($entity_type->getKey('owner'));
// Run updates.
$this->runUpdates();
// Check that the entity key exists and it has the correct value.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('file');
$this->assertEqual('uid', $entity_type->getKey('owner'));
}
}
......@@ -163,3 +163,16 @@ function media_update_8600() {
->set('oembed_providers_url', 'https://oembed.com/providers.json')
->save(TRUE);
}
/**
* Set the 'owner' entity key and update the field.
*/
function media_update_8601() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('media');
$keys = $entity_type->getKeys();
$keys['owner'] = 'uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
$definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'media'));
}
......@@ -10,7 +10,7 @@
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceEntityConstraintsInterface;
use Drupal\media\MediaSourceFieldConstraintsInterface;
use Drupal\user\UserInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the media entity class.
......@@ -60,6 +60,7 @@
* "langcode" = "langcode",
* "uuid" = "uuid",
* "published" = "status",
* "owner" = "uid",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_user",
......@@ -85,6 +86,7 @@
*/
class Media extends EditorialContentEntityBase implements MediaInterface {
use EntityOwnerTrait;
use StringTranslationTrait;
/**
......@@ -130,34 +132,6 @@ public function setCreatedTime($timestamp) {
return $this->set('created', $timestamp);
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
return $this->set('uid', $account->id());
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
return $this->set('uid', $uid);
}
/**
* {@inheritdoc}
*/
......@@ -453,6 +427,7 @@ public function validate() {
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
......@@ -484,13 +459,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDisplayConfigurable('view', TRUE)
->setReadOnly(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
$fields['uid']
->setLabel(t('Authored by'))
->setDescription(t('The user ID of the author.'))
->setRevisionable(TRUE)
->setDefaultValueCallback(static::class . '::getCurrentUserId')
->setSetting('target_type', 'user')
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
......@@ -551,10 +523,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
*
* @see ::baseFieldDefinitions()
*
* @deprecated The ::getCurrentUserId method is deprecated in 8.6.x and will
* be removed before 9.0.0.
*
* @return int[]
* An array of default values.
*/
public static function getCurrentUserId() {
@trigger_error('The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0.', E_USER_DEPRECATED);
return [\Drupal::currentUser()->id()];
}
......
......@@ -62,4 +62,22 @@ public function testOEmbedConfig() {
$this->assertSame('', $config->get('iframe_domain'));
}
/**
* Tests that the media entity type has an 'owner' entity key.
*
* @see media_update_8600()
*/
public function testOwnerEntityKey() {
// Check that the 'owner' entity key does not exist prior to the update.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('media');
$this->assertFalse($entity_type->getKey('owner'));
// Run updates.
$this->runUpdates();
// Check that the entity key exists and it has the correct value.
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('media');
$this->assertEquals('uid', $entity_type->getKey('owner'));
}
}
......@@ -34,4 +34,14 @@ public function testNameBaseField() {
$this->assertSame($field_definitions['name']->getDisplayOptions('view'), ['region' => 'hidden']);
}
/**
* Tests the legacy method used as the default entity owner.
*
* @group legacy
* @expectedDeprecation The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0.
*/
public function testGetCurrentUserId() {
$this->assertEquals(['1'], Media::getCurrentUserId());
}
}
......@@ -267,3 +267,16 @@ function node_update_8401() {
node_access_needs_rebuild(TRUE);
}
}
/**
* Set the 'owner' entity key and update the field.
*/
function node_update_8600() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('node');
$keys = $entity_type->getKeys();
$keys['owner'] = 'uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
$definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'node'));
}
......@@ -8,7 +8,7 @@
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\user\UserInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the node entity class.
......@@ -59,6 +59,7 @@
* "status" = "status",
* "published" = "status",
* "uid" = "uid",
* "owner" = "uid",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_uid",
......@@ -82,6 +83,8 @@
*/
class Node extends EditorialContentEntityBase implements NodeInterface {
use EntityOwnerTrait;
/**
* Whether the node is being previewed or not.
*
......@@ -250,36 +253,6 @@ public function setSticky($sticky) {
return $this;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->getEntityKey('uid');
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -300,6 +273,7 @@ public function setRevisionAuthorId($uid) {
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
......@@ -318,13 +292,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
])
->setDisplayConfigurable('form', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
$fields['uid']
->setLabel(t('Authored by'))
->setDescription(t('The username of the content author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
->setTranslatable(TRUE)