file Breadcrumb.php

  1. 8.x-3.x src/Plugin/Preprocess/Breadcrumb.php
  2. 8.x-3.x src/Plugin/Setting/Components/Breadcrumbs/Breadcrumb.php

Namespace

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

Classes

Name Description
Breadcrumb Pre-processes variables for the "breadcrumb" theme hook.