From b479abc6887e28397c4c54f66a6e10c2775d52a8 Mon Sep 17 00:00:00 2001 From: tuutti Date: Fri, 13 Oct 2023 09:49:23 +0300 Subject: [PATCH 1/4] Make sure to always use same environment to fetch global announcements and news --- .../StorageClient/Announcements.php | 18 +--------- .../src/HelfiExternalEntityBase.php | 35 +++++-------------- 2 files changed, 10 insertions(+), 43 deletions(-) diff --git a/modules/helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php b/modules/helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php index c09eafcd4..31072e763 100644 --- a/modules/helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php +++ b/modules/helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php @@ -71,24 +71,8 @@ public static function create( /** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */ $environmentResolver = $container->get('helfi_api_base.environment_resolver'); - - // Attempt to read environment from config override and fallback to - // currently active environment. - $environment = $container->get('config.factory') - ->get('helfi_global_announcement.settings') - ->get('source_environment'); - - if (!$environment) { - try { - $environment = $environmentResolver->getActiveEnvironment() - ->getEnvironmentName(); - } - catch (\InvalidArgumentException) { - $environment = 'prod'; - } - } $instance->environment = $environmentResolver - ->getEnvironment(Project::ETUSIVU, $environment); + ->getEnvironment(Project::ETUSIVU, $environmentResolver->getActiveEnvironmentName()); return $instance; } diff --git a/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php b/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php index 71dd47415..e744e7719 100644 --- a/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php +++ b/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php @@ -52,11 +52,11 @@ abstract class HelfiExternalEntityBase extends ExternalEntityStorageClientBase { protected string $endpoint; /** - * The environment resolver. + * The active endpoint environment. * - * @var \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface + * @var \Drupal\helfi_api_base\Environment\Environment */ - protected EnvironmentResolverInterface $environmentResolver; + private Environment $environment; /** * The config factory service. @@ -77,8 +77,12 @@ public static function create( $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); $instance->languageManager = $container->get('language_manager'); $instance->client = $container->get('http_client'); - $instance->environmentResolver = $container->get('helfi_api_base.environment_resolver'); $instance->configFactory = $container->get('config.factory'); + /** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */ + $environmentResolver = $container->get('helfi_api_base.environment_resolver'); + $instance->environment = $environmentResolver + ->getEnvironment(Project::ETUSIVU, $environmentResolver->getActiveEnvironmentName()); + return $instance; } @@ -191,27 +195,6 @@ public function delete(ExternalEntityInterface $entity) : void { throw new EntityStorageException('::delete() is not supported.'); } - /** - * Gets the endpoint environment. - * - * @return \Drupal\helfi_api_base\Environment\Environment - * The environment. - */ - protected function getEnvironment() : Environment { - static $environment = NULL; - - if ($environment === NULL) { - $environment = $this->configFactory->get('helfi_paragraphs_news_list.settings') - ->get('source_environment') ?: EnvironmentEnum::Prod->value; - - /** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */ - $environment = $this->environmentResolver - ->getEnvironment(Project::ETUSIVU, $environment); - } - - return $environment; - } - /** * Formats the JSON response. * @@ -318,7 +301,7 @@ protected function request( ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) ->getId(); $uri = vsprintf('%s%s?%s', [ - $this->getEnvironment()->getInternalAddress($langcode), + $this->environment->getInternalAddress($langcode), $this->endpoint, \GuzzleHttp\http_build_query($parameters), ]); From 78b6871129b484a29e30b7ab2b7361734d9674aa Mon Sep 17 00:00:00 2001 From: tuutti Date: Fri, 13 Oct 2023 09:57:53 +0300 Subject: [PATCH 2/4] Updated documentation, removed setting --- modules/helfi_global_announcement/README.md | 3 --- modules/helfi_paragraphs_news_list/README.md | 9 --------- .../config/schema/helfi_paragraphs_news_list.schema.yml | 6 ------ 3 files changed, 18 deletions(-) delete mode 100644 modules/helfi_paragraphs_news_list/config/schema/helfi_paragraphs_news_list.schema.yml diff --git a/modules/helfi_global_announcement/README.md b/modules/helfi_global_announcement/README.md index cae3e7b23..a96ea1f32 100644 --- a/modules/helfi_global_announcement/README.md +++ b/modules/helfi_global_announcement/README.md @@ -7,6 +7,3 @@ Global announcements can be fetched to any instance and rendered using blocks. ## How to set up locally Local setup requires Etusivu-instance to be up and running with some relevant data created to it. - -Add following line to local.settings.php in order to connect to local etusivu-instance -`$config['helfi_global_announcement.settings']['source_environment'] = 'local'` diff --git a/modules/helfi_paragraphs_news_list/README.md b/modules/helfi_paragraphs_news_list/README.md index 5675922b0..83ccd9a5d 100644 --- a/modules/helfi_paragraphs_news_list/README.md +++ b/modules/helfi_paragraphs_news_list/README.md @@ -8,15 +8,6 @@ News items are fetched runtime using [external_entities](https://www.drupal.org/ Enable the `helfi_paragraphs_news_list` module. -## Overriding the source environment - -The source environment (production by default) can be changed by overriding the `helfi_paragraphs_news_list.settings.source_environment` configuration. - -For example: -```php -# public/sites/default/local.settings.php -$config['helfi_paragraphs_news_list.settings']['source_environment'] = 'dev'; -``` ## How to add new fields to paragraph type diff --git a/modules/helfi_paragraphs_news_list/config/schema/helfi_paragraphs_news_list.schema.yml b/modules/helfi_paragraphs_news_list/config/schema/helfi_paragraphs_news_list.schema.yml deleted file mode 100644 index d7024e20f..000000000 --- a/modules/helfi_paragraphs_news_list/config/schema/helfi_paragraphs_news_list.schema.yml +++ /dev/null @@ -1,6 +0,0 @@ -helfi_paragraphs_news_list.settings: - type: mapping - mapping: - source_environment: - type: string - label: 'The source environment' From b70b85f6ba8d0e0cf7ceb9e3ad0b9b08b0fde6c7 Mon Sep 17 00:00:00 2001 From: tuutti Date: Fri, 13 Oct 2023 10:41:11 +0300 Subject: [PATCH 3/4] phpcs fixes --- .../helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php b/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php index e744e7719..7f1c542d7 100644 --- a/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php +++ b/modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php @@ -9,8 +9,6 @@ use Drupal\external_entities\ExternalEntityInterface; use Drupal\external_entities\StorageClient\ExternalEntityStorageClientBase; use Drupal\helfi_api_base\Environment\Environment; -use Drupal\helfi_api_base\Environment\EnvironmentEnum; -use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; use Drupal\helfi_api_base\Environment\Project; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; From 6826718c873b902f1d3c23e54389f8dd241d817a Mon Sep 17 00:00:00 2001 From: tuutti Date: Fri, 13 Oct 2023 11:04:19 +0300 Subject: [PATCH 4/4] Fixed test --- .../tests/src/Kernel/NewsExternalEntityTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/helfi_paragraphs_news_list/tests/src/Kernel/NewsExternalEntityTest.php b/modules/helfi_paragraphs_news_list/tests/src/Kernel/NewsExternalEntityTest.php index 5ea4a2a44..c7d92194a 100644 --- a/modules/helfi_paragraphs_news_list/tests/src/Kernel/NewsExternalEntityTest.php +++ b/modules/helfi_paragraphs_news_list/tests/src/Kernel/NewsExternalEntityTest.php @@ -6,7 +6,10 @@ use Drupal\Core\Entity\EntityStorageException; use Drupal\external_entities\ExternalEntityStorageInterface; +use Drupal\helfi_api_base\Environment\EnvironmentEnum; +use Drupal\helfi_api_base\Environment\Project; use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait; +use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait; use Drupal\Tests\helfi_api_base\Traits\TestLoggerTrait; use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\RequestInterface; @@ -20,6 +23,7 @@ class NewsExternalEntityTest extends KernelTestBase { use ApiTestTrait; use TestLoggerTrait; + use EnvironmentResolverTrait; /** * {@inheritdoc} @@ -27,6 +31,7 @@ class NewsExternalEntityTest extends KernelTestBase { public function setUp(): void { parent::setUp(); $this->setUpMockLogger(); + $this->setActiveProject(Project::ASUMINEN, EnvironmentEnum::Local); } /**