PhotosBreadcrumbBuilder.php in Album Photos 8.4
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;
class PhotosBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
protected $connection;
protected $context;
protected $entityTypeManager;
public function __construct(Connection $connection, RequestContext $context, EntityTypeManagerInterface $entity_manager) {
$this->connection = $connection;
$this->context = $context;
$this->entityTypeManager = $entity_manager;
}
public function applies(RouteMatchInterface $route_match) {
$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';
}
}
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb
->addCacheContexts([
'route',
]);
$breadcrumb
->addLink(Link::createFromRoute($this
->t('Home'), '<front>'));
$fid = $route_match
->getParameter('file');
if ($fid) {
$breadcrumb
->addLink(Link::createFromRoute($this
->t('Images'), 'photos.image.recent'));
$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,
]));
$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;
}
}