Skip to content

Instantly share code, notes, and snippets.

@damiankloip
Created December 5, 2013 16:04
Show Gist options
  • Save damiankloip/7808062 to your computer and use it in GitHub Desktop.
Save damiankloip/7808062 to your computer and use it in GitHub Desktop.
diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderInterface.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderInterface.php
index 1fae4c6..cff4c54 100644
--- a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderInterface.php
+++ b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderInterface.php
@@ -13,14 +13,26 @@
interface BreadcrumbBuilderInterface {
/**
+ * Whether this breadcrumb builder should be used to build the breadcrumb.
+ *
+ * @param array $attributes
+ * Attributes representing the current page.
+ *
+ * @return bool
+ * TRUE if this builder should be used or FALSE to let other builders
+ * decide.
+ */
+ public function applies(array $attributes);
+
+ /**
* Builds the breadcrumb.
*
* @param array $attributes
* Attributes representing the current page.
*
- * @return array|null
- * A render array for the breadcrumbs or NULL to let other builders decide.
- * Returning empty array will suppress all breadcrumbs.
+ * @return array
+ * A render array for the breadcrumbs. Returning an empty array will
+ * suppress all breadcrumbs.
*/
public function build(array $attributes);
diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php
index 45de662..95b42f1 100644
--- a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php
+++ b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php
@@ -67,18 +67,27 @@ public function addBuilder(BreadcrumbBuilderInterface $builder, $priority) {
/**
* {@inheritdoc}
*/
+ public function applies(array $attributes) {
+ return TRUE;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function build(array $attributes) {
$breadcrumb = array();
$context = array('builder' => NULL);
// Call the build method of registered breadcrumb builders,
// until one of them returns an array.
foreach ($this->getSortedBuilders() as $builder) {
- $build = $builder->build($attributes);
- if (!isset($build)) {
- // The builder returned NULL, so we continue with the other builders.
+ if (!$builder->applies($attributes)) {
+ // The builder does not apply, so we continue with the other builders.
continue;
}
- elseif (is_array($build)) {
+
+ $build = $builder->build($attributes);
+
+ if (is_array($build)) {
// The builder returned an array of breadcrumb links.
$breadcrumb = $build;
$context['builder'] = $builder;
diff --git a/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php b/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php
index 3ad730f..6dd54ff 100644
--- a/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php
+++ b/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php
@@ -58,31 +58,38 @@ public function __construct(EntityManagerInterface $entity_manager, AccessManage
/**
* {@inheritdoc}
*/
+ public function applies(array $attributes) {
+ return !empty($attributes['node'])
+ && ($attributes['node'] instanceof NodeInterface)
+ && !empty($attributes['node']->book);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function build(array $attributes) {
- if (!empty($attributes['node']) && $attributes['node'] instanceof NodeInterface && !empty($attributes['node']->book)) {
- $mlids = array();
- $links = array($this->l($this->t('Home'), '<front>'));
- $book = $attributes['node']->book;
+ $mlids = array();
+ $links = array($this->l($this->t('Home'), '<front>'));
+ $book = $attributes['node']->book;
+ $depth = 1;
+ // We skip the current node.
+ while (!empty($book['p' . ($depth + 1)])) {
+ $mlids[] = $book['p' . $depth];
+ $depth++;
+ }
+ $menu_links = $this->menuLinkStorage->loadMultiple($mlids);
+ if (count($menu_links) > 0) {
$depth = 1;
- // We skip the current node.
while (!empty($book['p' . ($depth + 1)])) {
- $mlids[] = $book['p' . $depth];
- $depth++;
- }
- $menu_links = $this->menuLinkStorage->loadMultiple($mlids);
- if (count($menu_links) > 0) {
- $depth = 1;
- 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, $this->account)) {
- $links[] = $this->l($menu_link->label(), $menu_link->route_name, $menu_link->route_parameters, $menu_link->options);
- }
+ 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, $this->account)) {
+ $links[] = $this->l($menu_link->label(), $menu_link->route_name, $menu_link->route_parameters, $menu_link->options);
}
- $depth++;
}
+ $depth++;
}
- return $links;
}
+ return $links;
}
}
diff --git a/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php b/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php
index e7a4bb8..aa41c41 100644
--- a/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php
+++ b/core/modules/comment/lib/Drupal/comment/CommentBreadcrumbBuilder.php
@@ -36,20 +36,26 @@ public function __construct(EntityManagerInterface $entity_manager) {
/**
* {@inheritdoc}
*/
+ public function applies(array $attributes) {
+ return isset($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'comment.reply'
+ && isset($attributes['entity_type'])
+ && isset($attributes['entity_id'])
+ && isset($attributes['field_name']);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function build(array $attributes) {
- if (isset($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'comment.reply'
- && isset($attributes['entity_type'])
- && isset($attributes['entity_id'])
- && isset($attributes['field_name'])
- ) {
- $breadcrumb[] = $this->l($this->t('Home'), '<front>');
- $entity = $this->entityManager
- ->getStorageController($attributes['entity_type'])
- ->load($attributes['entity_id']);
- $uri = $entity->uri();
- $breadcrumb[] = l($entity->label(), $uri['path'], $uri['options']);
- return $breadcrumb;
- }
+ $breadcrumb = array();
+
+ $breadcrumb[] = $this->l($this->t('Home'), '<front>');
+ $entity = $this->entityManager
+ ->getStorageController($attributes['entity_type'])
+ ->load($attributes['entity_id']);
+ $uri = $entity->uri();
+ $breadcrumb[] = l($entity->label(), $uri['path'], $uri['options']);
+ return $breadcrumb;
}
}
diff --git a/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php b/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php
index e49f093..cf1d148 100644
--- a/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php
+++ b/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php
@@ -58,18 +58,25 @@ public function __construct(EntityManagerInterface $entity_manager, ConfigFactor
/**
* {@inheritdoc}
*/
+ public function applies(array $attributes) {
+ return !empty($attributes[RouteObjectInterface::ROUTE_NAME]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function build(array $attributes) {
- if (!empty($attributes[RouteObjectInterface::ROUTE_NAME])) {
- $route_name = $attributes[RouteObjectInterface::ROUTE_NAME];
- if ($route_name == 'node.view' && isset($attributes['node'])) {
- if ($this->forumManager->checkNodeType($attributes['node'])) {
- return $this->forumPostBreadcrumb($attributes['node']);
- }
- }
- if ($route_name == 'forum.page' && isset($attributes['taxonomy_term'])) {
- return $this->forumTermBreadcrumb($attributes['taxonomy_term']);
+ $route_name = $attributes[RouteObjectInterface::ROUTE_NAME];
+
+ if ($route_name == 'node.view' && isset($attributes['node'])) {
+ if ($this->forumManager->checkNodeType($attributes['node'])) {
+ return $this->forumPostBreadcrumb($attributes['node']);
}
}
+
+ if ($route_name == 'forum.page' && isset($attributes['taxonomy_term'])) {
+ return $this->forumTermBreadcrumb($attributes['taxonomy_term']);
+ }
}
/**
diff --git a/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php b/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php
index 1f59256..f185e48 100644
--- a/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php
+++ b/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php
@@ -105,6 +105,13 @@ public function __construct(Request $request, EntityManagerInterface $entity_man
/**
* {@inheritdoc}
*/
+ public function applies(array $attributes) {
+ return TRUE;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function build(array $attributes) {
$links = array();
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/TermBreadcrumbBuilder.php b/core/modules/taxonomy/lib/Drupal/taxonomy/TermBreadcrumbBuilder.php
index 3a6e3e9..e886357 100644
--- a/core/modules/taxonomy/lib/Drupal/taxonomy/TermBreadcrumbBuilder.php
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/TermBreadcrumbBuilder.php
@@ -18,21 +18,29 @@ class TermBreadcrumbBuilder extends BreadcrumbBuilderBase {
/**
* {@inheritdoc}
*/
+ public function applies(array $attributes) {
+ return !empty($attributes[RouteObjectInterface::ROUTE_NAME])
+ && ($attributes[RouteObjectInterface::ROUTE_NAME] == 'taxonomy.term_page')
+ && ($attributes['taxonomy_term'] instanceof TermInterface);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function build(array $attributes) {
- if (!empty($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'taxonomy.term_page' && ($term = $attributes['taxonomy_term']) && $term instanceof TermInterface) {
- // @todo This overrides any other possible breadcrumb and is a pure
- // hard-coded presumption. Make this behavior configurable per
- // vocabulary or term.
- $breadcrumb = array();
- while ($parents = taxonomy_term_load_parents($term->id())) {
- $term = array_shift($parents);
- $breadcrumb[] = $this->l($term->label(), 'taxonomy.term_page', array('taxonomy_term' => $term->id()));
- }
- $breadcrumb[] = $this->l($this->t('Home'), '<front>');
- $breadcrumb = array_reverse($breadcrumb);
-
- return $breadcrumb;
+ $term = $attributes['taxonomy_term'];
+ // @todo This overrides any other possible breadcrumb and is a pure
+ // hard-coded presumption. Make this behavior configurable per
+ // vocabulary or term.
+ $breadcrumb = array();
+ while ($parents = taxonomy_term_load_parents($term->id())) {
+ $term = array_shift($parents);
+ $breadcrumb[] = $this->l($term->label(), 'taxonomy.term_page', array('taxonomy_term' => $term->id()));
}
+ $breadcrumb[] = $this->l($this->t('Home'), '<front>');
+ $breadcrumb = array_reverse($breadcrumb);
+
+ return $breadcrumb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment