Skip to content

Commit

Permalink
Update all services blocks to use node contexts
Browse files Browse the repository at this point in the history
Set a node context to all service blocks and update the block base and all service
block functions to use the contextValue('node') if present.

Update each of the provided blocks to set a context_mapping for the current node route
Provide update hooks for CTA, releated links and related topics blocks
Provide a backward compatible way of accessing the current node via $this->node
for blocks that have not updated to use a node context so they can still get the current
node. (todo: decide if this should then be deprecated).
  • Loading branch information
andybroomfield committed Dec 10, 2023
1 parent e65add6 commit 2fbf3d2
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ settings:
label: 'Service page related links'
provider: localgov_services_page
label_display: '0'
context_mapping:
node: '@node.node_route_context:node'
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ settings:
label: 'Service page related links'
provider: localgov_services_page
label_display: '0'
context_mapping:
node: '@node.node_route_context:node'
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ settings:
label: 'Service page related topics'
provider: localgov_services_page
label_display: '0'
context_mapping:
node: '@node.node_route_context:node'
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ settings:
label: 'Service page related topics'
provider: localgov_services_page
label_display: '0'
context_mapping:
node: '@node.node_route_context:node'
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ settings:
label: 'Services call to action'
provider: localgov_services
label_display: '0'
context_mapping:
node: '@node.node_route_context:node'
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ settings:
label: 'Services call to action'
provider: localgov_services
label_display: '0'
context_mapping:
node: '@node.node_route_context:node'
visibility: { }
24 changes: 24 additions & 0 deletions localgov_services.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @file
* LocalGov services install file.
*/

/**
* Add node_route context mapping to localgov_service_cta_block.
*/
function localgov_services_update_8001() {
$entity_type_manager = \Drupal::entityTypeManager();
$cta_blocks = $entity_type_manager->getStorage('block')->loadByProperties([
'plugin' => 'localgov_service_cta_block',
]);

foreach ($cta_blocks as $block) {
$settings = $block->get('settings');
// Modify settings.
$settings['context_mapping']['node'] = '@node.node_route_context:node';
$block->set('settings', $settings);
$block->save();
}
}
26 changes: 25 additions & 1 deletion modules/localgov_services_page/localgov_services_page.install
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* @file
* LocalGov services page subanding install file.
* LocalGov services page install file.
*/

use Drupal\localgov_core\FieldRenameHelper;
Expand Down Expand Up @@ -35,3 +35,27 @@ function localgov_services_page_update_8001(&$sandbox) {

return t('Please export your sites configuration! Config entities for localgov_services_page where updated.');
}

/**
* Add node_route context mapping to service page blocks.
*
* Adding to localgov_services_related_links_block and
* localgov_services_related_topics_block.
*/
function localgov_services_page_update_8002() {
$entity_type_manager = \Drupal::entityTypeManager();
$related_links_blocks = $entity_type_manager->getStorage('block')->loadByProperties([
'plugin' => 'localgov_services_related_links_block',
]);
$related_topics_blocks = $entity_type_manager->getStorage('block')->loadByProperties([
'plugin' => 'localgov_services_related_topics_block',
]);

foreach (array_merge($related_links_blocks, $related_topics_blocks) as $block) {
$settings = $block->get('settings');
// Modify settings.
$settings['context_mapping']['node'] = '@node.node_route_context:node';
$block->set('settings', $settings);
$block->save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* id = "localgov_services_related_links_block",
* admin_label = @Translation("Service page related links"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = TRUE)
* }
* )
*/
Expand All @@ -27,9 +27,7 @@ class ServicesRelatedLinksBlock extends ServicesBlockBase implements ContainerFa
* {@inheritdoc}
*/
public function build() {
// $this->node = $this->getContextValue('node');
$build = parent::build();

$build = [];
$links = $this->getShouldUseManual() ? $this->buildManual() : $this->buildAutomated();

if ($links) {
Expand All @@ -50,9 +48,10 @@ public function build() {
*/
private function buildManual() {
$links = [];
$node = $this->getContextValue('node');

if ($this->node && $this->node->hasField('localgov_related_links')) {
foreach ($this->node->get('localgov_related_links')->getValue() as $link) {
if ($node && $node->hasField('localgov_related_links')) {
foreach ($node->get('localgov_related_links')->getValue() as $link) {
if (isset($link['title']) && isset($link['uri'])) {
$links[] = [
'title' => $link['title'],
Expand Down Expand Up @@ -86,6 +85,8 @@ private function buildAutomated() {
}

if ($topics) {
$this_node = $this->getContextValue('node');

// Perform our query.
$query = $this->database->query('SELECT entity_id FROM node__localgov_topic_classified
LEFT JOIN node_field_data ON node_field_data.nid=node__localgov_topic_classified.entity_id
Expand All @@ -96,7 +97,7 @@ private function buildAutomated() {
ORDER BY count(*) desc
LIMIT 6;',
[
':nid' => $this->node->id(),
':nid' => $this_node->id(),
':tids[]' => $topics,
]
);
Expand Down Expand Up @@ -124,8 +125,9 @@ private function buildAutomated() {
* Should manual links be displayed?
*/
private function getShouldUseManual() {
if ($this->node && $this->node->hasField('localgov_override_related_links') && !$this->node->get('localgov_override_related_links')->isEmpty()) {
return $this->node->get('localgov_override_related_links')->first()->getValue()['value'];
$node = $this->getContextValue('node');
if ($node && $node->hasField('localgov_override_related_links') && !$node->get('localgov_override_related_links')->isEmpty()) {
return $node->get('localgov_override_related_links')->first()->getValue()['value'];
}

return FALSE;
Expand All @@ -139,11 +141,12 @@ private function getShouldUseManual() {
*/
private function getTopics() {
$topics = [];
$node = $this->getContextValue('node');

if ($this->node && $this->node->hasField('localgov_topic_classified')) {
if ($node && $node->hasField('localgov_topic_classified')) {

/** @var \Drupal\taxonomy\TermInterface $term_info */
foreach ($this->node->get('localgov_topic_classified')->getValue() as $term_info) {
foreach ($node->get('localgov_topic_classified')->getValue() as $term_info) {
$topicEntity = $this->entityTypeManager->getStorage('taxonomy_term')->load($term_info['target_id']);

// Add topic only if an actual taxonomy term,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* id = "localgov_services_related_topics_block",
* admin_label = @Translation("Service page related topics"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = TRUE)
* }
* )
*/
Expand All @@ -25,13 +25,13 @@ class ServicesRelatedTopicsBlock extends ServicesBlockBase {
* {@inheritdoc}
*/
public function build() {
// $this->node = $this->getContextValue('node');
$build = parent::build();
$build = [];
$links = [];
$node = $this->getContextValue('node');

if ($this->node && $this->node->hasField('localgov_topic_classified')) {
if ($node && $node->hasField('localgov_topic_classified')) {
/** @var \Drupal\taxonomy\TermInterface $term_info */
foreach ($this->node->get('localgov_topic_classified')->getValue() as $term_info) {
foreach ($node->get('localgov_topic_classified')->getValue() as $term_info) {
$term = Term::load($term_info['target_id']);

// Add link only if an actual taxonomy term,
Expand Down Expand Up @@ -64,8 +64,9 @@ public function build() {
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
*/
private function hideRelatedTopics() {
if ($this->node->hasField('localgov_hide_related_topics') && !$this->node->get('localgov_hide_related_topics')->isEmpty()) {
return (bool) $this->node->get('localgov_hide_related_topics')->first()->getValue()['value'];
$node = $this->getContextValue('node');
if ($node->hasField('localgov_hide_related_topics') && !$node->get('localgov_hide_related_topics')->isEmpty()) {
return (bool) $node->get('localgov_hide_related_topics')->first()->getValue()['value'];
}

return FALSE;
Expand Down
31 changes: 14 additions & 17 deletions src/Plugin/Block/ServicesBlockBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ abstract class ServicesBlockBase extends BlockBase implements ContainerFactoryPl
/**
* Service node instance.
*
* This is a backup from the current route if no node context is present.
*
* @var \Drupal\node\Entity\Node
* @todo deprecate this property.
*/
protected $node = FALSE;

Expand Down Expand Up @@ -60,6 +63,10 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_type_manager;

// Set the node property to the node in the current route.
// This was the previous way of getting the current node if a node context
// has not been set. It's presence is for backward compatability with
// blocks that extend this block that have not been updated.
if ($this->routeMatch->getParameter('node')) {
$this->node = $this->routeMatch->getParameter('node');
if (!$this->node instanceof NodeInterface && is_int($this->node)) {
Expand All @@ -86,29 +93,19 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIf($this->node);
}

/**
* {@inheritdoc}
*/
public function build() {

// If there is a node context, set its value.
if ($node_context = $this->getContextValue('node')) {
$this->node = $node_context;
}

return [];
$node = isset($this->getContextDefinitions()['node']) ? $this->getContextValue('node') : $this->node;
return AccessResult::allowedIf($node);
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
if ($this->node instanceof NodeInterface) {
return Cache::mergeTags(parent::getCacheTags(), ['node:' . $this->node->id()]);
} else {
$node = isset($this->getContextDefinitions()['node']) ? $this->getContextValue('node') : $this->node;
if ($node instanceof NodeInterface) {
return Cache::mergeTags(parent::getCacheTags(), ['node:' . $node->id()]);
}
else {
return parent::getCacheTags();
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/Plugin/Block/ServicesCtaBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

/**
* Provides a 'Services CTA Block' block.
Expand All @@ -16,7 +15,7 @@
* id = "localgov_service_cta_block",
* admin_label = @Translation("Services call to action"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = TRUE)
* }
* )
*/
Expand All @@ -27,9 +26,10 @@ class ServicesCtaBlock extends ServicesBlockBase {
*/
protected function blockAccess(AccountInterface $account) {
// We only show this block if the current node contains some CTA actions.
if ($this->node &&
$this->node->hasField('localgov_common_tasks') &&
count($this->node->get('localgov_common_tasks')->getValue()) >= 1
$node = $this->getContextValue('node');
if ($node &&
$node->hasField('localgov_common_tasks') &&
count($node->get('localgov_common_tasks')->getValue()) >= 1
) {
return AccessResult::allowed();
}
Expand All @@ -40,15 +40,13 @@ protected function blockAccess(AccountInterface $account) {
* {@inheritdoc}
*/
public function build() {
// $this->node = $this->getContextValue('node');
parent::build();

$buttons = [];
if (empty($this->node)) {
$node = $this->getContextValue('node');
if (empty($node)) {
return [];
}

foreach ($this->node->get('localgov_common_tasks')->getValue() as $call_to_action) {
foreach ($node->get('localgov_common_tasks')->getValue() as $call_to_action) {
$type = 'cta-info';
if (isset($call_to_action['options']['type']) && $call_to_action['options']['type'] === 'action') {
$type = 'cta-action';
Expand All @@ -67,7 +65,7 @@ public function build() {
'#theme' => 'services_cta_block',
'#buttons' => $buttons,
'#cache' => [
'tags' => ['node:' . $this->node->id()],
'tags' => ['node:' . $node->id()],
'contexts' => ['url.path'],
],
];
Expand Down

0 comments on commit 2fbf3d2

Please sign in to comment.