Skip to content

Commit

Permalink
Merge pull request #584 from City-of-Helsinki/UHF-10906
Browse files Browse the repository at this point in the history
UHF-10906: Potential fix for sidebar issue
  • Loading branch information
khalima authored Nov 1, 2024
2 parents 30dfd8d + 0ba538c commit 88dde65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function _helfi_rekry_content_add_schema(string|NULL $url = NULL): ?string {
/**
* Implements hook_theme().
*/
function helfi_rekry_content_theme() {
function helfi_rekry_content_theme() : array {
return [
'organization_information_block' => [
'render element' => 'elements',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

namespace Drupal\helfi_rekry_content\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\helfi_platform_config\EntityVersionMatcher;
use Drupal\helfi_platform_config\Plugin\Block\ContentBlockBase;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a 'OrganizationInformation' block.
Expand All @@ -20,77 +15,18 @@
* admin_label = @Translation("Organization information block"),
* )
*/
final class OrganizationInformation extends BlockBase implements ContainerFactoryPluginInterface {

/**
* Constructs a new instance.
*
* @param array $configuration
* The configuration.
* @param string $plugin_id
* The plugin id.
* @param array $plugin_definition
* The plugin definition.
* @param \Drupal\helfi_platform_config\EntityVersionMatcher $entityVersionMatcher
* The entity version matcher.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
private readonly EntityVersionMatcher $entityVersionMatcher,
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition,
) : self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('helfi_platform_config.entity_version_matcher'),
$container->get('entity_type.manager'),
);
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
$matcher = $this->entityVersionMatcher->getType();

if (
!$matcher['entity'] ||
$matcher['entity_version'] == EntityVersionMatcher::ENTITY_VERSION_REVISION
) {
return parent::getCacheTags();
}
return Cache::mergeTags(parent::getCacheTags(), $matcher['entity']->getCacheTags());
}

/**
* {@inheritDoc}
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
}
final class OrganizationInformation extends ContentBlockBase {

/**
* {@inheritdoc}
*/
public function build() {
$build = [];
public function build() : array {
$build = [
'sidebar_content' => [
'#theme' => 'sidebar_content_block',
],
'#cache' => ['tags' => $this->getCacheTags()],
];

// Get current entity and entity version.
$entity_matcher = $this->entityVersionMatcher->getType();
Expand All @@ -105,8 +41,8 @@ public function build() {
$entity->hasField('field_organization_description')
) {
$view_builder = $this->entityTypeManager->getViewBuilder('node');
$build['organization_information'] = $view_builder->view($entity);
$build['organization_information']['#theme'] = 'organization_information_block';
$build['sidebar_content']['#computed'] = $view_builder->view($entity);
$build['sidebar_content']['#computed']['#theme'] = 'organization_information_block';
}

return $build;
Expand Down

0 comments on commit 88dde65

Please sign in to comment.