diff --git a/core/modules/forum/forum.services.yml b/core/modules/forum/forum.services.yml new file mode 100644 index 0000000..ff9f695 --- /dev/null +++ b/core/modules/forum/forum.services.yml @@ -0,0 +1,5 @@ +services: + forum.breadcrumb: + class: Drupal\forum\ForumBreadcrumbBuilder + tags: + - { name: breadcrumb_builder, priority: 1001 } diff --git a/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php b/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php new file mode 100644 index 0000000..3ee7df1 --- /dev/null +++ b/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php @@ -0,0 +1,101 @@ +attributes->has('drupal_menu_item')) { + $item = $request->attributes->get('drupal_menu_item'); + switch ($item['path']) { + + case 'node/%': + $node = $item['map'][1]; + // Load the object in case of missing wildcard loaders. + $node = is_object($node) ? $node : node_load($node); + if (_forum_node_check_node_type($node)) { + $breadcrumb = $this->forumPostBreadcrumb($node); + } + break; + + case 'forum/%': + $term = $item['map'][1]; + // Load the object in case of missing wildcard loaders. + $term = is_object($term) ? $term : forum_forum_load($term); + $breadcrumb = $this->forumTermBreadcrumb($term); + break; + } + } + + if (!empty($breadcrumb)) { + return array( + '#theme' => 'breadcrumb', + '#breadcrumb' => $breadcrumb, + ); + } + } + + /** + * Builds the breadcrumb for a forum post page. + */ + protected function forumPostBreadcrumb($node) { + $config = config('forum.settings'); + $vocabulary = entity_load('taxonomy_vocabulary', $config->get('vocabulary')); + + $breadcrumb[] = l(t('Home'), NULL); + $breadcrumb[] = l($vocabulary->name, '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->tid); + } + } + return $breadcrumb; + } + + /** + * Builds the breadcrumb for a forum term page. + */ + protected function forumTermBreadcrumb($term) { + $config = config('forum.settings'); + $vocabulary = entity_load('taxonomy_vocabulary', $config->get('vocabulary')); + + $breadcrumb[] = l(t('Home'), NULL); + if ($term->tid) { + // Parent of all forums is the vocabulary name. + $breadcrumb[] = l($vocabulary->label(), 'forum'); + } + // Add all parent forums to breadcrumbs. + if ($term->parents) { + foreach (array_reverse($term->parents) as $parent) { + if ($parent->id() != $term->tid) { + $breadcrumb[] = l($parent->label(), 'forum/' . $parent->id()); + } + } + } + return $breadcrumb; + } +}