Issue #2398075 by andypost, sarav.din33, larowlan, nemethf, hussainweb: Breadcrumbs for comment entity

8.0.x
Alex Pott 2015-02-16 22:43:15 +00:00
parent 0e1fe4c234
commit 3edf291326
5 changed files with 69 additions and 6 deletions

View File

@ -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'

View File

@ -1,6 +1,7 @@
services:
comment.breadcrumb:
class: Drupal\comment\CommentBreadcrumbBuilder
arguments: ['@entity.manager']
tags:
- { name: breadcrumb_builder, priority: 100 }

View File

@ -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;
@ -18,6 +19,23 @@ use Drupal\Core\StringTranslation\StringTranslationTrait;
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}
*/
@ -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;
}

View File

@ -141,6 +141,19 @@ class CommentController extends ControllerBase {
throw new NotFoundHttpException();
}
/**
* 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.
*

View File

@ -35,6 +35,13 @@ class CommentNonNodeTest extends WebTestBase {
*/
protected $adminUser;
/**
* The entity to use within tests.
*
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected $entity;
/**
* {@inheritdoc}
*/
@ -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');