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