diff --git a/src/Entity/AlertBannerEntity.php b/src/Entity/AlertBannerEntity.php index 1fbab7e..bc1a06f 100644 --- a/src/Entity/AlertBannerEntity.php +++ b/src/Entity/AlertBannerEntity.php @@ -314,6 +314,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('Title')) ->setDescription(t('The title of the Alert banner.')) ->setRevisionable(TRUE) + ->setTranslatable(TRUE) ->setSettings([ 'max_length' => 50, 'text_processing' => 0, diff --git a/src/Plugin/Block/AlertBannerBlock.php b/src/Plugin/Block/AlertBannerBlock.php index 2bfcc60..5e62de6 100644 --- a/src/Plugin/Block/AlertBannerBlock.php +++ b/src/Plugin/Block/AlertBannerBlock.php @@ -4,6 +4,7 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -36,6 +37,13 @@ class AlertBannerBlock extends BlockBase implements ContainerFactoryPluginInterf */ protected $currentUser; + /** + * The entity repository services. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + /** * Constructs a new AlertBannerBlock. * @@ -49,11 +57,14 @@ class AlertBannerBlock extends BlockBase implements ContainerFactoryPluginInterf * The entity type manager service. * @param \Drupal\Core\Session\AccountProxyInterface $current_user * Current user service. + * @param Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, EntityRepositoryInterface $entity_repository) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; $this->currentUser = $current_user; + $this->entityRepository = $entity_repository; } /** @@ -76,7 +87,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('entity_type.manager'), - $container->get('current_user') + $container->get('current_user'), + $container->get('entity.repository'), ); } @@ -191,6 +203,8 @@ protected function getCurrentAlertBanners() { // Visibility check happens in build, so we get cache contexts on all. foreach ($published_alert_banners as $alert_banner_id) { $alert_banner = $this->entityTypeManager->getStorage('localgov_alert_banner')->load($alert_banner_id); + $alert_banner = $this->entityRepository->getTranslationFromContext($alert_banner); + $is_accessible = $alert_banner->access('view', $this->currentUser); if ($is_accessible) { $current_alert_banners[] = $alert_banner; diff --git a/tests/src/Functional/TranslationsTest.php b/tests/src/Functional/TranslationsTest.php new file mode 100644 index 0000000..9e24d5c --- /dev/null +++ b/tests/src/Functional/TranslationsTest.php @@ -0,0 +1,240 @@ +drupalPlaceBlock('localgov_alert_banner_block'); + ConfigurableLanguage::createFromLangcode('zz')->save(); + $this->container->get('content_translation.manager')->setEnabled('localgov_alert_banner', 'localgov_alert_banner', TRUE); + FieldConfig::loadByName('localgov_alert_banner', 'localgov_alert_banner', 'short_description')->setTranslatable(TRUE)->save(); + } + + /** + * Test that valid translation is brought back based on current language. + */ + public function testAlertBannerTranslation(): void { + + $default_langcode = $this->container->get('language.default')->get()->getId(); + + // Create alert banner. + $alert_title = 'home page alert title - ' . $this->randomMachineName(8); + $alert_body = 'home page alert body - ' . $this->randomMachineName(32); + $alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner') + ->create([ + 'type' => 'localgov_alert_banner', + 'title' => $alert_title, + 'short_description' => $alert_body, + 'type_of_alert' => 'minor', + 'moderation_state' => 'published', + 'langcode' => $default_langcode, + ]); + $alert->save(); + + // Create translation. + $translated_alert_title = 'translated home page alert title - ' . $this->randomMachineName(8); + $translated_alert_body = 'translated home page alert body - ' . $this->randomMachineName(32); + $alert->addTranslation('zz', [ + 'title' => $translated_alert_title, + 'short_description' => $translated_alert_body, + 'type_of_alert' => 'minor', + 'moderation_state' => 'published', + ])->save(); + + // Test on home page. + $this->drupalGet(''); + $this->assertSession()->pageTextContains($alert_title); + $this->assertSession()->pageTextContains($alert_body); + $this->assertSession()->pageTextNotContains($translated_alert_title); + $this->assertSession()->pageTextNotContains($translated_alert_body); + + // Switch language. + $this->drupalGet('/zz'); + + // Test correct translation on home page. + $this->assertSession()->pageTextNotContains($alert_title); + $this->assertSession()->pageTextNotContains($alert_body); + $this->assertSession()->pageTextContains($translated_alert_title); + $this->assertSession()->pageTextContains($translated_alert_body); + + // Create node. + $this->drupalCreateContentType(['type' => 'page']); + $page = $this->createNode([ + 'type' => 'page', + 'title' => $this->randomMachineName(8), + 'status' => NodeInterface::PUBLISHED, + 'langcode' => $default_langcode, + ]); + + // Add node translation. + $page->addTranslation('zz', [ + 'title' => $this->randomMachineName(8), + 'status' => NodeInterface::PUBLISHED, + ]); + + // Create banner with page restriction. + $alert_title_node = 'node 1 alert title - ' . $this->randomMachineName(8); + $alert_body_node = 'node 1 alert body - ' . $this->randomMachineName(32); + $node_alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner') + ->create([ + 'type' => 'localgov_alert_banner', + 'title' => $alert_title_node, + 'short_description' => $alert_body_node, + 'type_of_alert' => 'minor', + 'moderation_state' => 'published', + 'langcode' => $default_langcode, + 'visibility' => [ + 'conditions' => [ + 'request_path' => [ + 'pages' => '/node/1', + 'negate' => 0, + ], + ], + ], + ]); + $node_alert->save(); + + // Translate banner with page restriction. + $translated_alert_title_node = 'translated node 1 alert title - ' . $this->randomMachineName(8); + $translated_alert_body_node = 'translated node 1 alert body - ' . $this->randomMachineName(32); + $node_alert->addTranslation('zz', [ + 'title' => $translated_alert_title_node, + 'short_description' => $translated_alert_body_node, + 'type_of_alert' => 'minor', + 'moderation_state' => 'published', + ])->save(); + + // Go to node in default language. + $this->drupalGet('/node/1'); + + // Test correct translation appears. + $this->assertSession()->pageTextContains($alert_title_node); + $this->assertSession()->pageTextContains($alert_body_node); + $this->assertSession()->pageTextNotContains($translated_alert_title_node); + $this->assertSession()->pageTextNotContains($translated_alert_body_node); + + // Change language. + $this->drupalGet('/zz/node/1'); + + // Test correct translation appears. + $this->assertSession()->pageTextNotContains($alert_title_node); + $this->assertSession()->pageTextNotContains($alert_body_node); + $this->assertSession()->pageTextContains($translated_alert_title_node); + $this->assertSession()->pageTextContains($translated_alert_body_node); + + // Create node with path. + $page = $this->createNode([ + 'type' => 'page', + 'title' => $this->randomMachineName(8), + 'status' => NodeInterface::PUBLISHED, + 'langcode' => $default_langcode, + ]); + $this->container->get('entity_type.manager')->getStorage('path_alias')->create([ + 'path' => '/node/' . $page->id(), + 'alias' => '/untranslated-path', + 'langcode' => $default_langcode, + ])->save(); + + // Add node translation. + $page->addTranslation('zz', [ + 'title' => $this->randomMachineName(8), + 'status' => NodeInterface::PUBLISHED, + ]); + $this->container->get('entity_type.manager')->getStorage('path_alias')->create([ + 'path' => '/node/' . $page->id(), + 'alias' => '/translated-path', + 'langcode' => 'zz', + ])->save(); + + // Enable translation of the visibility field. + FieldConfig::loadByName('localgov_alert_banner', 'localgov_alert_banner', 'visibility')->setTranslatable(TRUE)->save(); + + // Create banner with page restriction. + $alert_title_pa_node = 'path alias page alert title - ' . $this->randomMachineName(8); + $alert_body_pa_node = 'path alias page alert body - ' . $this->randomMachineName(32); + $pa_node_alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner') + ->create([ + 'type' => 'localgov_alert_banner', + 'title' => $alert_title_pa_node, + 'short_description' => $alert_body_pa_node, + 'type_of_alert' => 'minor', + 'moderation_state' => 'published', + 'langcode' => $default_langcode, + 'visibility' => [ + 'conditions' => [ + 'request_path' => [ + 'pages' => '/untranslated-path', + 'negate' => 0, + ], + ], + ], + ]); + $node_alert->save(); + + // Translate banner with page restriction. + $translated_alert_title_pa_node = 'translated path alias page alert title - ' . $this->randomMachineName(8); + $translated_alert_body_pa_node = 'translated path alias page alert body - ' . $this->randomMachineName(32); + $pa_node_alert->addTranslation('zz', [ + 'title' => $translated_alert_title_pa_node, + 'short_description' => $translated_alert_body_pa_node, + 'type_of_alert' => 'minor', + 'moderation_state' => 'published', + ])->save(); + + // Go to node in default language. + $this->drupalGet('/untranslated-path'); + + // Test correct translation appears. + $this->assertSession()->pageTextContains($alert_title_pa_node); + $this->assertSession()->pageTextContains($alert_body_pa_node); + $this->assertSession()->pageTextNotContains($translated_alert_title_pa_node); + $this->assertSession()->pageTextNotContains($translated_alert_body_pa_node); + + // Change language. + $this->drupalGet('/zz/translated-path'); + + // Test correct translation appears. + $this->assertSession()->pageTextNotContains($alert_title_pa_node); + $this->assertSession()->pageTextNotContains($alert_body_pa_node); + $this->assertSession()->pageTextContains($translated_alert_title_pa_node); + $this->assertSession()->pageTextContains($translated_alert_body_pa_node); + } + +}