You are here

class PanelsBreadcrumbBuilder in Panels Breadcrumbs 8

Class PanelsBreadcrumbBuilder.

Hierarchy

Expanded class hierarchy of PanelsBreadcrumbBuilder

1 string reference to 'PanelsBreadcrumbBuilder'
panels_breadcrumbs.services.yml in ./panels_breadcrumbs.services.yml
panels_breadcrumbs.services.yml
1 service uses PanelsBreadcrumbBuilder
panels_breadcrumbs.breadcrumb in ./panels_breadcrumbs.services.yml
Drupal\panels_breadcrumbs\Breadcrumb\PanelsBreadcrumbBuilder

File

src/Breadcrumb/PanelsBreadcrumbBuilder.php, line 24

Namespace

Drupal\panels_breadcrumbs\Breadcrumb
View source
class PanelsBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * Route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * Token service.
   *
   * @var \Drupal\token\TokenInterface
   */
  protected $token;

  /**
   * The menu link access service.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * The dynamic router service.
   *
   * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface
   */
  protected $router;

  /**
   * The current user object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * PanelsBreadcrumbManager constructor.
   */
  public function __construct(RouteProviderInterface $route_provider, TokenInterface $token, AccessManagerInterface $access_manager, RequestMatcherInterface $router, AccountInterface $current_user) {
    $this->routeProvider = $route_provider;
    $this->token = $token;
    $this->accessManager = $access_manager;
    $this->router = $router;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $page_variant = $route_match
      ->getParameter('page_manager_page_variant');
    $variant_settings = $page_variant
      ->get('variant_settings');
    $titles = array_filter(array_map('trim', explode("\r\n", $variant_settings['panels_breadcrumbs']['titles'])), 'strlen');
    $paths = array_filter(array_map('trim', explode("\r\n", $variant_settings['panels_breadcrumbs']['paths'])), 'strlen');
    $entities = [];
    foreach ($page_variant
      ->getContexts() as $id => $context) {
      $entities[$id] = $context
        ->getContextValue();
    }
    $links = [];
    if ($variant_settings['panels_breadcrumbs']['home'] == 1) {
      $home_title = $this
        ->getTitle($variant_settings['panels_breadcrumbs']['home_text'], $entities);
      $links[] = Link::createFromRoute($home_title, '<front>');
    }
    foreach ($titles as $key => $title) {
      $title = $this
        ->getTitle($title, $entities);
      $path = $this->token
        ->replace($paths[$key], $entities);
      if ($this->routeProvider
        ->getRoutesByNames([
        $path,
      ])) {
        $path = Url::fromRoute($path)
          ->toString();
      }
      if ($path && ($request = $this
        ->getRequestForPath($path))) {
        $route_match = RouteMatch::createFromRequest($request);
        $access = $this->accessManager
          ->check($route_match, $this->currentUser, NULL, TRUE);
        $breadcrumb = $breadcrumb
          ->addCacheableDependency($access);
        if ($access
          ->isAllowed()) {
          $links[] = Link::fromTextAndUrl($title, Url::fromRouteMatch($route_match));
        }
      }
      else {
        $links[] = Link::createFromRoute($title, '<nolink>');
      }
    }
    $this
      ->addCaching($route_match, $breadcrumb);
    $breadcrumb
      ->setLinks($links);
    return $breadcrumb;
  }

  /**
   * Check if title is not token, and filtering It for security reason.
   */
  protected function getTitle($title, array $entities) {
    if (!$this->token
      ->scan($title)) {
      $title = $this
        ->t(Xss::filter($title));
    }
    else {
      $title = $this->token
        ->replace($title, $entities);
    }
    return $title;
  }

  /**
   * Get request object from path.
   */
  protected function getRequestForPath($path) {
    $request = Request::create($path);

    // Performance optimization: set a short accept header to reduce overhead in
    // AcceptHeaderMatcher when matching the request.
    $request->headers
      ->set('Accept', 'text/html');

    // Attempt to match this path to provide a fully built request.
    $request->attributes
      ->add($this->router
      ->matchRequest($request));
    return $request;
  }

  /**
   * Add cacheable dependencies and cache contexts.
   */
  protected function addCaching(RouteMatchInterface $route_match, Breadcrumb $breadcrumb) {
    $parameters = $route_match
      ->getParameters();
    foreach ($parameters as $key => $parameter) {
      if ($parameter instanceof CacheableDependencyInterface) {
        $breadcrumb
          ->addCacheableDependency($parameter);
      }
    }
    $breadcrumb
      ->addCacheContexts([
      'url.path',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PanelsBreadcrumbBuilder::$accessManager protected property The menu link access service.
PanelsBreadcrumbBuilder::$currentUser protected property The current user object.
PanelsBreadcrumbBuilder::$routeProvider protected property Route provider.
PanelsBreadcrumbBuilder::$router protected property The dynamic router service.
PanelsBreadcrumbBuilder::$token protected property Token service.
PanelsBreadcrumbBuilder::addCaching protected function Add cacheable dependencies and cache contexts.
PanelsBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
PanelsBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
PanelsBreadcrumbBuilder::getRequestForPath protected function Get request object from path.
PanelsBreadcrumbBuilder::getTitle protected function Check if title is not token, and filtering It for security reason.
PanelsBreadcrumbBuilder::__construct public function PanelsBreadcrumbManager constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.