X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcomment%2Fsrc%2FEntity%2FComment.php;h=e0745d3da0e3d67b1a798ef646c49dc6aec96f43;hb=1c1cb0980bfa6caf0c24cce671b6bb541dc87583;hp=a5f196646974da48c9e71ba4b35f1cc12d338630;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/modules/comment/src/Entity/Comment.php b/web/core/modules/comment/src/Entity/Comment.php index a5f196646..e0745d3da 100644 --- a/web/core/modules/comment/src/Entity/Comment.php +++ b/web/core/modules/comment/src/Entity/Comment.php @@ -56,7 +56,9 @@ use Drupal\user\UserInterface; * links = { * "canonical" = "/comment/{comment}", * "delete-form" = "/comment/{comment}/delete", + * "delete-multiple-form" = "/admin/content/comment/delete", * "edit-form" = "/comment/{comment}/edit", + * "create" = "/comment", * }, * bundle_entity_type = "comment_type", * field_ui_base_route = "entity.comment_type.edit_form", @@ -72,6 +74,8 @@ class Comment extends ContentEntityBase implements CommentInterface { /** * The thread for which a lock was acquired. + * + * @var string */ protected $threadLock = ''; @@ -81,14 +85,6 @@ class Comment extends ContentEntityBase implements CommentInterface { public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); - if (is_null($this->get('status')->value)) { - if (\Drupal::currentUser()->hasPermission('skip comment approval')) { - $this->setPublished(); - } - else { - $this->setUnpublished(); - } - } if ($this->isNew()) { // Add the comment to database. This next section builds the thread field. // @see \Drupal\comment\CommentViewBuilder::buildComponents() @@ -146,16 +142,15 @@ class Comment extends ContentEntityBase implements CommentInterface { } while (!\Drupal::lock()->acquire($lock_name)); $this->threadLock = $lock_name; } - // We test the value with '===' because we need to modify anonymous - // users as well. - if ($this->getOwnerId() === \Drupal::currentUser()->id() && \Drupal::currentUser()->isAuthenticated()) { - $this->setAuthorName(\Drupal::currentUser()->getUsername()); - } $this->setThread($thread); - if (!$this->getHostname()) { - // Ensure a client host from the current request. - $this->setHostname(\Drupal::request()->getClientIP()); - } + } + // The entity fields for name and mail have no meaning if the user is not + // Anonymous. Set them to NULL to make it clearer that they are not used. + // For anonymous users see \Drupal\comment\CommentForm::form() for mail, + // and \Drupal\comment\CommentForm::buildEntity() for name setting. + if (!$this->getOwner()->isAnonymous()) { + $this->set('name', NULL); + $this->set('mail', NULL); } } @@ -237,6 +232,9 @@ class Comment extends ContentEntityBase implements CommentInterface { $fields['langcode']->setDescription(t('The comment language code.')); + // Set the default value callback for the status field. + $fields['status']->setDefaultValueCallback('Drupal\comment\Entity\Comment::getDefaultStatus'); + $fields['pid'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Parent ID')) ->setDescription(t('The parent comment ID if this is a reply to a comment.')) @@ -289,7 +287,8 @@ class Comment extends ContentEntityBase implements CommentInterface { ->setLabel(t('Hostname')) ->setDescription(t("The comment author's hostname.")) ->setTranslatable(TRUE) - ->setSetting('max_length', 128); + ->setSetting('max_length', 128) + ->setDefaultValueCallback(static::class . '::getDefaultHostname'); $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Created')) @@ -558,4 +557,26 @@ class Comment extends ContentEntityBase implements CommentInterface { return $this->bundle(); } + /** + * Default value callback for 'status' base field definition. + * + * @see ::baseFieldDefinitions() + * + * @return bool + * TRUE if the comment should be published, FALSE otherwise. + */ + public static function getDefaultStatus() { + return \Drupal::currentUser()->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED; + } + + /** + * Returns the default value for entity hostname base field. + * + * @return string + * The client host name. + */ + public static function getDefaultHostname() { + return \Drupal::request()->getClientIP(); + } + }