diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index a8fc4c1..d032bdb 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -40,11 +40,11 @@ class CommentForm extends ContentEntityForm { protected $renderer; /** - * The current entity. + * The commented entity obtained from route. * * @var \Drupal\node\EntityInterface */ - protected $currentEntity; + protected $commentedEntity; /** * {@inheritdoc} @@ -83,10 +83,16 @@ public function __construct(EntityManagerInterface $entity_manager, AccountInter // Get node entity from route match. $parameters = $route_match->getParameters(); if ($parameters->has('node')) { - $this->currentEntity = $parameters->get('node'); + $this->commentedEntity = $parameters->get('node'); } elseif ($parameters->has('entity')) { - $this->currentEntity = $parameters->get('entity'); + $this->commentedEntity = $parameters->get('entity'); + } + elseif ($parameters->has('comment')) { + $comment = $parameters->get('comment'); + $this->commentedEntity = $this->entityManager + ->getStorage($comment->getCommentedEntityTypeId()) + ->load($comment->getCommentedEntityId()); } } @@ -125,9 +131,9 @@ public function form(array $form, FormStateInterface $form_state) { // If not replying to a comment, use our dedicated page callback for new // Comments on entities. - if (isset($this->currentEntity) && - $this->currentEntity->getEntityTypeId() == $entity->getEntityTypeId() && - $this->currentEntity->id() == $entity->id() && !$comment->id() && + if (isset($this->commentedEntity) && + $this->commentedEntity->getEntityTypeId() == $entity->getEntityTypeId() && + $this->commentedEntity->id() == $entity->id() && !$comment->id() && !$comment->hasParentComment()) { $form['#action'] = $this->url('comment.reply', [ 'entity_type' => $entity->getEntityTypeId(), @@ -424,9 +430,9 @@ public function save(array $form, FormStateInterface $form_state) { drupal_set_message($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]), 'error'); // Redirect the user to the entity they are commenting on. } - if (isset($this->currentEntity) && - $this->currentEntity->getEntityTypeId() == $entity->getEntityTypeId() && - $this->currentEntity->id() == $entity->id() && $uri->access()) { + if (isset($this->commentedEntity) && + $this->commentedEntity->getEntityTypeId() == $entity->getEntityTypeId() && + $this->commentedEntity->id() == $entity->id() && $uri->access()) { $form_state->setRedirectUrl($uri); } } diff --git a/core/modules/comment/tests/src/Functional/CommentBlockContentTest.php b/core/modules/comment/tests/src/Functional/CommentBlockContentTest.php index de4ac15..1272d06 100644 --- a/core/modules/comment/tests/src/Functional/CommentBlockContentTest.php +++ b/core/modules/comment/tests/src/Functional/CommentBlockContentTest.php @@ -7,14 +7,18 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Url; use Drupal\comment\Entity\CommentType; +use Drupal\comment\Tests\CommentTestTrait; use Drupal\user\RoleInterface; +use Drupal\Tests\BrowserTestBase; /** * Tests use of comment field on entity-type without a canonical path. * * @group comment */ -class CommentBlockContentTest extends CommentTestBase { +class CommentBlockContentTest extends BrowserTestBase { + + use CommentTestTrait; /** * Modules to install.