Skip to content

Commit

Permalink
Merge pull request #152 from rupertj/header_block_cache_tags
Browse files Browse the repository at this point in the history
Adds a mechanism for subscribers to PageHeaderDisplayEvent to set the cache tags on the PageHeaderBlock.
  • Loading branch information
finnlewis committed Oct 31, 2022
2 parents 198272b + 83b4b70 commit c191ec8
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 9 deletions.
26 changes: 26 additions & 0 deletions src/Event/PageHeaderDisplayEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class PageHeaderDisplayEvent extends Event {
*/
protected $visibility = TRUE;

/**
* Cache tags override.
*
* @var array|null
*/
protected $cacheTags = NULL;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -116,4 +123,23 @@ public function setVisibility($visibility) {
$this->visibility = $visibility;
}

/**
* Cache tags getter.
*
* @return array|null
*/
public function getCacheTags() {
return $this->cacheTags;
}

/**
* Cache tags setter.
*
* @param array $cacheTags
* The cache tags.
*/
public function setCacheTags(array $cacheTags) {
$this->cacheTags = $cacheTags;
}

}
15 changes: 12 additions & 3 deletions src/Plugin/Block/PageHeaderBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class PageHeaderBlock extends BlockBase implements ContainerFactoryPluginInterfa
*/
protected $visible;

/**
* Cache tags for this block.
*
* @var array
*/
protected $cacheTags;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -126,10 +133,12 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$event = new PageHeaderDisplayEvent($this->entity);
$this->eventDispatcher->dispatch($event, PageHeaderDisplayEvent::EVENT_NAME);

// Set the title, lede and visibility.
// Set the title, lede, visibility and cache tags.
$this->title = is_null($event->getTitle()) ? $this->getTitle() : $event->getTitle();
$this->lede = is_null($event->getLede()) ? $this->getLede() : $event->getLede();
$this->visible = $event->getVisibility();
$entityCacheTags = is_null($this->entity) ? [] : $this->entity->getCacheTags();
$this->cacheTags = is_null($event->getCacheTags()) ? $entityCacheTags : $event->getCacheTags();
}

/**
Expand Down Expand Up @@ -214,8 +223,8 @@ public function defaultConfiguration() {
* {@inheritdoc}
*/
public function getCacheTags() {
if (!is_null($this->entity)) {
return Cache::mergeTags(parent::getCacheTags(), $this->entity->getCacheTags());
if (!empty($this->cacheTags)) {
return Cache::mergeTags(parent::getCacheTags(), $this->cacheTags);
}
return parent::getCacheTags();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\localgov_core_page_header_event_test\EventSubscriber;

use Drupal\Core\Cache\Cache;
use Drupal\node\Entity\Node;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\localgov_core\Event\PageHeaderDisplayEvent;
Expand All @@ -27,20 +28,29 @@ public static function getSubscribedEvents() {
*/
public function setPageHeader(PageHeaderDisplayEvent $event) {

$node = $event->getEntity();

if (!$node instanceof Node) {
return;
}

// Override title and lede for page1 node content types.
if ($event->getEntity() instanceof Node &&
$event->getEntity()->bundle() == 'page1'
) {
if ($node->bundle() == 'page1') {
$event->setTitle('Overridden title');
$event->setLede('Overridden lede');
}

// Hide page header block for page2 content types.
if ($event->getEntity() instanceof Node &&
$event->getEntity()->bundle() == 'page2'
) {
if ($node->bundle() == 'page2') {
$event->setVisibility(FALSE);
}

// Set lede from summary, and cache tags from the parent for page3 nodes.
if ($node->bundle() == 'page3') {
$parent = $node->parent->entity;
$event->setLede($parent->body->summary);
$event->setCacheTags(Cache::mergeTags($node->getCacheTags(), $parent->getCacheTags()));
}
}

}
64 changes: 64 additions & 0 deletions tests/src/Functional/PageHeaderBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Drupal\Tests\localgov_core\Functional;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;


/**
* Functional tests for LocalGovDrupal install profile.
*/
Expand Down Expand Up @@ -119,6 +122,67 @@ public function testPageHeaderDisplayEvent() {
$this->assertSession()->pageTextNotContains($summary);
$this->assertSession()->responseNotContains('<h1 class="header">Overridden title</h1>');
$this->assertSession()->pageTextNotContains('Overridden lede');

// Check cache tags override.
// Set up a page3 that can reference other page3 nodes.
$this->createContentType(['type' => 'page3']);

FieldStorageConfig::create([
'field_name' => 'parent',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => [
'target_type' => 'node',
],
])->save();

FieldConfig::create([
'field_name' => 'parent',
'entity_type' => 'node',
'bundle' => 'page3',
'label' => 'Parent',
'cardinality' => -1,
])->save();

$page3parent = $this->createNode([
'type' => 'page3',
'title' => 'page 3 parent title',
'body' => [
'summary' => 'page 3 parent summary',
'value' => '',
],
'status' => NodeInterface::PUBLISHED,
]);

$page3child = $this->createNode([
'type' => 'page3',
'title' => 'page 3 child title',
'body' => [
'summary' => 'page 3 child summary',
'value' => '',
],
'parent' => [
'target_id' => $page3parent->id(),
],
'status' => NodeInterface::PUBLISHED,
]);

// Load the child page.
$this->drupalGet($page3child->toUrl()->toString());

// Check the child page contains the parent summary.
$this->assertSession()->pageTextContains('page 3 parent summary');

// Update the parent summary.
$page3parent->body->summary = 'page 3 parent updated summary';
$page3parent->save();

// Reload the child page.
$this->drupalGet($page3child->toUrl()->toString());

// Check the child page contains the updated parent summary.
$this->assertSession()->pageTextContains('page 3 parent updated summary');
}

}

0 comments on commit c191ec8

Please sign in to comment.