You are here

class PhotosBreadcrumbBuilder in Album Photos 8.4

Photos breadcrumb builder.

Hierarchy

Expanded class hierarchy of PhotosBreadcrumbBuilder

1 string reference to 'PhotosBreadcrumbBuilder'
photos.services.yml in ./photos.services.yml
photos.services.yml
1 service uses PhotosBreadcrumbBuilder
photos.breadcrumb in ./photos.services.yml
Drupal\photos\PhotosBreadcrumbBuilder

File

src/PhotosBreadcrumbBuilder.php, line 17

Namespace

Drupal\photos
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhotosBreadcrumbBuilder::$connection protected property The database connection.
PhotosBreadcrumbBuilder::$context protected property The router request context.
PhotosBreadcrumbBuilder::$entityTypeManager protected property The entity manager.
PhotosBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
PhotosBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
PhotosBreadcrumbBuilder::__construct public function Constructs the PathBasedBreadcrumbBuilder.
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.