Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update service blocks to use a node route context #236

Draft
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -16,6 +16,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 {
Expand All @@ -25,7 +28,6 @@ class ServicesRelatedLinksBlock extends ServicesBlockBase implements ContainerFa
*/
public function build() {
$build = [];

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

if ($links) {
Expand All @@ -46,9 +48,10 @@ public function build() {
*/
private function buildManual() {
$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'],
Expand Down Expand Up @@ -82,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 @@ -92,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 @@ -120,8 +125,9 @@ private function buildAutomated() {
* Should manual links be displayed?
*/
private function getShouldUseManual() {
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 && $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 @@ -135,11 +141,12 @@ private function getShouldUseManual() {
*/
private function getTopics() {
$topics = [];
$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) {
$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 @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 17 additions & 3 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,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);
}
Expand All @@ -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();
}
}

/**
Expand Down
18 changes: 13 additions & 5 deletions src/Plugin/Block/ServicesCtaBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
Expand All @@ -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';
Expand All @@ -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'],
],
];
Expand Down
Loading