diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 136b318..3d2fab1 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -17,8 +17,10 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -47,7 +49,8 @@ public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), $container->get('current_user'), - $container->get('renderer') + $container->get('renderer'), + $container->get('path.current'), ); } @@ -60,11 +63,14 @@ public static function create(ContainerInterface $container) { * The current user. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. + * @param \Drupal\Core\Path\CurrentPathStack $current_path + * Current path service. */ - public function __construct(EntityManagerInterface $entity_manager, AccountInterface $current_user, RendererInterface $renderer) { + public function __construct(EntityManagerInterface $entity_manager, AccountInterface $current_user, RendererInterface $renderer, CurrentPathStack $current_path) { parent::__construct($entity_manager); $this->currentUser = $current_user; $this->renderer = $renderer; + $this->currentPath = $current_path; } /** @@ -74,6 +80,7 @@ public function form(array $form, FormStateInterface $form_state) { /** @var \Drupal\comment\CommentInterface $comment */ $comment = $this->entity; $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); + $field_name = $comment->getFieldName(); $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; $config = $this->config('user.settings'); @@ -102,7 +109,7 @@ 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()) { + if ($entity->url() == $this->currentPath->getPath() && !$comment->id() && !$comment->hasParentComment()) { $form['#action'] = $this->url('comment.reply', array('entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name)); } @@ -382,8 +389,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.', array('%subject' => $comment->getSubject())), 'error'); // Redirect the user to the entity they are commenting on. } - if ($uri->access()) { + if ($entity->url() == $this->currentPath->getPath() && $uri->access()) { $form_state->setRedirectUrl($uri); } } + } diff --git a/core/modules/comment/src/Tests/CommentBlockContentTest.php b/core/modules/comment/src/Tests/CommentBlockContentTest.php index b791f64..7f0481c 100644 --- a/core/modules/comment/src/Tests/CommentBlockContentTest.php +++ b/core/modules/comment/src/Tests/CommentBlockContentTest.php @@ -17,7 +17,7 @@ /** * Tests use of comment field on entity-type without a canonical path. - * @group Comment + * @group comment */ class CommentBlockContentTest extends WebTestBase { @@ -101,9 +101,8 @@ public function testAnonymousBlockContentCommenting() { $edit['comment_body[0][value]'] = 'Noni the pony is skinny and bony'; $edit['subject[0][value]'] = 'Oh no, why does it go?'; $this->drupalPostForm(NULL, $edit, t('Save')); - // User without permission to visit block/{block} doesn't get redirected. $this->assertResponse(200); - $this->assertUrl(Url::fromRoute('comment.reply', ['entity_type' => 'block_content', 'entity' => 1, 'field_name' => 'comment'])); + $this->assertUrl(Url::fromRoute('')); } }