From 81ffc7b3d19ef8750c39e4cf6e95d3582bd079de Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 7 May 2024 14:53:44 +0200 Subject: [PATCH] add module settings service extension and shop configuration extension --- README.en.md | 9 ++++- README.md | 10 ++++-- services.yaml | 6 ++++ src/ModuleSettingsServiceExtension.php | 50 ++++++++++++++++++++++++++ src/ShopConfigurationExtension.php | 46 ++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/ModuleSettingsServiceExtension.php create mode 100644 src/ShopConfigurationExtension.php diff --git a/README.en.md b/README.en.md index 62f0353..6701649 100644 --- a/README.en.md +++ b/README.en.md @@ -6,12 +6,19 @@ additional extensions that can be used in the shop's Twig templates: - method_exists - Checks whether the method of an object exists. - ``` {% if method_exists(entity, 'getCreatedBy') %} ({{ entity.createdBy.name }}) {% endif %} ``` +- getModuleSettingsService - get the module settings service + ``` + {{ getModuleSettingsService().getString('myModuleSetting', 'myModuleId') }} + ``` +- getShopConfiguration - get the shop configuration + ``` + {{ getShopConfiguration().getShopId() }} + ``` ## Table of content diff --git a/README.md b/README.md index c94d371..d840cc1 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,19 @@ zusätzliche Erweiterungen, die in den Twig-Templates des Shops verwendet werden können: - method_exists - Prüft, ob die Methode eines Objekts existiert. - ``` {% if method_exists(entity, 'getCreatedBy') %} ({{ entity.createdBy.name }}) {% endif %} ``` +- getModuleSettingsService - liefert den Moduleinstellungsservice + ``` + {{ getModuleSettingsService().getString('myModuleSetting', 'myModuleId') }} + ``` +- getShopConfiguration - liefert die Shopkonfiguration + ``` + {{ getShopConfiguration().getShopId() }} + ``` ## Inhaltsverzeichnis @@ -27,7 +34,6 @@ Dieses Paket erfordert einen mit Composer installierten OXID eShop in einer in d Öffnen Sie eine Kommandozeile und navigieren Sie zum Stammverzeichnis des Shops (Elternverzeichnis von source und vendor). Führen Sie den folgenden Befehl aus. Passen Sie die Pfadangaben an Ihre Installationsumgebung an. - ```bash php composer require d3/oxid-twig-extensions:^1.0 ``` diff --git a/services.yaml b/services.yaml index a740e58..799bb8b 100644 --- a/services.yaml +++ b/services.yaml @@ -3,4 +3,10 @@ services: autowire: true D3\OxidTwigExtensions\MethodExistsExtension: + tags: ['twig.extension'] + + D3\OxidTwigExtensions\ModuleSettingsServiceExtension: + tags: ['twig.extension'] + + D3\OxidTwigExtensions\ShopConfigurationExtension: tags: ['twig.extension'] \ No newline at end of file diff --git a/src/ModuleSettingsServiceExtension.php b/src/ModuleSettingsServiceExtension.php new file mode 100644 index 0000000..b3f2df1 --- /dev/null +++ b/src/ModuleSettingsServiceExtension.php @@ -0,0 +1,50 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\OxidTwigExtensions; + +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class ModuleSettingsServiceExtension extends AbstractExtension +{ + /** + * @return TwigFunction[] + */ + public function getFunctions(): array + { + return [ + new TwigFunction( + 'getModuleSettingsService', + [$this, 'getModuleSettingsService'], + ['is_safe' => ['html']] + ) + ]; + } + + /** + * @return ModuleSettingServiceInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function getModuleSettingsService(): ModuleSettingServiceInterface + { + return ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class); + } +} \ No newline at end of file diff --git a/src/ShopConfigurationExtension.php b/src/ShopConfigurationExtension.php new file mode 100644 index 0000000..9ce8ff2 --- /dev/null +++ b/src/ShopConfigurationExtension.php @@ -0,0 +1,46 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\OxidTwigExtensions; + +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\EshopCommunity\Core\Config; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class ShopConfigurationExtension extends AbstractExtension +{ + /** + * @return TwigFunction[] + */ + public function getFunctions(): array + { + return [ + new TwigFunction( + 'getShopConfiguration', + [$this, 'getShopConfiguration'], + ['is_safe' => ['html']] + ) + ]; + } + + /** + * @return Config + */ + public function getShopConfiguration(): Config + { + return Registry::getConfig(); + } +} \ No newline at end of file