You are here

PhotosBreadcrumbBuilder.php in Album Photos 8.4

Namespace

Drupal\photos

File

src/PhotosBreadcrumbBuilder.php
View source
<?php

namespace Drupal\photos;

use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Photos breadcrumb builder.
 */
class PhotosBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The router request context.
   *
   * @var \Drupal\Core\Routing\RequestContext
   */
  protected $context;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs the PathBasedBreadcrumbBuilder.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\Routing\RequestContext $context
   *   The router request context.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager service.
   */
  public function __construct(Connection $connection, RequestContext $context, EntityTypeManagerInterface $entity_manager) {
    $this->connection = $connection;
    $this->context = $context;
    $this->entityTypeManager = $entity_manager;
  }

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

    // Check if image page.
    $fid = $route_match
      ->getParameter('file');
    if ($fid) {
      $path = trim($this->context
        ->getPathInfo(), '/');
      $path_elements = explode('/', $path);
      return $path_elements[0] == 'photos' && $path_elements[1] == 'image';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addCacheContexts([
      'route',
    ]);

    // Home.
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Home'), '<front>'));
    $fid = $route_match
      ->getParameter('file');
    if ($fid) {

      // Recent images.
      $breadcrumb
        ->addLink(Link::createFromRoute($this
        ->t('Images'), 'photos.image.recent'));

      // Images by User.
      $uid = $this->connection
        ->query("SELECT uid FROM {file_managed} WHERE fid = :fid", [
        ':fid' => $fid,
      ])
        ->fetchField();
      $account = $this->entityTypeManager
        ->getStorage('user')
        ->load($uid);
      $username = $account
        ->getDisplayName();
      $breadcrumb
        ->addLink(Link::createFromRoute($this
        ->t('Images by :name', [
        ':name' => $username,
      ]), 'photos.user.images', [
        'user' => $uid,
      ]));

      // Album.
      $pid = $this->connection
        ->query("SELECT pid FROM {photos_image} WHERE fid = :fid", [
        ':fid' => $fid,
      ])
        ->fetchField();
      $node = $this->entityTypeManager
        ->getStorage('node')
        ->load($pid);
      $breadcrumb
        ->addLink(Link::createFromRoute($node
        ->getTitle(), 'photos.album', [
        'node' => $pid,
      ]));
    }
    return $breadcrumb;
  }

}

Classes

Namesort descending Description
PhotosBreadcrumbBuilder Photos breadcrumb builder.