diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 0f9c7dbd6c..48cd1085d9 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -7,6 +7,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Unicode; use Drupal\Core\Datetime\DrupalDateTime; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityConstraintViolationListInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; @@ -15,6 +16,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -47,6 +49,20 @@ class CommentForm extends ContentEntityForm { */ protected $entityFieldManager; + /** + * The commented entity obtained from route. + * + * @var \Drupal\node\EntityInterface + */ + protected $commentedEntity; + + /** + * The entity type manager. + * + * @var \Drupal\node\EntityInterface + */ + protected $entityTypeManager; + /** * {@inheritdoc} */ @@ -57,7 +73,9 @@ public static function create(ContainerInterface $container) { $container->get('renderer'), $container->get('entity_type.bundle.info'), $container->get('datetime.time'), - $container->get('entity_field.manager') + $container->get('entity_field.manager'), + $container->get('current_route_match'), + $container->get('entity_type.manager') ); } @@ -76,12 +94,32 @@ public static function create(ContainerInterface $container) { * The time service. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager * The entity field manager service. + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The route match. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. */ - public function __construct(EntityRepositoryInterface $entity_repository, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) { + public function __construct(EntityRepositoryInterface $entity_repository, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, EntityFieldManagerInterface $entity_field_manager = NULL, RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager) { parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->currentUser = $current_user; $this->renderer = $renderer; $this->entityFieldManager = $entity_field_manager ?: \Drupal::service('entity_field.manager'); + $this->entityTypeManager = $entity_type_manager ?: \Drupal::service('entity_type_manager'); + + // Get node entity from route match. + $parameters = $route_match->getParameters(); + if ($parameters->has('node')) { + $this->commentedEntity = $parameters->get('node'); + } + elseif ($parameters->has('entity')) { + $this->commentedEntity = $parameters->get('entity'); + } + elseif ($parameters->has('comment')) { + $comment = $parameters->get('comment'); + $this->commentedEntity = $this->entityTypeManager + ->getStorage($comment->getCommentedEntityTypeId()) + ->load($comment->getCommentedEntityId()); + } } /** @@ -119,8 +157,15 @@ 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 (!$comment->id() && !$comment->hasParentComment()) { - $form['#action'] = Url::fromRoute('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name])->toString(); + if (isset($this->commentedEntity) && + $this->commentedEntity->getEntityTypeId() == $entity->getEntityTypeId() && + $this->commentedEntity->id() == $entity->id() && !$comment->id() && + !$comment->hasParentComment()) { + $form['#action'] = Url::fromRoute('comment.reply', [ + 'entity_type' => $entity->getEntityTypeId(), + 'entity' => $entity->id(), + 'field_name' => $field_name, + ])->toString(); } $comment_preview = $form_state->get('comment_preview'); @@ -411,7 +456,11 @@ public function save(array $form, FormStateInterface $form_state) { $this->messenger()->addError($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()])); // Redirect the user to the entity they are commenting on. } - $form_state->setRedirectUrl($uri); + 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/CommentTestBase.php b/core/modules/comment/tests/src/Functional/CommentTestBase.php index 1007a7983a..9247c33313 100644 --- a/core/modules/comment/tests/src/Functional/CommentTestBase.php +++ b/core/modules/comment/tests/src/Functional/CommentTestBase.php @@ -183,6 +183,19 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $ \Drupal::entityTypeManager()->getStorage('comment')->resetCache([$match[1]]); return Comment::load($match[1]); } + else { + $cids = \Drupal::entityQuery('comment') + ->condition('entity_id', $entity->id()) + ->condition('field_name', $field_name) + ->sort('cid', 'DESC') + ->range(0, 1) + ->execute(); + if (!empty($cids)) { + $cid = reset($cids); + \Drupal::entityTypeManager()->getStorage('comment')->resetCache([$cid]); + return Comment::load($cid); + } + } } /**