Skip to content

Commit

Permalink
Merge pull request #780 from UN-OCHA/berliner/HPC-9279
Browse files Browse the repository at this point in the history
HPC-9279: Add year switcher to plan name also on article and document pages
  • Loading branch information
berliner authored Nov 3, 2023
2 parents 04c79f6 + 8fb6e91 commit 098d3fb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/block.block.sectionswitcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ visibility:
context_mapping:
node: '@node.node_route_context:node'
bundles:
article: article
custom_subpage: custom_subpage
document: document
financials: financials
logframe: logframe
plan_cluster: plan_cluster
Expand Down
1 change: 1 addition & 0 deletions config/config_split.config_split.config_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ label: 'Config Dev'
description: 'Dev modules and settings not suitable for production.'
weight: 0
stackable: false
no_patching: false
storage: folder
folder: ../config_dev
module:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\ghi_base_objects\Traits\ShortNameTrait;
use Drupal\ghi_sections\Entity\GlobalSection;
use Drupal\ghi_sections\Entity\Homepage;
use Drupal\ghi_sections\Entity\SectionNodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -60,6 +62,20 @@ class SectionSwitcher extends BlockBase implements ContainerFactoryPluginInterfa
*/
protected $subpageManager;

/**
* The path alias manager.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected $aliasManager;

/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* {@inheritdoc}
*/
Expand All @@ -71,6 +87,8 @@ public static function create(ContainerInterface $container, array $configuratio
$instance->moduleHandler = $container->get('module_handler');
$instance->sectionManager = $container->get('ghi_sections.manager');
$instance->subpageManager = $container->get('ghi_subpages.manager');
$instance->aliasManager = $container->get('path_alias.manager');
$instance->requestStack = $container->get('request_stack');
return $instance;
}

Expand Down Expand Up @@ -151,9 +169,45 @@ private function getSectionNode() {
if ($section_node instanceof GlobalSection && !$section_node instanceof Homepage) {
return NULL;
}
if (!$section_node) {
// We can still try to deduce the section from the path.
$section_node = $this->getSectionNodeFromPath();
}
return $section_node;
}

/**
* Get the section node from the path.
*
* This processes the path recursively until it can find an alias that
* represents a section node.
*
* @param string $path
* The path to process.
*
* @return \Drupal\ghi_sections\Entity\SectionNodeInterface|null
* The section node or NULL if not found.
*/
private function getSectionNodeFromPath($path = NULL) {
$path = $path ?? $this->requestStack->getCurrentRequest()->getPathInfo();
$section = NULL;
$path_internal = $this->aliasManager->getPathByAlias($path);
try {
$params = Url::fromUri("internal:" . $path_internal)->getRouteParameters();
$entity_type = key($params);
$loaded = $this->entityTypeManager->getStorage($entity_type)->load($params[$entity_type]);
$section = $loaded instanceof SectionNodeInterface ? $loaded : NULL;
}
catch (\Exception $e) {
// Just catch any issue and pretend this didn't happen.
}
if (!$section && strrpos($path, '/') > 1) {
$path = substr($path, 0, strrpos($path, '/'));
$section = $this->getSectionNodeFromPath($path);
}
return $section instanceof SectionNodeInterface ? $section : NULL;
}

/**
* Get switcher options by a country reference on the sections base objects.
*
Expand Down

0 comments on commit 098d3fb

Please sign in to comment.