Commit b9ed957d authored by catch's avatar catch

Issue #2099095 by jibran, tim.plunkett: Create BreadcrumbBuilderBase and...

Issue #2099095 by jibran, tim.plunkett: Create BreadcrumbBuilderBase and remove the boilerplate code from BreadcrumbBuilders.
parent ea41ae25
<?php
/**
* @file
* Contains \Drupal\Core\Breadcrumb\BreadcrumbBuilderBase.
*/
namespace Drupal\Core\Breadcrumb;
/**
* Defines a common base class for breadcrumb builders adding a link generator.
*
* @todo Use traits once we have a PHP 5.4 requirement.
*/
abstract class BreadcrumbBuilderBase implements BreadcrumbBuilderInterface {
/**
* The link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected $linkGenerator;
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* Returns the service container.
*
* @return \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container.
*/
protected function container() {
return \Drupal::getContainer();
}
/**
* Renders a link to a route given a route name and its parameters.
*
* @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
* on the arguments, usage, and possible exceptions.
*
* @return string
* An HTML string containing a link to the given route and parameters.
*/
protected function l($text, $route_name, array $parameters = array(), array $options = array()) {
return $this->linkGenerator()->generate($text, $route_name, $parameters, $options);
}
/**
* Returns the link generator.
*
* @return \Drupal\Core\Utility\LinkGeneratorInterface
* The link generator
*/
protected function linkGenerator() {
if (!isset($this->linkGenerator)) {
$this->linkGenerator = $this->container()->get('link_generator');
}
return $this->linkGenerator;
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translationManager()->translate($string, $args, $options);
}
/**
* Returns the translation manager.
*
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function translationManager() {
if (!$this->translationManager) {
$this->translationManager = $this->container()->get('string_translation');
}
return $this->translationManager;
}
}
services:
book.breadcrumb:
class: Drupal\book\BookBreadcrumbBuilder
arguments: ['@entity.manager', '@string_translation', '@link_generator', '@access_manager']
arguments: ['@entity.manager', '@access_manager']
tags:
- { name: breadcrumb_builder, priority: 701 }
book.manager:
......
......@@ -8,16 +8,14 @@
namespace Drupal\book;
use Drupal\Core\Access\AccessManager;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderBase;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\node\NodeInterface;
/**
* Provides a breadcrumb builder for nodes in a book.
*/
class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
class BookBreadcrumbBuilder extends BreadcrumbBuilderBase {
/**
* The menu link storage controller.
......@@ -26,20 +24,6 @@ class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
protected $menuLinkStorage;
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface;
*/
protected $translation;
/**
* The link generator service.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected $linkGenerator;
/**
* The access manager.
*
......@@ -52,17 +36,11 @@ class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The translation manager service.
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generator.
* @param \Drupal\Core\Access\AccessManager $access_manager
* The access manager.
*/
public function __construct(EntityManager $entity_manager, TranslationInterface $translation, LinkGeneratorInterface $link_generator, AccessManager $access_manager) {
public function __construct(EntityManager $entity_manager, AccessManager $access_manager) {
$this->menuLinkStorage = $entity_manager->getStorageController('menu_link');
$this->translation = $translation;
$this->linkGenerator = $link_generator;
$this->accessManager = $access_manager;
}
......@@ -72,7 +50,7 @@ public function __construct(EntityManager $entity_manager, TranslationInterface
public function build(array $attributes) {
if (!empty($attributes['node']) && $attributes['node'] instanceof NodeInterface && !empty($attributes['node']->book)) {
$mlids = array();
$links = array($this->linkGenerator->generate($this->t('Home'), '<front>'));
$links = array($this->l($this->t('Home'), '<front>'));
$book = $attributes['node']->book;
$depth = 1;
// We skip the current node.
......@@ -86,7 +64,7 @@ public function build(array $attributes) {
while (!empty($book['p' . ($depth + 1)])) {
if (!empty($menu_links[$book['p' . $depth]]) && ($menu_link = $menu_links[$book['p' . $depth]])) {
if ($this->accessManager->checkNamedRoute($menu_link->route_name, $menu_link->route_parameters)) {
$links[] = $this->linkGenerator->generate($menu_link->label(), $menu_link->route_name, $menu_link->route_parameters, $menu_link->options);
$links[] = $this->l($menu_link->label(), $menu_link->route_name, $menu_link->route_parameters, $menu_link->options);
}
}
$depth++;
......@@ -96,13 +74,4 @@ public function build(array $attributes) {
}
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translation->translate($string, $args, $options);
}
}
......@@ -3,7 +3,7 @@ services:
class: Drupal\comment\CommentBreadcrumbBuilder
tags:
- { name: breadcrumb_builder, priority: 100 }
arguments: ['@string_translation', '@entity.manager']
arguments: ['@entity.manager']
comment.manager:
class: Drupal\comment\CommentManager
......
......@@ -7,22 +7,14 @@
namespace Drupal\comment;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderBase;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\StringTranslation\TranslationManager;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
/**
* Class to define the comment breadcrumb builder.
*/
class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $translation;
class CommentBreadcrumbBuilder extends BreadcrumbBuilderBase {
/**
* Stores the Entity manager service.
......@@ -34,13 +26,10 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
/**
* Constructs a CommentBreadcrumbBuilder object.
*
* @param \Drupal\Core\StringTranslation\TranslationManager $translation
* The translation manager.
* @param \Drupal\Core\Entity\EntityManager
* The entity manager.
*/
public function __construct(TranslationManager $translation, EntityManager $entity_manager) {
$this->translation = $translation;
public function __construct(EntityManager $entity_manager) {
$this->entityManager = $entity_manager;
}
......@@ -53,7 +42,7 @@ public function build(array $attributes) {
&& isset($attributes['entity_id'])
&& isset($attributes['field_name'])
) {
$breadcrumb[] = l($this->t('Home'), NULL);
$breadcrumb[] = $this->l($this->t('Home'), '<front>');
$entity = $this->entityManager
->getStorageController($attributes['entity_type'])
->load($attributes['entity_id']);
......@@ -63,13 +52,4 @@ public function build(array $attributes) {
}
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translation->translate($string, $args, $options);
}
}
......@@ -7,7 +7,7 @@
namespace Drupal\forum;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderBase;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityManager;
use Drupal\forum\ForumManagerInterface;
......@@ -16,7 +16,7 @@
/**
* Class to define the forum breadcrumb builder.
*/
class ForumBreadcrumbBuilder implements BreadcrumbBuilderInterface {
class ForumBreadcrumbBuilder extends BreadcrumbBuilderBase {
/**
* Configuration object for this builder.
......@@ -78,12 +78,12 @@ public function build(array $attributes) {
protected function forumPostBreadcrumb($node) {
$vocabulary = $this->entityManager->getStorageController('taxonomy_vocabulary')->load($this->config->get('vocabulary'));
$breadcrumb[] = l(t('Home'), NULL);
$breadcrumb[] = $this->l($this->t('Home'), '<front>');
$breadcrumb[] = l($vocabulary->label(), 'forum');
if ($parents = taxonomy_term_load_parents_all($node->forum_tid)) {
$parents = array_reverse($parents);
foreach ($parents as $parent) {
$breadcrumb[] = l($parent->label(), 'forum/' . $parent->id());
$breadcrumb[] = $this->l($parent->label(), 'forum.page', array('taxonomy_term' => $parent->id()));
}
}
return $breadcrumb;
......@@ -95,16 +95,16 @@ protected function forumPostBreadcrumb($node) {
protected function forumTermBreadcrumb($term) {
$vocabulary = $this->entityManager->getStorageController('taxonomy_vocabulary')->load($this->config->get('vocabulary'));
$breadcrumb[] = l(t('Home'), NULL);
$breadcrumb[] = $this->l($this->t('Home'), '<front>');
if ($term->tid) {
// Parent of all forums is the vocabulary name.
$breadcrumb[] = l($vocabulary->label(), 'forum');
$breadcrumb[] = $this->l($vocabulary->label(), 'forum.index');
}
// Add all parent forums to breadcrumbs.
if ($term->parents) {
foreach (array_reverse($term->parents) as $parent) {
if ($parent->id() != $term->id()) {
$breadcrumb[] = l($parent->label(), 'forum/' . $parent->id());
$breadcrumb[] = $this->l($parent->label(), 'forum.page', array('taxonomy_term' => $parent->id()));
}
}
}
......
......@@ -7,16 +7,13 @@
namespace Drupal\system;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderBase;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Routing\RequestHelper;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Access\AccessManager;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
......@@ -26,7 +23,7 @@
/**
* Class to define the menu_link breadcrumb builder.
*/
class PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
class PathBasedBreadcrumbBuilder extends BreadcrumbBuilderBase {
/**
* The current request.
......@@ -42,13 +39,6 @@ class PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
protected $accessManager;
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface;
*/
protected $translation;
/**
* The menu storage controller.
*
......@@ -70,13 +60,6 @@ class PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
protected $pathProcessor;
/**
* The link generator service.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected $linkGenerator;
/**
* Site config object.
*
......@@ -101,28 +84,22 @@ class PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
* The entity manager service.
* @param \Drupal\Core\Access\AccessManager $access_manager
* The menu link access service.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The translation manager service.
* @param \Symfony\Component\Routing\Matcher\RequestMatcherInterface $router
* The dynamic router service.
* @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $path_processor
* The inbound path processor.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory service.
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generator.
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* The title resolver service.
*/
public function __construct(Request $request, EntityManager $entity_manager, AccessManager $access_manager, TranslationInterface $translation, RequestMatcherInterface $router, InboundPathProcessorInterface $path_processor, ConfigFactory $config_factory, LinkGeneratorInterface $link_generator, TitleResolverInterface $title_resolver) {
public function __construct(Request $request, EntityManager $entity_manager, AccessManager $access_manager, RequestMatcherInterface $router, InboundPathProcessorInterface $path_processor, ConfigFactory $config_factory, TitleResolverInterface $title_resolver) {
$this->request = $request;
$this->accessManager = $access_manager;
$this->translation = $translation;
$this->menuStorage = $entity_manager->getStorageController('menu');
$this->router = $router;
$this->pathProcessor = $path_processor;
$this->config = $config_factory->get('system.site');
$this->linkGenerator = $link_generator;
$this->titleResolver = $title_resolver;
}
......@@ -184,7 +161,7 @@ public function build(array $attributes) {
}
if ($path && $path != $front) {
// Add the Home link, except for the front page.
$links[] = $this->linkGenerator->generate($this->t('Home'), '<front>');
$links[] = $this->l($this->t('Home'), '<front>');
}
return array_reverse($links);
}
......@@ -227,13 +204,4 @@ protected function getRequestForPath($path, array $exclude) {
}
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translation->translate($string, $args, $options);
}
}
......@@ -12,7 +12,7 @@ services:
- {name: breadcrumb_builder, priority: 500}
system.breadcrumb.default:
class: Drupal\system\PathBasedBreadcrumbBuilder
arguments: ['@request', '@entity.manager', '@access_manager', '@string_translation', '@router', '@path_processor_manager', '@config.factory', '@link_generator', '@title_resolver']
arguments: ['@request', '@entity.manager', '@access_manager', '@router', '@path_processor_manager', '@config.factory', '@title_resolver']
tags:
- { name: breadcrumb_builder, priority: 0 }
path_processor.files:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment