Skip to content

Commit

Permalink
Merge pull request #196 from localgovdrupal/2.x
Browse files Browse the repository at this point in the history
Release 2.13.0
  • Loading branch information
andybroomfield committed Nov 23, 2023
2 parents 2dcbd8c + fde05ad commit 50aece6
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 8 deletions.
1 change: 1 addition & 0 deletions localgov_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function localgov_core_theme($existing, $type, $theme, $path) {
'localgov_page_header_block' => [
'variables' => [
'title' => '',
'subtitle' => NULL,
'lede' => NULL,
],
'render element' => 'block',
Expand Down
27 changes: 27 additions & 0 deletions src/Event/PageHeaderDisplayEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class PageHeaderDisplayEvent extends Event {
*/
protected $title = NULL;

/**
* The page sub title override.
*
* @var array|string|null
*/
protected $subTitle = NULL;

/**
* Should the page header block be displayed?
*
Expand Down Expand Up @@ -103,6 +110,26 @@ public function setTitle($title) {
$this->title = $title;
}

/**
* Sub title getter.
*
* @return array|string|null
* The sub title.
*/
public function getSubTitle() {
return $this->subTitle;
}

/**
* Sub title setter.
*
* @param array|string|null $sub_title
* The sub title.
*/
public function setSubTitle($sub_title) {
$this->subTitle = $sub_title;
}

/**
* Visibility getter.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Plugin/Block/PageHeaderBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class PageHeaderBlock extends BlockBase implements ContainerFactoryPluginInterfa
*/
protected $title;

/**
* The page subtitle override.
*
* @var array|string|null
*/
protected $subTitle;

/**
* The page lede override.
*
Expand Down Expand Up @@ -149,6 +156,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition

// Set the title, lede, visibility and cache tags.
$this->title = is_null($event->getTitle()) ? $this->getTitle() : $event->getTitle();
$this->subTitle = is_null($event->getSubTitle()) ? NULL : $event->getSubTitle();
$this->lede = is_null($event->getLede()) ? $this->getLede() : $event->getLede();
$this->visible = $event->getVisibility();
$entityCacheTags = is_null($this->entity) ? [] : $this->entity->getCacheTags();
Expand All @@ -164,6 +172,7 @@ public function build() {
$build[] = [
'#theme' => 'localgov_page_header_block',
'#title' => $this->title,
'#subtitle' => $this->subTitle,
'#lede' => $this->lede,
];

Expand Down
7 changes: 6 additions & 1 deletion templates/localgov-page-header-block.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
] %}
{% if title %}
<div{{ attributes.addClass(classes) }}>
<h1 class="header">{{ title }}</h1>
<h1 class="header">
{{ title }}
{% if subtitle %}
<div class="header__subtitle">{{ subtitle }}</div>
{% endif %}
</h1>
{% if lede %}
<div class="subheader">{{ lede }}</div>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public function setPageHeader(PageHeaderDisplayEvent $event) {
$event->setLede($parent->body->summary);
$event->setCacheTags(Cache::mergeTags($node->getCacheTags(), $parent->getCacheTags()));
}

// Set subtitle from parent, and cache tags from the parent for page4 nodes.
if ($node->bundle() == 'page4') {
$parent = $node->parent->entity;
$event->setSubTitle($parent->title->value);
$event->setCacheTags(Cache::mergeTags($node->getCacheTags(), $parent->getCacheTags()));
}
}

}
75 changes: 68 additions & 7 deletions tests/src/Functional/PageHeaderBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ public function testPageHeaderBlockDisplay() {
'status' => NodeInterface::PUBLISHED,
]);
$this->drupalGet($page->toUrl()->toString());
$this->assertSession()->responseContains('<h1 class="header">' . $node_title . '</h1>');
$query = $this->xpath('.//h1[contains(concat(" ",normalize-space(@class)," ")," header ")]');
$page_title = $query[0]->getText();
$this->assertEquals($page_title, $node_title);
$this->assertSession()->pageTextNotContains($node_summary);
$page->set('body', [
'summary' => $node_summary,
'value' => '',
]);
$page->save();
$this->drupalGet($page->toUrl()->toString());
$this->assertSession()->responseContains('<h1 class="header">' . $node_title . '</h1>');
$query = $this->xpath('.//h1[contains(concat(" ",normalize-space(@class)," ")," header ")]');
$page_title = $query[0]->getText();
$this->assertEquals($page_title, $node_title);
$this->assertSession()->pageTextContains($node_summary);

// Check title and lede display on a taxonomy term page.
Expand All @@ -77,7 +81,9 @@ public function testPageHeaderBlockDisplay() {
]);
$term->save();
$this->drupalGet($term->toUrl()->toString());
$this->assertSession()->responseContains('<h1 class="header">' . $term_name . '</h1>');
$query = $this->xpath('.//h1[contains(concat(" ",normalize-space(@class)," ")," header ")]');
$page_title = $query[0]->getText();
$this->assertEquals($page_title, $term_name);
$this->assertSession()->pageTextContains('All pages relating to ' . $term_name);
}

Expand All @@ -100,9 +106,11 @@ public function testPageHeaderDisplayEvent() {
'status' => NodeInterface::PUBLISHED,
]);
$this->drupalGet($page1->toUrl()->toString());
$this->assertSession()->responseNotContains('<h1 class="header">' . $title . '</h1>');
$query = $this->xpath('.//h1[contains(concat(" ",normalize-space(@class)," ")," header ")]');
$page_title = $query[0]->getText();
$this->assertNotEquals($page_title, $title);
$this->assertSession()->pageTextNotContains($summary);
$this->assertSession()->responseContains('<h1 class="header">Overridden title</h1>');
$this->assertEquals($page_title, 'Overridden title');
$this->assertSession()->pageTextContains('Overridden lede');

// Check hidden page header block.
Expand All @@ -117,9 +125,20 @@ public function testPageHeaderDisplayEvent() {
'status' => NodeInterface::PUBLISHED,
]);
$this->drupalGet($page2->toUrl()->toString());
$this->assertSession()->responseNotContains('<h1 class="header">' . $title . '</h1>');

// There should be no h1 visible.
$query = $this->xpath('.//h1[contains(concat(" ",normalize-space(@class)," ")," header ")]');
$this->assertEmpty($query);

// Using pageTextNotContains also fetches the title tag, so do an xpath on
// the body tag to check the title text is not present.
$body_query = $this->xpath('.//body');
$body_text = $body_query[0]->getText();
$this->assertStringNotContainsString($title, $body_text);

// Check summary and overridden title and summary not present in page.
$this->assertSession()->pageTextNotContains($summary);
$this->assertSession()->responseNotContains('<h1 class="header">Overridden title</h1>');
$this->assertSession()->pageTextNotContains('Overridden title');
$this->assertSession()->pageTextNotContains('Overridden lede');

// Check cache tags override.
Expand Down Expand Up @@ -182,6 +201,48 @@ public function testPageHeaderDisplayEvent() {

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

// Set up a page4 that can reference other page4 nodes with
// subtitle in header.
$this->createContentType(['type' => 'page4']);

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

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

$page4child = $this->createNode([
'type' => 'page4',
'title' => 'page 4 child title',
'body' => [
'summary' => 'page 4 child summary',
'value' => '',
],
'parent' => [
'target_id' => $page4parent->id(),
],
'status' => NodeInterface::PUBLISHED,
]);

// Load the child page.
$this->drupalGet($page4child->toUrl()->toString());
$query = $this->xpath('.//h1[contains(concat(" ",normalize-space(@class)," ")," header ")]//div');
$page_subtitle = $query[0]->getText();
$this->assertEquals($page_subtitle, 'page 4 parent title');

}

}

0 comments on commit 50aece6

Please sign in to comment.