diff --git a/core/modules/comment/lib/Drupal/comment/Controller/CommentPageController.php b/core/modules/comment/lib/Drupal/comment/Controller/CommentPageController.php index 546d06d..19fc4ad 100644 --- a/core/modules/comment/lib/Drupal/comment/Controller/CommentPageController.php +++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentPageController.php @@ -7,12 +7,50 @@ namespace Drupal\comment\Controller; +use Drupal\Core\Controller\ControllerInterface; +use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Routing\PathBasedGeneratorInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** * Controller for comment forms. */ -class CommentPageController { +class CommentPageController implements ControllerInterface { + + /** + * The entity manager service. + * + * @var \Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + + /** + * The url generator service. + * + * @var \Drupal\Core\Routing\UrlGenerator + */ + protected $urlGenerator; + + /** + * Constructs a CommentPageController object. + * + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The entity manager. + * @param \Drupal\Core\Routing\UrlGenerator $url_generator + * The url generator. + */ + public function __construct(EntityManager $entity_manager, PathBasedGeneratorInterface $url_generator) { + $this->entityManager = $entity_manager; + $this->urlGenerator = $url_generator; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static($container->get('plugin.manager.entity'), $container->get('url_generator')); + } /** * Form constructor for the comment reply form. @@ -26,7 +64,7 @@ class CommentPageController { * The node or comment that is being replied to must appear above the comment * form to provide the user context while authoring the comment. * - * @param \Drupal\Core\Entity\EntityInterface $node + * @param \Drupal\node\NodeInterface $node * Every comment belongs to a node. This is that node. * @param $pid * (optional) Some comments are replies to other comments. In those cases, @@ -42,21 +80,25 @@ class CommentPageController { * - comment_parent: If the comment is a reply to another comment. * - comment_form: The comment form as a renderable array. */ - function getReply(EntityInterface $node, $pid, Request $request) { + function getReply(NodeInterface $node, $pid, Request $request) { // Set the breadcrumb trail. drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->label(), 'node/' . $node->id()))); - $op = \Drupal::request()->request->get('op'); - $op = ($op ? $op : ''); + $op = $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); + $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); } else { drupal_set_message(t('You are not authorized to post comments.'), 'error'); - return RedirectResponse::create("node/$node->id()"); + return new RedirectResponse($this->urlGenerator->generateFromPath($node->uri(), array('absolute' => TRUE))); } } @@ -65,44 +107,50 @@ function getReply(EntityInterface $node, $pid, Request $request) { if ($pid) { if (user_access('access comments')) { // Load the parent comment. - $comment = comment_load($pid); + $comments = $this->entityManager->getStorageController('comment')->load($pid); + $comment = reset($comments); 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 RedirectResponse::create("node/$node->id()"); + return new RedirectResponse($this->urlGenerator->generateFromPath($node->uri(), array('absolute' => TRUE))); } // Display the parent comment - $build['comment_parent'] = comment_view($comment); + $build['comment_parent'] = $this->entityManager->getRenderController('comment')->view($comment); } else { drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); - return RedirectResponse::create("node/$node->id()"); + return new RedirectResponse($this->urlGenerator->generateFromPath($node->uri(), array('absolute' => TRUE))); } } else { drupal_set_message(t('You are not authorized to view comments.'), 'error'); - return RedirectResponse::create("node/$node->id()"); + return new RedirectResponse($this->urlGenerator->generateFromPath($node->uri(), 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); + $build['comment_node'] = $this->entityManager->getRenderController('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 RedirectResponse::create("node/$node->id()"); + return new RedirectResponse($this->urlGenerator->generateFromPath($node->uri(), array('absolute' => TRUE))); } elseif (user_access('post comments')) { - $build['comment_form'] = comment_add($node, $pid); + $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); } else { drupal_set_message(t('You are not authorized to post comments.'), 'error'); - return RedirectResponse::create("node/$node->id()"); + return new RedirectResponse($this->urlGenerator->generateFromPath($node->uri(), array('absolute' => TRUE))); } } return $build;