diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 98e9dde..d032bdb 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -13,7 +13,9 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -38,6 +40,13 @@ class CommentForm extends ContentEntityForm { protected $renderer; /** + * The commented entity obtained from route. + * + * @var \Drupal\node\EntityInterface + */ + protected $commentedEntity; + + /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { @@ -46,7 +55,8 @@ public static function create(ContainerInterface $container) { $container->get('current_user'), $container->get('renderer'), $container->get('entity_type.bundle.info'), - $container->get('datetime.time') + $container->get('datetime.time'), + $container->get('current_route_match') ); } @@ -63,11 +73,27 @@ public static function create(ContainerInterface $container) { * The entity type bundle service. * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The route match. */ - public function __construct(EntityManagerInterface $entity_manager, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { + public function __construct(EntityManagerInterface $entity_manager, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, RouteMatchInterface $route_match) { parent::__construct($entity_manager, $entity_type_bundle_info, $time); $this->currentUser = $current_user; $this->renderer = $renderer; + // 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->entityManager + ->getStorage($comment->getCommentedEntityTypeId()) + ->load($comment->getCommentedEntityId()); + } } /** @@ -105,8 +131,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'] = $this->url('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name]); + 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(), + 'entity' => $entity->id(), + 'field_name' => $field_name, + ]); } $comment_preview = $form_state->get('comment_preview'); @@ -397,7 +430,11 @@ 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. } - $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/CommentBlockContentTest.php b/core/modules/comment/tests/src/Functional/CommentBlockContentTest.php new file mode 100644 index 0000000..1272d06 --- /dev/null +++ b/core/modules/comment/tests/src/Functional/CommentBlockContentTest.php @@ -0,0 +1,110 @@ + 'comments', + 'label' => 'Comments', + ]); + $type->save(); + block_content_add_body_field($type->id()); + CommentType::create([ + 'id' => 'block_content_comment_type', + 'label' => 'Comment settings', + 'description' => 'Comment settings', + 'target_entity_type_id' => 'block_content', + ])->save(); + // Create comment field on block_content bundle. + $this->addDefaultCommentField('block_content', 'comments'); + + // Create test user. + $this->adminUser = $this->drupalCreateUser(array( + 'administer comments', + 'skip comment approval', + 'post comments', + 'access comments', + 'administer blocks', + )); + + // Enable anonymous and authenticated user comments. + user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array( + 'access comments', + 'post comments', + 'skip comment approval', + )); + + // Create a block and place it. + $this->drupalLogin($this->adminUser); + $edit = array(); + $edit['info[0][value]'] = $this->randomMachineName(8); + $body = $this->randomMachineName(16); + $edit['body[0][value]'] = $body; + $this->drupalPostForm('block/add', $edit, t('Save')); + + // Place the block. + $instance = array( + 'id' => Unicode::strtolower($edit['info[0][value]']), + 'settings[label]' => $edit['info[0][value]'], + 'region' => 'sidebar_first', + ); + $block = BlockContent::load(1); + $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default'); + $this->drupalPostForm($url, $instance, t('Save block')); + $this->drupalLogout(); + } + + /** + * Tests anonymous commenting via a block. + */ + public function testAnonymousBlockContentCommenting() { + // Navigate to home page. + $this->drupalGet(''); + // Comment on the block. + $edit = []; + $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')); + $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->addressEquals(Url::fromRoute('user.login')); + } + +}