summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-02-16 22:43:15 (GMT)
committerAlex Pott2015-02-16 22:43:15 (GMT)
commit3edf291326d76b7ad4231ef3a5386c8f3510c1b7 (patch)
tree98d8149312dcb0bc35df80690e48f36fd02d228f
parent0e1fe4c2340ce3370e4c4b589a974ca22085a2d2 (diff)
Issue #2398075 by andypost, sarav.din33, larowlan, nemethf, hussainweb: Breadcrumbs for comment entity
-rw-r--r--core/modules/comment/comment.routing.yml2
-rw-r--r--core/modules/comment/comment.services.yml1
-rw-r--r--core/modules/comment/src/CommentBreadcrumbBuilder.php29
-rw-r--r--core/modules/comment/src/Controller/CommentController.php13
-rw-r--r--core/modules/comment/src/Tests/CommentNonNodeTest.php30
5 files changed, 69 insertions, 6 deletions
diff --git a/core/modules/comment/comment.routing.yml b/core/modules/comment/comment.routing.yml
index 4799400..fea8cc1 100644
--- a/core/modules/comment/comment.routing.yml
+++ b/core/modules/comment/comment.routing.yml
@@ -37,7 +37,7 @@ comment.approve:
entity.comment.canonical:
path: '/comment/{comment}'
defaults:
- _title: 'Comment permalink'
+ _title_callback: '\Drupal\comment\Controller\CommentController::commentPermalinkTitle'
_controller: '\Drupal\comment\Controller\CommentController::commentPermalink'
requirements:
_entity_access: 'comment.view'
diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml
index 239cdb2..7bb901c 100644
--- a/core/modules/comment/comment.services.yml
+++ b/core/modules/comment/comment.services.yml
@@ -1,6 +1,7 @@
services:
comment.breadcrumb:
class: Drupal\comment\CommentBreadcrumbBuilder
+ arguments: ['@entity.manager']
tags:
- { name: breadcrumb_builder, priority: 100 }
diff --git a/core/modules/comment/src/CommentBreadcrumbBuilder.php b/core/modules/comment/src/CommentBreadcrumbBuilder.php
index 1ec574d..8bc2f25 100644
--- a/core/modules/comment/src/CommentBreadcrumbBuilder.php
+++ b/core/modules/comment/src/CommentBreadcrumbBuilder.php
@@ -8,6 +8,7 @@
namespace Drupal\comment;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
+use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -19,6 +20,23 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
+ * The comment storage.
+ *
+ * @var \Drupal\Core\Entity\EntityStorageInterface
+ */
+ protected $storage;
+
+ /**
+ * Constructs the CommentBreadcrumbBuilder.
+ *
+ * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
+ * The entity manager.
+ */
+ public function __construct(EntityManagerInterface $entity_manager) {
+ $this->storage = $entity_manager->getStorage('comment');
+ }
+
+ /**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
@@ -29,11 +47,18 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
- $breadcrumb = array();
+ $breadcrumb = [Link::createFromRoute($this->t('Home'), '<front>')];
- $breadcrumb[] = Link::createFromRoute($this->t('Home'), '<front>');
$entity = $route_match->getParameter('entity');
$breadcrumb[] = new Link($entity->label(), $entity->urlInfo());
+
+ if (($pid = $route_match->getParameter('pid')) && ($comment = $this->storage->load($pid))) {
+ /** @var \Drupal\comment\CommentInterface $comment */
+ // Display link to parent comment.
+ // @todo Clean-up permalink in https://www.drupal.org/node/2198041
+ $breadcrumb[] = new Link($comment->getSubject(), $comment->urlInfo());
+ }
+
return $breadcrumb;
}
diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php
index 69be36a..05baad8 100644
--- a/core/modules/comment/src/Controller/CommentController.php
+++ b/core/modules/comment/src/Controller/CommentController.php
@@ -142,6 +142,19 @@ class CommentController extends ControllerBase {
}
/**
+ * The _title_callback for the page that renders the comment permalink.
+ *
+ * @param \Drupal\comment\CommentInterface $comment
+ * The current comment.
+ *
+ * @return string
+ * The translated comment subject.
+ */
+ public function commentPermalinkTitle(CommentInterface $comment) {
+ return $this->entityManager()->getTranslationFromContext($comment)->label();
+ }
+
+ /**
* Redirects legacy node links to the new path.
*
* @param \Drupal\Core\Entity\EntityInterface $node
diff --git a/core/modules/comment/src/Tests/CommentNonNodeTest.php b/core/modules/comment/src/Tests/CommentNonNodeTest.php
index 73fa8e2..97dd9cc 100644
--- a/core/modules/comment/src/Tests/CommentNonNodeTest.php
+++ b/core/modules/comment/src/Tests/CommentNonNodeTest.php
@@ -36,6 +36,13 @@ class CommentNonNodeTest extends WebTestBase {
protected $adminUser;
/**
+ * The entity to use within tests.
+ *
+ * @var \Drupal\entity_test\Entity\EntityTest
+ */
+ protected $entity;
+
+ /**
* {@inheritdoc}
*/
protected function setUp() {
@@ -261,13 +268,30 @@ class CommentNonNodeTest extends WebTestBase {
$this->drupalLogin($this->adminUser);
+ // Test breadcrumb on comment add page.
+ $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
+ $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
+ $this->assertEqual(current($this->xpath($xpath)), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.');
+
// Post a comment.
+ /** @var \Drupal\comment\CommentInterface $comment1 */
$comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
- // Assert the breadcrumb is valid.
- $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
- $this->assertLink($this->entity->label());
+ // Test breadcrumb on comment reply page.
+ $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
+ $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
+ $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.');
+
+ // Test breadcrumb on comment edit page.
+ $this->drupalGet('comment/' . $comment1->id() . '/edit');
+ $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
+ $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.');
+
+ // Test breadcrumb on comment delete page.
+ $this->drupalGet('comment/' . $comment1->id() . '/delete');
+ $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
+ $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.');
// Unpublish the comment.
$this->performCommentOperation($comment1, 'unpublish');