-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #598 from City-of-Helsinki/UHF-8910_menu_publish
UHF-8910 menu publish
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |