Skip to content

Commit

Permalink
Merge pull request #807 from City-of-Helsinki/UHF-10554
Browse files Browse the repository at this point in the history
UHF-10554: Fix announcements and surveys for alt languages
  • Loading branch information
hyrsky authored Sep 12, 2024
2 parents 3002d13 + 18795e9 commit 9e9b0e9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions modules/helfi_etusivu_entities/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Remote entities module allows fetching announcements from `Etusivu`-instance.
It utilizes `json-api` and `external_entities`-module to transfer the data between instances.

## Language support

If current language is not in [default languages](https://github.com/City-of-Helsinki/drupal-module-helfi-api-base/blob/main/documentation/default-languages.md), remote entities use fallback language. Alt languages show local announcements in current language (if translated) and in English.

## How to set up locally

Local setup requires Etusivu-instance to be up and running with some relevant data created to it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Drupal\external_entities\StorageClient\ExternalEntityStorageClientBase;
use Drupal\helfi_api_base\Environment\Environment;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\helfi_api_base\Language\DefaultLanguageResolver;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
Expand Down Expand Up @@ -62,6 +63,11 @@ abstract class EtusivuJsonApiEntityBase extends ExternalEntityStorageClientBase
*/
protected CacheBackendInterface $cache;

/**
* Default language resolver.
*/
protected DefaultLanguageResolver $defaultLanguageResolver;

/**
* {@inheritdoc}
*/
Expand All @@ -75,6 +81,7 @@ public static function create(
$instance->client = $container->get('http_client');
$instance->languageManager = $container->get('language_manager');
$instance->cache = $container->get('cache.default');
$instance->defaultLanguageResolver = $container->get(DefaultLanguageResolver::class);

/** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */
$environmentResolver = $container->get('helfi_api_base.environment_resolver');
Expand Down Expand Up @@ -153,6 +160,10 @@ protected function request(string $endpoint, array $parameters, string $langcode
return [];
}

if ($this->defaultLanguageResolver->isAltLanguage($langcode)) {
$langcode = $this->defaultLanguageResolver->getFallbackLanguage();
}

$uri = vsprintf('%s/jsonapi%s?%s', [
$this->environment->getInternalAddress($langcode),
$endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\Tests\helfi_etusivu_entities\Functional;

use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\NodeInterface;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
Expand Down Expand Up @@ -45,6 +46,8 @@ final class LocalEntitiesTest extends BrowserTestBase {
public function setUp(): void {
parent::setUp();

ConfigurableLanguage::createFromLangcode('uk')->save();

$this->testNode = $this->createNode([
'status' => NodeInterface::PUBLISHED,
'type' => 'page',
Expand Down Expand Up @@ -99,17 +102,32 @@ public function testBlocks(): void {
'type' => 'announcement',
'title' => 'Test announcement',
'body' => 'Announcement content',
'field_announcement_type' => 'notification',
'langcode' => 'en',
]);

$this->drupalGet('/');
$this->createNode([
'status' => NodeInterface::PUBLISHED,
'type' => 'announcement',
'title' => 'UK announcement (uk)',
'body' => 'Announcement content',
'field_announcement_type' => 'notification',
'langcode' => 'uk',
]);

$this->drupalGet('/');
$this->assertSession()->pageTextContains('Old test survey');
$this->assertSession()->pageTextNotContains('New test survey');
$this->assertSession()->pageTextContains('Test announcement');

$this->drupalGet($this->testNode->toUrl());
// Local announcements can be translated.pages
// English announcements are shown on alt language pages.
$this->drupalGet('/', ['language' => \Drupal::languageManager()->getLanguage('uk')]);
$this->assertSession()->pageTextContains('UK announcement');
$this->assertSession()->pageTextContains('Test announcement');

// Tests page filtering.
$this->drupalGet($this->testNode->toUrl());
$this->assertSession()->pageTextContains('New test survey');
$this->assertSession()->pageTextNotContains('Old test survey');
$this->assertSession()->pageTextContains('Test announcement');
Expand Down

0 comments on commit 9e9b0e9

Please sign in to comment.