Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions localgov_guides.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ services:
# Alter page header.
localgov_guides.page_header:
class: Drupal\localgov_guides\EventSubscriber\PageHeaderSubscriber
arguments: ['@entity.repository']
tags:
- { name: 'event_subscriber' }
19 changes: 19 additions & 0 deletions src/EventSubscriber/PageHeaderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\localgov_guides\EventSubscriber;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\localgov_core\Event\PageHeaderDisplayEvent;
use Drupal\node\Entity\Node;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -14,6 +15,23 @@
*/
class PageHeaderSubscriber implements EventSubscriberInterface {

/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;

/**
* PageHeaderSubscriber constructor.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(EntityRepositoryInterface $entity_repository) {
$this->entityRepository = $entity_repository;
}

/**
* {@inheritdoc}
*/
Expand All @@ -40,6 +58,7 @@ public function setPageHeader(PageHeaderDisplayEvent $event) {

$overview = $node->localgov_guides_parent->entity ?? NULL;
if (!empty($overview)) {
$overview = $this->entityRepository->getTranslationFromContext($overview);
$event->setTitle($overview->getTitle());
if ($overview->get('body')->summary) {
$event->setLede([
Expand Down
21 changes: 18 additions & 3 deletions src/Plugin/Block/GuidesAbstractBaseBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
Expand Down Expand Up @@ -63,6 +64,13 @@ abstract class GuidesAbstractBaseBlock extends BlockBase implements ContainerFac
*/
protected $routeMatch;

/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;

/**
* {@inheritdoc}
*/
Expand All @@ -72,7 +80,8 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_id,
$plugin_definition,
$container->get('current_route_match'),
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('entity.repository')
);
}

Expand All @@ -89,12 +98,15 @@ public static function create(ContainerInterface $container, array $configuratio
* The route match service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $route_match, EntityTypeManagerInterface $entity_type_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $route_match, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
if ($this->routeMatch->getParameter('node')) {
$this->node = $this->routeMatch->getParameter('node');
if (!$this->node instanceof NodeInterface) {
Expand All @@ -116,7 +128,10 @@ protected function setPages() {
$this->overview = $this->node->localgov_guides_parent->entity;
}

$this->guidePages = $this->overview->localgov_guides_pages->referencedEntities();
$this->overview = $this->entityRepository->getTranslationFromContext($this->overview);
foreach ($this->overview->localgov_guides_pages->referencedEntities() as $guide_page) {
$this->guidePages[] = $this->entityRepository->getTranslationFromContext($guide_page);
}
$this->guidePages = array_filter($this->guidePages, function ($guide_node) {
return ($guide_node instanceof NodeInterface) && $guide_node->access('view');
});
Expand Down