bb4008077488158bd90ede474ae8d0e55e4ce006
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / Breadcrumb.php
1 <?php
2
3 namespace Drupal\bootstrap\Plugin\Preprocess;
4
5 use Drupal\bootstrap\Utility\Variables;
6 use Drupal\Core\Template\Attribute;
7 use Drupal\Core\Url;
8
9 /**
10  * Pre-processes variables for the "breadcrumb" theme hook.
11  *
12  * @ingroup plugins_preprocess
13  *
14  * @BootstrapPreprocess("breadcrumb")
15  */
16 class Breadcrumb extends PreprocessBase implements PreprocessInterface {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function preprocessVariables(Variables $variables) {
22     $breadcrumb = &$variables['breadcrumb'];
23
24     // Determine if breadcrumbs should be displayed.
25     $breadcrumb_visibility = $this->theme->getSetting('breadcrumb');
26     if (($breadcrumb_visibility == 0 || ($breadcrumb_visibility == 2 && \Drupal::service('router.admin_context')->isAdminRoute())) || empty($breadcrumb)) {
27       $breadcrumb = [];
28       return;
29     }
30
31     // Remove first occurrence of the "Home" <front> link, provided by core.
32     if (!$this->theme->getSetting('breadcrumb_home')) {
33       $front = Url::fromRoute('<front>')->toString();
34       foreach ($breadcrumb as $key => $link) {
35         if (isset($link['url']) && $link['url'] === $front) {
36           unset($breadcrumb[$key]);
37           break;
38         }
39       }
40     }
41
42     if ($this->theme->getSetting('breadcrumb_title') && !empty($breadcrumb)) {
43       $request = \Drupal::request();
44       $route_match = \Drupal::routeMatch();
45       $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
46       if (!empty($page_title)) {
47         $breadcrumb[] = [
48           'text' => $page_title,
49           'attributes' => new Attribute(['class' => ['active']]),
50         ];
51       }
52     }
53
54     // Add cache context based on url.
55     $variables->addCacheContexts(['url']);
56   }
57
58 }