diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 5bdee72..aa37668 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -232,14 +232,6 @@ function comment_menu() { 'file' => 'comment.admin.inc', 'weight' => 20, ); - $items['comment/reply/%node'] = array( - 'title' => 'Add new comment', - 'page callback' => 'comment_reply', - 'page arguments' => array(2), - 'access callback' => 'node_access', - 'access arguments' => array('view', 2), - 'file' => 'comment.pages.inc', - ); return $items; } diff --git a/core/modules/comment/comment.pages.inc b/core/modules/comment/comment.pages.inc deleted file mode 100644 index 221fbfc..0000000 --- a/core/modules/comment/comment.pages.inc +++ /dev/null @@ -1,101 +0,0 @@ -label(), 'node/' . $node->id()))); - $op = Drupal::request()->request->get('op'); - $build = array(); - - // The user is previewing a comment prior to submitting it. - if ($op == t('Preview')) { - if (user_access('post comments')) { - $build['comment_form'] = comment_add($node, $pid); - } - else { - drupal_set_message(t('You are not authorized to post comments.'), 'error'); - return new RedirectResponse(url('node/' . $node->id(), array('absolute' => TRUE))); - } - } - else { - // $pid indicates that this is a reply to a comment. - if ($pid) { - if (user_access('access comments')) { - // Load the parent comment. - $comment = comment_load($pid); - if ($comment->status->value == COMMENT_PUBLISHED) { - // If that comment exists, make sure that the current comment and the - // parent comment both belong to the same parent node. - if ($comment->nid->target_id != $node->id()) { - // Attempting to reply to a comment not belonging to the current nid. - drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); - return new RedirectResponse(url('node/' . $node->id(), array('absolute' => TRUE))); - } - // Display the parent comment - $build['comment_parent'] = comment_view($comment); - } - else { - drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); - return new RedirectResponse(url('node/' . $node->id(), array('absolute' => TRUE))); - } - } - else { - drupal_set_message(t('You are not authorized to view comments.'), 'error'); - return new RedirectResponse(url('node/' . $node->id(), array('absolute' => TRUE))); - } - } - // This is the case where the comment is in response to a node. Display the node. - elseif (user_access('access content')) { - $build['comment_node'] = node_view($node); - } - - // Should we show the reply box? - if ($node->comment != COMMENT_NODE_OPEN) { - drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error'); - return new RedirectResponse(url('node/' . $node->id(), array('absolute' => TRUE))); - } - elseif (user_access('post comments')) { - $build['comment_form'] = comment_add($node, $pid); - } - else { - drupal_set_message(t('You are not authorized to post comments.'), 'error'); - return new RedirectResponse(url('node/' . $node->id(), array('absolute' => TRUE))); - } - } - - return $build; -} diff --git a/core/modules/comment/comment.routing.yml b/core/modules/comment/comment.routing.yml index b62c118..fc420d9 100644 --- a/core/modules/comment/comment.routing.yml +++ b/core/modules/comment/comment.routing.yml @@ -19,3 +19,11 @@ comment_permalink: _controller: '\Drupal\comment\Controller\CommentController::commentPermalink' requirements: _entity_access: 'comment.view' + +comment_reply: + pattern: 'comment/reply/{node}/{pid}' + defaults: + _content: '\Drupal\comment\Controller\CommentController::getReplyForm' + pid: ~ + requirements: + _entity_access: 'node.view' diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml new file mode 100644 index 0000000..3599592 --- /dev/null +++ b/core/modules/comment/comment.services.yml @@ -0,0 +1,5 @@ +services: + comment.breadcrumb: + class: Drupal\comment\CommentBreadcrumbBuilder + tags: + - { name: breadcrumb_builder, priority: 100 } diff --git a/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php b/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php new file mode 100644 index 0000000..fcaf24a --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php @@ -0,0 +1,30 @@ +uri(); + $breadcrumb[] = l(t('Home'), NULL); + $breadcrumb[] = l($node->label(), $uri['path']); + return $breadcrumb; + } + } + +} diff --git a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php index 84ef0c1..d3a1411 100644 --- a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php +++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php @@ -10,7 +10,9 @@ use Drupal\comment\CommentInterface; use Drupal\comment\Plugin\Core\Entity\Comment; use Drupal\Core\Controller\ControllerInterface; +use Drupal\Core\Entity\EntityManager; use Drupal\Core\Routing\PathBasedGeneratorInterface; +use Drupal\Node\NodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -26,6 +28,13 @@ class CommentController implements ControllerInterface { /** + * The entity manager service. + * + * @var \Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + + /** * The url generator service. * * @var \Drupal\Core\Routing\PathBasedGeneratorInterface @@ -42,12 +51,15 @@ class CommentController implements ControllerInterface { /** * Constructs a CommentController object. * + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The entity manager. * @param \Drupal\Core\Routing\PathBasedGeneratorInterface $url_generator * The url generator service. * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel * HTTP kernel to handle requests. */ - public function __construct(PathBasedGeneratorInterface $url_generator, HttpKernelInterface $httpKernel) { + public function __construct(EntityManager $entity_manager, PathBasedGeneratorInterface $url_generator, HttpKernelInterface $httpKernel) { + $this->entityManager = $entity_manager; $this->urlGenerator = $url_generator; $this->httpKernel = $httpKernel; } @@ -56,6 +68,7 @@ public function __construct(PathBasedGeneratorInterface $url_generator, HttpKern */ public static function create(ContainerInterface $container) { return new static( + $container->get('plugin.manager.entity'), $container->get('url_generator'), $container->get('http_kernel') ); @@ -132,4 +145,90 @@ public function commentPermalink(Request $request, CommentInterface $comment) { throw new NotFoundHttpException(); } + /** + * Form constructor for the comment reply form. + * + * Both replies on the node itself and replies on other comments are + * supported. To provide context, the node or comment that is being replied on + * will be displayed along the comment reply form. + * The constructor takes care of access permissions and checks whether the + * node still accepts comments. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The current request object. + * @param \Drupal\node\NodeInterface $node + * Every comment belongs to a node. This is that node. + * @param int $pid + * (optional) Some comments are replies to other comments. In those cases, + * $pid is the parent comment's ID. Defaults to NULL. + * + * @return array|\Symfony\Component\HttpFoundation\RedirectResponse + * One of the following: + * - An associative array containing: + * - An array for rendering the node or parent comment. + * - comment_node: If the comment is a reply to the node. + * - comment_parent: If the comment is a reply to another comment. + * - comment_form: The comment form as a renderable array. + * - A redirect response to current node: + * - If user is not authorized to post comments. + * - If parent comment doesn't belong to current node. + * - If user is not authorized to view comments. + * - If current node comments are disable. + */ + public function getReplyForm(Request $request, NodeInterface $node, $pid = NULL) { + $uri = $node->uri(); + $build = array(); + $account = $request->attributes->get('account'); + + drupal_set_title(t('Add new comment')); + + // Check if the user has the proper permissions. + if (!$account->hasPermission('post comments')) { + drupal_set_message(t('You are not authorized to post comments.'), 'error'); + return new RedirectResponse($this->urlGenerator->generateFromPath($uri['path'], array('absolute' => TRUE))); + } + + // The user is not just previewing a comment. + if ($request->request->get('op') != t('Preview')) { + if ($node->comment != COMMENT_NODE_OPEN) { + drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error'); + return new RedirectResponse($this->urlGenerator->generateFromPath($uri['path'], array('absolute' => TRUE))); + } + + // $pid indicates that this is a reply to a comment. + if ($pid) { + // Check if the user has the proper permissions. + if (!$account->hasPermission('access comments')) { + drupal_set_message(t('You are not authorized to view comments.'), 'error'); + return new RedirectResponse($this->urlGenerator->generateFromPath($uri['path'], array('absolute' => TRUE))); + } + // Load the parent comment. + $comment = $this->entityManager->getStorageController('comment')->load($pid); + // Check if the parent comment is published and belongs to the current nid. + if (($comment->status->value != COMMENT_PUBLISHED) || ($comment->nid->target_id != $node->id())) { + drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); + return new RedirectResponse($this->urlGenerator->generateFromPath($uri['path'], array('absolute' => TRUE))); + } + // Display the parent comment. + $build['comment_parent'] = $this->entityManager->getRenderController('comment')->view($comment); + } + + // The comment is in response to a node. + elseif ($account->hasPermission('access content')) { + // Display the node. + $build['comment_node'] = $this->entityManager->getRenderController('node')->view($node); + } + } + + // Show the actual reply box. + $comment = $this->entityManager->getStorageController('comment')->create(array( + 'nid' => $node->id(), + 'pid' => $pid, + 'node_type' => 'comment_node_' . $node->bundle(), + )); + $build['comment_form'] = $this->entityManager->getForm($comment); + + return $build; + } + }