diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php new file mode 100644 index 0000000..63fe199 --- /dev/null +++ b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php @@ -0,0 +1,89 @@ +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 f7ff964..c290e67 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 2fef927..dd4f919 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. @@ -27,20 +25,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. * * @var \Drupal\Core\Access\AccessManager @@ -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'), '')); + $links = array($this->l($this->t('Home'), '')); $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 e633fe5..a82bbe4 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 f18a66d..dd40084 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'), ''); $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 2223041..fb133a8 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'), ''); $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'), ''); 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 1279a92..5c0173e 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. @@ -43,13 +40,6 @@ class PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface { protected $accessManager; /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface; - */ - protected $translation; - - /** * The menu storage controller. * * @var \Drupal\Core\Config\Entity\ConfigStorageController @@ -71,13 +61,6 @@ class PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface { protected $pathProcessor; /** - * The link generator service. - * - * @var \Drupal\Core\Utility\LinkGeneratorInterface - */ - protected $linkGenerator; - - /** * Site config object. * * @var \Drupal\Core\Config\Config @@ -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'), ''); + $links[] = $this->l($this->t('Home'), ''); } 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 ae137da..6a68e29 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: