-
Notifications
You must be signed in to change notification settings - Fork 5
/
PageHeaderSubscriber.php
69 lines (58 loc) · 1.69 KB
/
PageHeaderSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Drupal\localgov_guides\EventSubscriber;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\localgov_core\Event\PageHeaderDisplayEvent;
use Drupal\node\NodeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Set page title.
*
* @package Drupal\localgov_guides\EventSubscriber
*/
class PageHeaderSubscriber implements EventSubscriberInterface {
/**
* PageHeaderSubscriber constructor.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* The entity repository.
*/
public function __construct(protected EntityRepositoryInterface $entityRepository) {
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
PageHeaderDisplayEvent::EVENT_NAME => ['setPageHeader', 0],
];
}
/**
* Set page title and lede.
*/
public function setPageHeader(PageHeaderDisplayEvent $event) {
$node = $event->getEntity();
if (!$node instanceof NodeInterface) {
return;
}
if ($node->bundle() !== 'localgov_guides_page') {
return;
}
$overview = $node->localgov_guides_parent->entity ?? NULL;
if ($overview instanceof NodeInterface) {
$overview = $this->entityRepository->getTranslationFromContext($overview);
$event->setTitle($overview->getTitle());
if ($overview->get('body')->summary) {
$event->setLede([
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $overview->get('body')->summary,
]);
}
$event->setCacheTags(Cache::mergeTags($node->getCacheTags(), $overview->getCacheTags()));
}
else {
$event->setLede('');
}
}
}