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 @@ + $account->id())); foreach ($comments as $comment) { - $comment->uid->target_id = 0; + $comment->setAuthorId(0); $comment->save(); } break; @@ -1424,7 +1424,7 @@ function comment_preview(Comment $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)) { @@ -1477,7 +1477,7 @@ function comment_preprocess_block(&$variables) { */ function comment_prepare_author(Comment $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)); } @@ -1602,14 +1602,14 @@ function template_preprocess_comment(&$variables) { if ($variables['new']) { $variables['attributes']['class'][] = 'new'; } - if (!$comment->uid->target_id) { + if (!$comment->getAuthorId()) { $variables['attributes']['class'][] = 'by-anonymous'; } else { - if ($comment->uid->target_id == $variables['node']->getAuthorId()) { + if ($comment->getAuthorId() == $variables['node']->getAuthorId()) { $variables['attributes']['class'][] = 'by-node-author'; } - if ($comment->uid->target_id == $variables['user']->id()) { + if ($comment->getAuthorId() == $variables['user']->id()) { $variables['attributes']['class'][] = 'by-viewer'; } } diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc index 8002162..066d9fc 100644 --- a/core/modules/comment/comment.tokens.inc +++ b/core/modules/comment/comment.tokens.inc @@ -133,13 +133,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; @@ -216,7 +216,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/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index 750f748..b4865ed 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -151,7 +151,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 : $user->isAnonymous()), + '#value' => ($comment->id() ? !$comment->getAuthorId() : $user->isAnonymous()), ); // Make the comment inherit the current content language unless specifically @@ -269,7 +269,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 456b957..32d25c2 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/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php index 59c122b..6f7250f 100644 --- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php @@ -468,4 +468,26 @@ public function getChangedTime() { return $this->changed->value; } + /** + * {@inheritdoc} + */ + public function getAuthor() { + return $this->get('uid')->entity; + } + + /** + * {@inheritdoc} + */ + public function getAuthorId() { + return $this->get('uid')->value; + } + + /** + * {@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 7ebb12e..4cbe4a2 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php @@ -69,7 +69,7 @@ function testCommentInterface() { // Test changing the comment author to "Anonymous". $this->drupalGet('comment/' . $comment->id() . '/edit'); $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(); @@ -81,7 +81,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 1819b10..c485c6a 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->nid->target_id; $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 f4a4316..906df52 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php @@ -78,7 +78,7 @@ public function setUp() { // Create some comments and attach them to the created node. for ($i = 0; $i < $this->masterDisplayResults; $i++) { $comment = entity_create('comment', array('node_type' => 'comment_node_' . $this->node->getType())); - $comment->uid->target_id = 0; + $comment->setAuthorId(0); $comment->nid->target_id = $this->node->id(); $comment->subject->value = 'Test comment ' . $i; $comment->comment_body->value = 'Test body ' . $i; 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 e59126d..0bb97f8 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -633,7 +633,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; } @@ -1047,7 +1047,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; } @@ -1057,8 +1057,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 833f2db..b632007 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')->value; + } + + /** + * {@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 ec1950c..d5f7e39 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 12d334b..bdb54cb 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 44e147c..8318d63 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/tracker/tracker.module b/core/modules/tracker/tracker.module index c6d2882..ed4bdb2 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -233,7 +233,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) { - _tracker_remove($comment->nid->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_remove($comment->nid->target_id, $comment->getAuthorId(), $comment->changed->value); } } @@ -244,21 +244,21 @@ function tracker_comment_update($comment) { * $comment->save() calls hook_comment_publish() for all published comments. */ function tracker_comment_publish($comment) { - _tracker_add($comment->nid->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_add($comment->nid->target_id, $comment->getAuthorId(), $comment->changed->value); } /** * Implements hook_comment_unpublish(). */ function tracker_comment_unpublish($comment) { - _tracker_remove($comment->nid->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_remove($comment->nid->target_id, $comment->getAuthorId(), $comment->changed->value); } /** * Implements hook_comment_delete(). */ function tracker_comment_delete($comment) { - _tracker_remove($comment->nid->target_id, $comment->uid->target_id, $comment->changed->value); + _tracker_remove($comment->nid->target_id, $comment->getAuthorId(), $comment->changed->value); } /**