Skip to content

Commit

Permalink
Merge pull request #598 from City-of-Helsinki/UHF-8910_menu_publish
Browse files Browse the repository at this point in the history
UHF-8910 menu publish
  • Loading branch information
rpnykanen authored Oct 4, 2023
2 parents f560477 + 4a51f9a commit 2013f96
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions helfi_platform_config.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ services:
tags:
- { name: event_subscriber }

helfi_platform_config.filter_disabled_translations:
class: Drupal\helfi_platform_config\Menu\FilterDisabledTranslations
arguments:
- '@entity_type.manager'
- '@language_manager'
tags:
- { name: event_subscriber }

logger.channel.helfi_platform_config:
parent: logger.channel_base
arguments:
- 'helfi_platform_config'

82 changes: 82 additions & 0 deletions src/Menu/FilterDisabledTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_platform_config\Menu;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\menu_block_current_language\Event\Events;
use Drupal\menu_block_current_language\Event\HasTranslationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* UHF-8910 Filter menu items without enabled translation on current langcode.
*/
final class FilterDisabledTranslations implements EventSubscriberInterface {

/**
* The constructor.
*
* @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
*/
public function __construct(
readonly private EntityTypeManagerInterface $entityTypeManager,
readonly private LanguageManagerInterface $languageManager
) {
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
Events::HAS_TRANSLATION => [
['filter'],
],
];
}

/**
* Responds to Events::HAS_TRANSLATION event.
*
* @param Drupal\menu_block_current_language\Event\Events $event
* The event subscribed to.
*/
public function filter(HasTranslationEvent $event): void {
if (!$event->hasTranslation()) {
return;
}

$link = $event->getLink();
$metadata = $link->getMetaData();

if (empty($metadata['entity_id'])) {
return;
}

$current_language = $this->languageManager
->getCurrentLanguage()
->getId();

$entity = $this->entityTypeManager->getStorage('menu_link_content')
->load($metadata['entity_id']);

// MenuLinkContent entity has translation which isn't enabled, hide it.
if (
$entity->getMenuName() == 'main' &&
$entity->hasTranslation($current_language)
) {
$translation_enabled = (bool) $entity->getTranslation($current_language)
->content_translation_status
->value;
if (!$translation_enabled) {
$event->setHasTranslation(FALSE);
}
}
}

}

0 comments on commit 2013f96

Please sign in to comment.