diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php new file mode 100644 index 0000000000000000000000000000000000000000..63fe1997b3dfb8a884b54aa75f71a22b8930aab8 --- /dev/null +++ b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php @@ -0,0 +1,89 @@ +<?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; + } + +} diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml index f7ff964c4d4becf174d4ab08d83e4c8956fbfe2e..c290e67d0f788036ae3382715ecb244dacd28edd 100644 --- a/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -1,7 +1,7 @@ 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: diff --git a/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php b/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php index 2fef9276bca69aced312b0b37ebd806d6b9f4033..dd4f91976ab138634feb5894cd2cf63e259ecff2 100644 --- a/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php +++ b/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php @@ -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); - } - } diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index e633fe5cf9ea7b954d073bc48dbdd55dcdf51939..a82bbe4a2164a6f9fb742af6340b7cf6abafe798 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -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 diff --git a/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php b/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php index f18a66da49eb8bf0e36822a449243b6b8c6d8a5e..dd4008429fbc1cbd6f150f4aa69896cc1bbe66a3 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php +++ b/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php @@ -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); - } - } diff --git a/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php b/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php index 22230413144fdf0836fc4d6ce0722e3c35b83170..fb133a8b7e2e8fbab37de4744e83bf1f59d9682e 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php +++ b/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php @@ -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())); } } } diff --git a/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php b/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php index 1279a927ffdada8603536b2b5739048ac7d46c1f..5c0173ea4425b68086f8376a674515ee3d0b4ed5 100644 --- a/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php +++ b/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php @@ -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); - } - } diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index ae137daf14be0c956c7cd33423f4c5a88ecb8a3f..6a68e294a5d0dbf7f34f0167fb4e2edc6df833ea 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -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: