diff --git a/config/optional/block.block.localgov_servicepagerelatedlinks_base.yml b/config/optional/block.block.localgov_servicepagerelatedlinks_base.yml index 8e57ecf..e3317ae 100644 --- a/config/optional/block.block.localgov_servicepagerelatedlinks_base.yml +++ b/config/optional/block.block.localgov_servicepagerelatedlinks_base.yml @@ -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: { } diff --git a/config/optional/block.block.localgov_servicepagerelatedlinks_scarfolk.yml b/config/optional/block.block.localgov_servicepagerelatedlinks_scarfolk.yml index 4a77a5f..aad3139 100644 --- a/config/optional/block.block.localgov_servicepagerelatedlinks_scarfolk.yml +++ b/config/optional/block.block.localgov_servicepagerelatedlinks_scarfolk.yml @@ -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: { } diff --git a/config/optional/block.block.localgov_servicepagerelatedtopics_base.yml b/config/optional/block.block.localgov_servicepagerelatedtopics_base.yml index 38e5d8f..e5d3a19 100644 --- a/config/optional/block.block.localgov_servicepagerelatedtopics_base.yml +++ b/config/optional/block.block.localgov_servicepagerelatedtopics_base.yml @@ -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: { } diff --git a/config/optional/block.block.localgov_servicepagerelatedtopics_scarfolk.yml b/config/optional/block.block.localgov_servicepagerelatedtopics_scarfolk.yml index 656346f..cb03b5f 100644 --- a/config/optional/block.block.localgov_servicepagerelatedtopics_scarfolk.yml +++ b/config/optional/block.block.localgov_servicepagerelatedtopics_scarfolk.yml @@ -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: { } diff --git a/config/optional/block.block.localgov_servicescalltoaction_base.yml b/config/optional/block.block.localgov_servicescalltoaction_base.yml index 4f86819..360ee7d 100644 --- a/config/optional/block.block.localgov_servicescalltoaction_base.yml +++ b/config/optional/block.block.localgov_servicescalltoaction_base.yml @@ -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: { } diff --git a/config/optional/block.block.localgov_servicescalltoaction_scarfolk.yml b/config/optional/block.block.localgov_servicescalltoaction_scarfolk.yml index b413f27..9864d88 100644 --- a/config/optional/block.block.localgov_servicescalltoaction_scarfolk.yml +++ b/config/optional/block.block.localgov_servicescalltoaction_scarfolk.yml @@ -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: { } diff --git a/localgov_services.install b/localgov_services.install new file mode 100644 index 0000000..1e3a70f --- /dev/null +++ b/localgov_services.install @@ -0,0 +1,24 @@ +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(); + } +} diff --git a/modules/localgov_services_landing/templates/node--localgov-services-landing--full.html.twig b/modules/localgov_services_landing/templates/node--localgov-services-landing--full.html.twig index 98af977..c6b4bd3 100644 --- a/modules/localgov_services_landing/templates/node--localgov-services-landing--full.html.twig +++ b/modules/localgov_services_landing/templates/node--localgov-services-landing--full.html.twig @@ -17,6 +17,11 @@ ] %} +{# Render layout builder as is idf present #} +{% if content._layout_builder|render|trim is not empty %} + {{ content._layout_builder }} +{% else %} + {% if content.localgov_destinations %}

{{ 'Services List'|t }}

@@ -109,3 +114,4 @@ +{% endif %} diff --git a/modules/localgov_services_page/localgov_services_page.install b/modules/localgov_services_page/localgov_services_page.install index 2ecd953..b008c94 100644 --- a/modules/localgov_services_page/localgov_services_page.install +++ b/modules/localgov_services_page/localgov_services_page.install @@ -2,7 +2,7 @@ /** * @file - * LocalGov services page subanding install file. + * LocalGov services page install file. */ use Drupal\localgov_core\FieldRenameHelper; @@ -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(); + } +} diff --git a/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedLinksBlock.php b/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedLinksBlock.php index 75482cb..6b743a6 100644 --- a/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedLinksBlock.php +++ b/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedLinksBlock.php @@ -14,6 +14,9 @@ * @Block( * id = "localgov_services_related_links_block", * admin_label = @Translation("Service page related links"), + * context_definitions = { + * "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = TRUE) + * } * ) */ class ServicesRelatedLinksBlock extends ServicesBlockBase implements ContainerFactoryPluginInterface { @@ -44,9 +47,10 @@ public function build(): array { */ private function getLinks(): array { $links = []; + $node = $this->getContextValue('node'); - if ($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'], @@ -69,8 +73,9 @@ private function getLinks(): array { * Should manual links be displayed? */ private function displayLinks(): bool { - if ($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->hasField('localgov_override_related_links') && !$node->get('localgov_override_related_links')->isEmpty()) { + return $node->get('localgov_override_related_links')->first()->getValue()['value']; } return TRUE; diff --git a/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedTopicsBlock.php b/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedTopicsBlock.php index 38fa650..c903ea3 100644 --- a/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedTopicsBlock.php +++ b/modules/localgov_services_page/src/Plugin/Block/ServicesRelatedTopicsBlock.php @@ -14,6 +14,9 @@ * @Block( * id = "localgov_services_related_topics_block", * admin_label = @Translation("Service page related topics"), + * context_definitions = { + * "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = TRUE) + * } * ) */ class ServicesRelatedTopicsBlock extends ServicesBlockBase { @@ -24,10 +27,11 @@ class ServicesRelatedTopicsBlock extends ServicesBlockBase { public function build() { $build = []; $links = []; + $node = $this->getContextValue('node'); - if ($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, @@ -60,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; diff --git a/src/Plugin/Block/ServicesBlockBase.php b/src/Plugin/Block/ServicesBlockBase.php index ede7685..28771d2 100644 --- a/src/Plugin/Block/ServicesBlockBase.php +++ b/src/Plugin/Block/ServicesBlockBase.php @@ -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; @@ -60,9 +63,13 @@ 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) { + if (!$this->node instanceof NodeInterface && is_int($this->node)) { $node_storage = $this->entityTypeManager->getStorage('node'); $this->node = $node_storage->load($this->node); } @@ -86,14 +93,21 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { - return AccessResult::allowedIf($this->node); + $node = isset($this->getContextDefinitions()['node']) ? $this->getContextValue('node') : $this->node; + return AccessResult::allowedIf($node); } /** * {@inheritdoc} */ public function getCacheTags() { - return Cache::mergeTags(parent::getCacheTags(), ['node:' . $this->node->id()]); + $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(); + } } /** diff --git a/src/Plugin/Block/ServicesCtaBlock.php b/src/Plugin/Block/ServicesCtaBlock.php index af102ac..79bff0b 100644 --- a/src/Plugin/Block/ServicesCtaBlock.php +++ b/src/Plugin/Block/ServicesCtaBlock.php @@ -14,6 +14,9 @@ * @Block( * id = "localgov_service_cta_block", * admin_label = @Translation("Services call to action"), + * context_definitions = { + * "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = TRUE) + * } * ) */ class ServicesCtaBlock extends ServicesBlockBase { @@ -23,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(); } @@ -37,8 +41,12 @@ protected function blockAccess(AccountInterface $account) { */ public function build() { $buttons = []; + $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'; @@ -57,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'], ], ];