diff --git a/core/lib/Drupal/Core/Entity/EntityAuthorInterface.php b/core/lib/Drupal/Core/Entity/EntityAuthorInterface.php new file mode 100644 index 0000000..050640d --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityAuthorInterface.php @@ -0,0 +1,42 @@ +getAuthorId() after https://drupal.org/node/2078387 - if ($entity->getPropertyDefinition('uid')) { - $last_comment_uid = $entity->get('uid')->value; + if ($entity instanceof EntityAuthorInterface) { + $last_comment_uid = $entity->getAuthorId(); } - else { - // Default to current user when entity does not have a uid property. + if (!isset($last_comment_uid)) { + // Default to current user when entity does not implements + // EntityAuthorInterface or author is not set. $last_comment_uid = \Drupal::currentUser()->id(); } // Default to REQUEST_TIME when entity does not have a changed property. @@ -1201,7 +1202,7 @@ function comment_user_cancel($edit, $account, $method) { case 'user_cancel_reassign': $comments = entity_load_multiple_by_properties('comment', array('uid' => $account->id())); foreach ($comments as $comment) { - $comment->uid->target_id = 0; + $comment->setAuthorId(0); $comment->save(); } break; @@ -1393,7 +1394,7 @@ function comment_preview(CommentInterface $comment) { } if ($account->id()) { - $comment->uid->target_id = $account->id(); + $comment->setAuthorId($account->id()); $comment->name->value = check_plain($account->getUsername()); } elseif (empty($comment->name->value)) { @@ -1464,7 +1465,7 @@ function comment_preprocess_block(&$variables) { */ function comment_prepare_author(CommentInterface $comment) { // The account has been pre-loaded by CommentRenderController::buildContent(). - $account = $comment->uid->entity; + $account = $comment->getAuthor(); if (empty($account->uid->value)) { $account = entity_create('user', array('uid' => 0, 'name' => $comment->name->value, 'homepage' => $comment->homepage->value)); } @@ -1586,12 +1587,11 @@ function template_preprocess_comment(&$variables) { if ($variables['status'] != 'published') { $variables['attributes']['class'][] = $variables['status']; } - if (!$comment->uid->target_id) { + if (!$comment->getAuthorId()) { $variables['attributes']['class'][] = 'by-anonymous'; } else { - // @todo Use $entity->getAuthorId() after https://drupal.org/node/2078387 - if ($commented_entity->getPropertyDefinition('uid') && $comment->uid->target_id == $commented_entity->get('uid')->value) { + if ($commented_entity instanceof EntityAuthorInterface && $comment->getAuthorId() == $commented_entity->getAuthorId()) { $variables['attributes']['class'][] = 'by-' . $commented_entity->entityType() . '-author'; } } @@ -1599,7 +1599,7 @@ function template_preprocess_comment(&$variables) { $variables['attributes']['class'][] = 'clearfix'; // Add comment author user ID. Necessary for the comment-by-viewer library. - $variables['attributes']['data-comment-user-id'] = $comment->uid->value; + $variables['attributes']['data-comment-user-id'] = $comment->getAuthorId(); $variables['content_attributes']['class'][] = 'content'; } diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc index eab4d3e..6702d24 100644 --- a/core/modules/comment/comment.tokens.inc +++ b/core/modules/comment/comment.tokens.inc @@ -142,13 +142,13 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = break; case 'name': - $name = ($comment->uid->target_id == 0) ? \Drupal::config('user.settings')->get('anonymous') : $comment->name->value; + $name = ($comment->getAuthorId() == 0) ? \Drupal::config('user.settings')->get('anonymous') : $comment->name->value; $replacements[$original] = $sanitize ? filter_xss($name) : $name; break; case 'mail': - if ($comment->uid->target_id != 0) { - $mail = $comment->uid->entity->getEmail(); + if ($comment->getAuthorId() != 0) { + $mail = $comment->getAuthor()->getEmail(); } else { $mail = $comment->mail->value; @@ -244,7 +244,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options); } - if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->uid->entity) { + if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getAuthor()) { $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options); } } diff --git a/core/modules/comment/lib/Drupal/comment/CommentAccessController.php b/core/modules/comment/lib/Drupal/comment/CommentAccessController.php index 800991f..1ff84c1 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentAccessController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentAccessController.php @@ -28,7 +28,7 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A break; case 'update': - return ($account->id() && $account->id() == $entity->uid->value && $entity->status->value == COMMENT_PUBLISHED && user_access('edit own comments', $account)) || user_access('administer comments', $account); + return ($account->id() && $account->id() == $entity->getAuthorId() && $entity->status->value == COMMENT_PUBLISHED && user_access('edit own comments', $account)) || user_access('administer comments', $account); break; case 'delete': diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index 4555e8f..38b6c00 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -204,7 +204,7 @@ public function form(array $form, array &$form_state) { // Used for conditional validation of author fields. $form['is_anonymous'] = array( '#type' => 'value', - '#value' => ($comment->id() ? !$comment->uid->target_id : $this->currentUser->isAnonymous()), + '#value' => ($comment->id() ? !$comment->getAuthorId() : $this->currentUser->isAnonymous()), ); // Make the comment inherit the current content language unless specifically @@ -318,7 +318,7 @@ public function submit(array $form, array &$form_state) { // @todo Too fragile. Should be prepared and stored in comment_form() // already. if (!$comment->is_anonymous && !empty($comment->name->value) && ($account = user_load_by_name($comment->name->value))) { - $comment->uid->target_id = $account->id(); + $comment->setAuthorId($account->id()); } // If the comment was posted by an anonymous user and no author name was // required, use "Anonymous" by default. diff --git a/core/modules/comment/lib/Drupal/comment/CommentInterface.php b/core/modules/comment/lib/Drupal/comment/CommentInterface.php index fb9c043..849f902 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentInterface.php +++ b/core/modules/comment/lib/Drupal/comment/CommentInterface.php @@ -8,12 +8,13 @@ namespace Drupal\comment; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityAuthorInterface; use Drupal\Core\Entity\EntityChangedInterface; /** * Provides an interface defining a comment entity. */ -interface CommentInterface extends ContentEntityInterface, EntityChangedInterface { +interface CommentInterface extends ContentEntityInterface, EntityChangedInterface, EntityAuthorInterface { /** * Returns the permalink URL for this comment. diff --git a/core/modules/comment/lib/Drupal/comment/CommentRenderController.php b/core/modules/comment/lib/Drupal/comment/CommentRenderController.php index b577b27..adefdd5 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentRenderController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentRenderController.php @@ -91,7 +91,7 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang // Pre-load associated users into cache to leverage multiple loading. $uids = array(); foreach ($entities as $entity) { - $uids[] = $entity->uid->target_id; + $uids[] = $entity->getAuthorId(); } $this->entityManager->getStorageController('user')->loadMultiple(array_unique($uids)); diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index e91d8a1..9141048 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -7,6 +7,7 @@ namespace Drupal\comment; +use Drupal\Core\Entity\EntityAuthorInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\DatabaseStorageControllerNG; use Drupal\Core\Entity\EntityChangedInterface; @@ -97,6 +98,14 @@ public function updateEntityStatistics(CommentInterface $comment) { else { // Comments do not exist. $entity = entity_load($comment->entity_type->value, $comment->entity_id->value); + if ($entity instanceof EntityAuthorInterface) { + $last_comment_uid = $entity->getAuthorId(); + } + if (!isset($last_comment_uid)) { + // Default to current user when entity does not implements + // EntityAuthorInterface or author is not set. + $last_comment_uid = \Drupal::currentUser()->id(); + } $this->database->update('comment_entity_statistics') ->fields(array( 'cid' => 0, @@ -105,10 +114,9 @@ public function updateEntityStatistics(CommentInterface $comment) { // REQUEST_TIME. 'last_comment_timestamp' => ($entity instanceof EntityChangedInterface) ? $entity->getChangedTime() : REQUEST_TIME, 'last_comment_name' => '', - // @todo Use $entity->getAuthorId() after https://drupal.org/node/2078387 // Get the user ID from the entity if it's set, or default to the // currently logged in user. - 'last_comment_uid' => $entity->getPropertyDefinition('uid') ? $entity->get('uid')->value : \Drupal::currentUser()->id(), + 'last_comment_uid' => $last_comment_uid, )) ->condition('entity_id', $comment->entity_id->value) ->condition('entity_type', $comment->entity_type->value) diff --git a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php index 617d4ad..7424188 100644 --- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php @@ -7,12 +7,11 @@ namespace Drupal\comment\Entity; -use Drupal\Core\Entity\EntityNG; -use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; -use Drupal\comment\CommentInterface; +use Drupal\Core\Entity\Annotation\EntityType; +use Drupal\Core\Entity\EntityNG; use Drupal\Core\Entity\EntityStorageControllerInterface; -use Drupal\Core\Language\Language; +use Drupal\comment\CommentInterface; /** * Defines the comment entity class. @@ -289,7 +288,7 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { } // We test the value with '===' because we need to modify anonymous // users as well. - if ($this->uid->target_id === $user->id() && $user->isAuthenticated()) { + if ($this->getAuthorId() === $user->id() && $user->isAuthenticated()) { $this->name->value = $user->getUsername(); } // Add the values which aren't passed into the function. @@ -472,4 +471,26 @@ public static function preCreate(EntityStorageControllerInterface $storage_contr } } + /** + * {@inheritdoc} + */ + public function getAuthor() { + return $this->get('uid')->entity; + } + + /** + * {@inheritdoc} + */ + public function getAuthorId() { + return $this->get('uid')->target_id; + } + + /** + * {@inheritdoc} + */ + public function setAuthorId($uid) { + $this->set('uid', $uid); + return $this; + } + } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php index 324e21e..6dad847 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php @@ -70,7 +70,7 @@ function testCommentInterface() { // Test changing the comment author to "Anonymous". $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => '')); - $this->assertTrue(empty($comment->name->value) && $comment->uid->target_id == 0, 'Comment author successfully changed to anonymous.'); + $this->assertTrue(empty($comment->name->value) && $comment->getAuthorId() == 0, 'Comment author successfully changed to anonymous.'); // Test changing the comment author to an unverified user. $random_name = $this->randomName(); @@ -82,7 +82,7 @@ function testCommentInterface() { // Test changing the comment author to a verified user. $this->drupalGet('comment/' . $comment->id() . '/edit'); $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => $this->web_user->getUsername())); - $this->assertTrue($comment->name->value == $this->web_user->getUsername() && $comment->uid->target_id == $this->web_user->id(), 'Comment author successfully changed to a registered user.'); + $this->assertTrue($comment->name->value == $this->web_user->getUsername() && $comment->getAuthorId() == $this->web_user->id(), 'Comment author successfully changed to a registered user.'); $this->drupalLogout(); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php index 451831a..c73f609 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php @@ -68,7 +68,7 @@ function testCommentTokenReplacement() { $tests['[comment:parent:title]'] = check_plain($parent_comment->subject->value); $tests['[comment:node:nid]'] = $comment->entity_id->value; $tests['[comment:node:title]'] = check_plain($node->getTitle()); - $tests['[comment:author:uid]'] = $comment->uid->target_id; + $tests['[comment:author:uid]'] = $comment->getAuthorId(); $tests['[comment:author:name]'] = check_plain($this->admin_user->getUsername()); // Test to make sure that we generated something for each token. diff --git a/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php b/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php index 3f3759e..adfd4d3 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php @@ -86,7 +86,7 @@ public function setUp() { 'entity_type' => 'node', 'entity_id' => $this->node->id(), )); - $comment->uid->target_id = 0; + $comment->setAuthorId(0); // Stagger the comments so the timestamp sorting works. $comment->created->value = REQUEST_TIME - $i; $comment->subject->value = 'Test comment ' . $i; diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php index 2b222d2..38aa4b5 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php @@ -9,6 +9,7 @@ use Drupal\Component\Annotation\Plugin; use Drupal\Core\Annotation\Translation; +use Drupal\Core\Entity\EntityAuthorInterface; use Drupal\Core\Entity\Field\FieldItemListInterface; use Drupal\field\Plugin\Type\Widget\WidgetBase; use Symfony\Component\Validator\ConstraintViolationInterface; @@ -95,8 +96,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#size' => $this->getSetting('size'), '#placeholder' => $this->getSetting('placeholder'), '#element_validate' => array(array($this, 'elementValidate')), - // @todo: Use wrapper to get the user if exists or needed. - '#autocreate_uid' => isset($entity->uid) ? $entity->uid : $user->id(), + '#autocreate_uid' => ($entity instanceof EntityAuthorInterface) ? $entity->getAuthorId() : $user->id(), ); return array('target_id' => $element); @@ -173,11 +173,16 @@ protected function createNewEntity($label, $uid) { $bundle_key = $entity_info['entity_keys']['bundle']; $label_key = $entity_info['entity_keys']['label']; - return $entity_manager->getStorageController($target_type)->create(array( + $entity = $entity_manager->getStorageController($target_type)->create(array( $label_key => $label, - $bundle_key => $bundle, - 'uid' => $uid, + $bundle_key => $bundle )); + + if ($entity instanceof EntityAuthorInterface) { + $entity->setAuthorId($uid); + } + + return $entity; } /** diff --git a/core/modules/file/file.api.php b/core/modules/file/file.api.php index 2624623..15254bf 100644 --- a/core/modules/file/file.api.php +++ b/core/modules/file/file.api.php @@ -113,9 +113,9 @@ function hook_file_insert(Drupal\file\FileInterface $file) { */ function hook_file_update(Drupal\file\FileInterface $file) { // Make sure that the file name starts with the owner's user name. - if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) { + if (strpos($file->getFilename(), $file->getAuthor()->name) !== 0) { $old_filename = $file->getFilename(); - $file->setFilename($file->getOwner()->name . '_' . $file->getFilename()); + $file->setFilename($file->getAuthor()->name . '_' . $file->getFilename()); $file->save(); watchdog('file', t('%source has been renamed to %destination', array('%source' => $old_filename, '%destination' => $file->getFilename()))); @@ -134,8 +134,8 @@ function hook_file_update(Drupal\file\FileInterface $file) { */ function hook_file_copy(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) { // Make sure that the file name starts with the owner's user name. - if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) { - $file->setFilename($file->getOwner()->name . '_' . $file->getFilename()); + if (strpos($file->getFilename(), $file->getAuthor()->name) !== 0) { + $file->setFilename($file->getAuthor()->name . '_' . $file->getFilename()); $file->save(); watchdog('file', t('Copied file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->getFilename()))); @@ -154,8 +154,8 @@ function hook_file_copy(Drupal\file\FileInterface $file, Drupal\file\FileInterfa */ function hook_file_move(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) { // Make sure that the file name starts with the owner's user name. - if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) { - $file->setFilename($file->getOwner()->name . '_' . $file->getFilename()); + if (strpos($file->getFilename(), $file->getAuthor()->name) !== 0) { + $file->setFilename($file->getAuthor()->name . '_' . $file->getFilename()); $file->save(); watchdog('file', t('Moved file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->getFilename()))); diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 5dfdb30..d5231a3 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -632,7 +632,7 @@ function file_file_download($uri, $field_type = 'file') { // temporary files where the host entity has not yet been saved (for example, // an image preview on a node/add form) in which case, allow download by the // file's owner. - if (empty($references) && ($file->isPermanent() || $file->getOwner()->id() != $user->id())) { + if (empty($references) && ($file->isPermanent() || $file->getAuthorId() != $user->id())) { return; } @@ -1046,7 +1046,7 @@ function file_tokens($type, $tokens, array $data = array(), array $options = arr break; case 'owner': - $name = $file->getOwner()->label(); + $name = $file->getAuthor()->label(); $replacements[$original] = $sanitize ? check_plain($name) : $name; break; } @@ -1056,8 +1056,8 @@ function file_tokens($type, $tokens, array $data = array(), array $options = arr $replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getChangedTime()), $options); } - if (($owner_tokens = $token_service->findWithPrefix($tokens, 'owner')) && $file->getOwner()) { - $replacements += $token_service->generate('user', $owner_tokens, array('user' => $file->getOwner()), $options); + if (($owner_tokens = $token_service->findWithPrefix($tokens, 'owner')) && $file->getAuthor()) { + $replacements += $token_service->generate('user', $owner_tokens, array('user' => $file->getAuthor()), $options); } } diff --git a/core/modules/file/lib/Drupal/file/Entity/File.php b/core/modules/file/lib/Drupal/file/Entity/File.php index b891253..81f915e 100644 --- a/core/modules/file/lib/Drupal/file/Entity/File.php +++ b/core/modules/file/lib/Drupal/file/Entity/File.php @@ -120,15 +120,23 @@ public function getChangedTime() { /** * {@inheritdoc} */ - public function getOwner() { + public function getAuthor() { return $this->get('uid')->entity; } /** * {@inheritdoc} */ - public function setOwner(UserInterface $user) { - return $this->get('uid')->entity = $user; + public function getAuthorId() { + return $this->get('uid')->target_id; + } + + /** + * {@inheritdoc} + */ + public function setAuthorId($uid) { + $this->set('uid', $uid); + return $this; } /** diff --git a/core/modules/file/lib/Drupal/file/FileInterface.php b/core/modules/file/lib/Drupal/file/FileInterface.php index 73783ef..31f7350 100644 --- a/core/modules/file/lib/Drupal/file/FileInterface.php +++ b/core/modules/file/lib/Drupal/file/FileInterface.php @@ -8,13 +8,14 @@ namespace Drupal\file; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityAuthorInterface; use Drupal\Core\Entity\EntityChangedInterface; use Drupal\user\UserInterface; /** * Defines getter and setter methods for file entity base fields. */ -interface FileInterface extends ContentEntityInterface, EntityChangedInterface { +interface FileInterface extends ContentEntityInterface, EntityChangedInterface, EntityAuthorInterface { /** * Returns the name of the file. @@ -111,20 +112,4 @@ public function setPermanent(); */ public function setTemporary(); - /** - * Returns the user that owns this file. - * - * @return \Drupal\user\UserInterface - * The user that owns the file. - */ - public function getOwner(); - - /** - * Sets the user that owns this file. - * - * @param \Drupal\user\UserInterface $user - * The user that owns the file. - */ - public function setOwner(UserInterface $user); - } diff --git a/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php b/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php index 924c2b9..fea281a 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php @@ -56,7 +56,7 @@ function testFileTokenReplacement() { $tests['[file:timestamp]'] = format_date($file->getChangedTime(), 'medium', '', NULL, $language_interface->id); $tests['[file:timestamp:short]'] = format_date($file->getChangedTime(), 'short', '', NULL, $language_interface->id); $tests['[file:owner]'] = check_plain(user_format_name($this->admin_user)); - $tests['[file:owner:uid]'] = $file->getOwner()->id(); + $tests['[file:owner:uid]'] = $file->getAuthorId(); // Test to make sure that we generated something for each token. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); diff --git a/core/modules/node/lib/Drupal/node/NodeInterface.php b/core/modules/node/lib/Drupal/node/NodeInterface.php index 34f7274..35673ab 100644 --- a/core/modules/node/lib/Drupal/node/NodeInterface.php +++ b/core/modules/node/lib/Drupal/node/NodeInterface.php @@ -7,6 +7,7 @@ namespace Drupal\node; +use Drupal\Core\Entity\EntityAuthorInterface; use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\user\UserInterface; @@ -14,7 +15,7 @@ /** * Provides an interface defining a node entity. */ -interface NodeInterface extends ContentEntityInterface, EntityChangedInterface { +interface NodeInterface extends ContentEntityInterface, EntityChangedInterface, EntityAuthorInterface { /** * Returns the node type. @@ -25,7 +26,6 @@ public function getType(); /** - * * Returns the node title. * * @return string @@ -102,33 +102,6 @@ public function isSticky(); public function setSticky($sticky); /** - * Returns the node author user entity. - * - * @return \Drupal\user\UserInterface - * The author user entity. - */ - public function getAuthor(); - - /** - * Returns the node author user ID. - * - * @return int - * The author user ID. - */ - public function getAuthorId(); - - /** - * Sets the node author user ID. - * - * @param int $uid - * The author user id. - * - * @return \Drupal\node\NodeInterface - * The called node entity. - */ - public function setAuthorId($uid); - - /** * Returns the node published status indicator. * * Unpublished nodes are only visible to their authors and to administrators. @@ -160,7 +133,7 @@ public function getRevisionCreationTime(); /** * Sets the node revision creation timestamp. * - * @param int $imestamp + * @param int $timestamp * The UNIX timestamp of when this revision was created. * * @return \Drupal\node\NodeInterface diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php index 8a6b8c7..ac72317 100644 --- a/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php @@ -278,8 +278,8 @@ function _testBasicCommentRdfaMarkup($graph, $comment, $account = array()) { $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/rss/1.0/modules/content/encoded', $expected_value), 'Comment body found in RDF output (content:encoded).'); // The comment author can be a registered user or an anonymous user. - if ($comment->uid->value > 0) { - $author_uri = url('user/' . $comment->uid->value, array('absolute' => TRUE)); + if ($comment->getAuthorId() > 0) { + $author_uri = url('user/' . $comment->getAuthorId(), array('absolute' => TRUE)); // Comment relation to author. $expected_value = array( 'type' => 'uri', @@ -307,7 +307,7 @@ function _testBasicCommentRdfaMarkup($graph, $comment, $account = array()) { $this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'Comment author name found in RDF output (foaf:name).'); // Comment author homepage (only for anonymous authors). - if ($comment->uid->value == 0) { + if ($comment->getAuthorId() == 0) { $expected_value = array( 'type' => 'uri', 'value' => 'http://example.org/', diff --git a/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php b/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php index 540c19a..221c547 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/FileTestBase.php @@ -37,7 +37,7 @@ function setUp() { */ function assertFileUnchanged(FileInterface $before, FileInterface $after) { $this->assertEqual($before->id(), $after->id(), t('File id is the same: %file1 == %file2.', array('%file1' => $before->id(), '%file2' => $after->id())), 'File unchanged'); - $this->assertEqual($before->getOwner()->id(), $after->getOwner()->id(), t('File owner is the same: %file1 == %file2.', array('%file1' => $before->getOwner()->id(), '%file2' => $after->getOwner()->id())), 'File unchanged'); + $this->assertEqual($before->getAuthorId(), $after->getAuthorId(), t('File owner is the same: %file1 == %file2.', array('%file1' => $before->getAuthorId(), '%file2' => $after->getAuthorId())), 'File unchanged'); $this->assertEqual($before->getFilename(), $after->getFilename(), t('File name is the same: %file1 == %file2.', array('%file1' => $before->getFilename(), '%file2' => $after->getFilename())), 'File unchanged'); $this->assertEqual($before->getFileUri(), $after->getFileUri(), t('File path is the same: %file1 == %file2.', array('%file1' => $before->getFileUri(), '%file2' => $after->getFileUri())), 'File unchanged'); $this->assertEqual($before->getMimeType(), $after->getMimeType(), t('File MIME type is the same: %file1 == %file2.', array('%file1' => $before->getMimeType(), '%file2' => $after->getMimeType())), 'File unchanged'); diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php index 06a7fe7..fc7e1e1 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php @@ -8,6 +8,7 @@ namespace Drupal\entity_test\Entity; use Drupal\Core\Entity\EntityNG; +use Drupal\Core\Entity\EntityAuthorInterface; use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; use Drupal\Core\Language\Language; @@ -39,7 +40,7 @@ * menu_base_path = "entity-test/manage/%entity_test" * ) */ -class EntityTest extends EntityNG { +class EntityTest extends EntityNG implements EntityAuthorInterface { /** * The entity ID. @@ -147,4 +148,27 @@ public static function baseFieldDefinitions($entity_type) { ); return $fields; } + + /** + * {@inheritdoc} + */ + public function getAuthor() { + return $this->get('user_id')->entity; + } + + /** + * {@inheritdoc} + */ + public function getAuthorId() { + return $this->get('user_id')->target_id; + } + + /** + * {@inheritdoc} + */ + public function setAuthorId($uid) { + $this->set('user_id', $uid); + return $this; + } + } diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module index 88f4f02..b9802b3 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -236,7 +236,7 @@ function tracker_comment_update($comment) { // $comment->save() calls hook_comment_publish() for all published comments // so we need to handle all other values here. if ($comment->status->value != COMMENT_PUBLISHED && $comment->entity_type->value == 'node') { - _tracker_remove($comment->entity_id->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_remove($comment->entity_id->target_id, $comment->getAuthorId(), $comment->changed->value); } } @@ -248,7 +248,7 @@ function tracker_comment_update($comment) { */ function tracker_comment_publish($comment) { if ($comment->entity_type->value == 'node') { - _tracker_add($comment->entity_id->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_add($comment->entity_id->target_id, $comment->getAuthorId(), $comment->changed->value); } } @@ -257,7 +257,7 @@ function tracker_comment_publish($comment) { */ function tracker_comment_unpublish($comment) { if ($comment->entity_type->value == 'node') { - _tracker_remove($comment->entity_id->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_remove($comment->entity_id->target_id, $comment->getAuthorId(), $comment->changed->value); } } @@ -266,7 +266,7 @@ function tracker_comment_unpublish($comment) { */ function tracker_comment_delete($comment) { if ($comment->entity_type->value == 'node') { - _tracker_remove($comment->entity_id->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_remove($comment->entity_id->target_id, $comment->getAuthorId(), $comment->changed->value); } }