diff --git a/helfi_platform_config.services.yml b/helfi_platform_config.services.yml index cc005d4a3..607ec38f5 100644 --- a/helfi_platform_config.services.yml +++ b/helfi_platform_config.services.yml @@ -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' + diff --git a/src/Menu/FilterDisabledTranslations.php b/src/Menu/FilterDisabledTranslations.php new file mode 100644 index 000000000..fb2d0f7ae --- /dev/null +++ b/src/Menu/FilterDisabledTranslations.php @@ -0,0 +1,82 @@ + [ + ['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); + } + } + } + +}