From d09fa6c3d40728a62b2bae178e36a2dcc67bc44d Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Thu, 11 Apr 2024 23:36:22 +0200 Subject: [PATCH 1/2] Make images in image and video element use liip imagine filter --- src/Resources/config/config.yaml | 9 +++++++++ src/Resources/config/images.yaml | 1 + src/Resources/config/services.yaml | 1 + .../views/Admin/UiElement/image.html.twig | 4 ++-- .../Admin/UiElement/image_collection.html.twig | 4 ++-- .../views/Admin/UiElement/video.html.twig | 2 +- src/Resources/views/Form/theme.html.twig | 2 +- .../views/Shop/UiElement/image.html.twig | 4 ++-- .../Shop/UiElement/image_collection.html.twig | 4 ++-- .../views/Shop/UiElement/video.html.twig | 2 +- src/Twig/RichEditorExtension.php | 16 +++++++++++++++- 11 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/Resources/config/config.yaml b/src/Resources/config/config.yaml index b7c77796..0e8ad07d 100644 --- a/src/Resources/config/config.yaml +++ b/src/Resources/config/config.yaml @@ -13,3 +13,12 @@ knp_gaufrette: filesystems: monsieurbiz_rich_editor_fixture_file: adapter: 'monsieurbiz_rich_editor_fixture_file' + +liip_imagine: + loaders: + default: + filesystem: + data_root: + # @TODO manage to use the configuration parameter + # - "%sylius_core.public_dir%/%monsieurbiz.richeditor.config.upload_directory%" + - "%sylius_core.public_dir%/media/rich-editor" diff --git a/src/Resources/config/images.yaml b/src/Resources/config/images.yaml index ce90e568..f034af7c 100644 --- a/src/Resources/config/images.yaml +++ b/src/Resources/config/images.yaml @@ -4,3 +4,4 @@ liip_imagine: filters: thumbnail: { size: [400, 400], mode: inset } relative_resize: { widen: 32 } + monsieurbiz_rich_editor_image: ~ diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index c899cd58..068ea8c6 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -7,6 +7,7 @@ services: bind: string $monsieurbizRicheditorDefaultElement: '%monsieurbiz.richeditor.config.default_element%' string $monsieurbizRicheditorDefaultElementDataField: '%monsieurbiz.richeditor.config.default_element_data_field%' + string $monsieurbizRicheditorUploadDirectory: '%monsieurbiz.richeditor.config.upload_directory%' Gaufrette\FilesystemInterface $filesystem: '@gaufrette.monsieurbiz_rich_editor_fixture_file_filesystem' MonsieurBiz\SyliusRichEditorPlugin\Controller\: diff --git a/src/Resources/views/Admin/UiElement/image.html.twig b/src/Resources/views/Admin/UiElement/image.html.twig index 01d59184..bb7513ad 100644 --- a/src/Resources/views/Admin/UiElement/image.html.twig +++ b/src/Resources/views/Admin/UiElement/image.html.twig @@ -13,10 +13,10 @@

{% if element.link is not empty %} - {{ element.alt|default('') }} + {{ element.alt|default('') }} {% else %} - {{ element.alt|default('') }} + {{ element.alt|default('') }} {% endif %}

{% endif %} diff --git a/src/Resources/views/Admin/UiElement/image_collection.html.twig b/src/Resources/views/Admin/UiElement/image_collection.html.twig index 2875d203..79f1e772 100644 --- a/src/Resources/views/Admin/UiElement/image_collection.html.twig +++ b/src/Resources/views/Admin/UiElement/image_collection.html.twig @@ -9,10 +9,10 @@
{% if image.link is not empty %} - {{ image.alt|default('') }} + {{ image.alt|default('') }} {% else %} - {{ image.alt|default('') }} + {{ image.alt|default('') }} {% endif %}
{% endfor %} diff --git a/src/Resources/views/Admin/UiElement/video.html.twig b/src/Resources/views/Admin/UiElement/video.html.twig index 7c4c298d..cd4b7a8d 100644 --- a/src/Resources/views/Admin/UiElement/video.html.twig +++ b/src/Resources/views/Admin/UiElement/video.html.twig @@ -8,7 +8,7 @@ #} {% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
-
diff --git a/src/Resources/views/Form/theme.html.twig b/src/Resources/views/Form/theme.html.twig index a7177c4f..bec62a4f 100644 --- a/src/Resources/views/Form/theme.html.twig +++ b/src/Resources/views/Form/theme.html.twig @@ -48,7 +48,7 @@ {% if form.vars.attr['data-image'] is defined %}

{{ 'monsieurbiz_richeditor_plugin.form.current_image' | trans }}
- +

{% elseif form.vars.attr['data-video'] is defined %}

diff --git a/src/Resources/views/Shop/UiElement/image.html.twig b/src/Resources/views/Shop/UiElement/image.html.twig index 01d59184..bb7513ad 100644 --- a/src/Resources/views/Shop/UiElement/image.html.twig +++ b/src/Resources/views/Shop/UiElement/image.html.twig @@ -13,10 +13,10 @@

{% if element.link is not empty %} - {{ element.alt|default('') }} + {{ element.alt|default('') }} {% else %} - {{ element.alt|default('') }} + {{ element.alt|default('') }} {% endif %}

{% endif %} diff --git a/src/Resources/views/Shop/UiElement/image_collection.html.twig b/src/Resources/views/Shop/UiElement/image_collection.html.twig index 2875d203..79f1e772 100644 --- a/src/Resources/views/Shop/UiElement/image_collection.html.twig +++ b/src/Resources/views/Shop/UiElement/image_collection.html.twig @@ -9,10 +9,10 @@
{% if image.link is not empty %} - {{ image.alt|default('') }} + {{ image.alt|default('') }} {% else %} - {{ image.alt|default('') }} + {{ image.alt|default('') }} {% endif %}
{% endfor %} diff --git a/src/Resources/views/Shop/UiElement/video.html.twig b/src/Resources/views/Shop/UiElement/video.html.twig index 7c4c298d..cd4b7a8d 100644 --- a/src/Resources/views/Shop/UiElement/video.html.twig +++ b/src/Resources/views/Shop/UiElement/video.html.twig @@ -8,7 +8,7 @@ #} {% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
-
diff --git a/src/Twig/RichEditorExtension.php b/src/Twig/RichEditorExtension.php index fc9c4383..8dc28af7 100644 --- a/src/Twig/RichEditorExtension.php +++ b/src/Twig/RichEditorExtension.php @@ -38,6 +38,8 @@ final class RichEditorExtension extends AbstractExtension private string $defaultElementDataField; + private string $uploadDirectory; + /** * RichEditorExtension constructor. */ @@ -45,12 +47,14 @@ public function __construct( RegistryInterface $uiElementRegistry, Environment $twig, string $monsieurbizRicheditorDefaultElement, - string $monsieurbizRicheditorDefaultElementDataField + string $monsieurbizRicheditorDefaultElementDataField, + string $monsieurbizRicheditorUploadDirectory ) { $this->uiElementRegistry = $uiElementRegistry; $this->twig = $twig; $this->defaultElement = $monsieurbizRicheditorDefaultElement; $this->defaultElementDataField = $monsieurbizRicheditorDefaultElementDataField; + $this->uploadDirectory = $monsieurbizRicheditorUploadDirectory; } /** @@ -78,6 +82,7 @@ public function getFunctions(): array new TwigFunction('monsieurbiz_richeditor_get_default_element', [$this, 'getDefaultElement'], ['is_safe' => ['html']]), new TwigFunction('monsieurbiz_richeditor_get_default_element_data_field', [$this, 'getDefaultElementDataField'], ['is_safe' => ['html']]), new TwigFunction('monsieurbiz_richeditor_get_current_file_path', [$this, 'getCurrentFilePath'], ['needs_context' => true, 'is_safe' => ['html']]), + new TwigFunction('monsieurbiz_richeditor_get_media_without_upload_dir', [$this, 'getMediaWithoutUploadDir'], ['is_safe' => ['html', 'js']]), ]; } @@ -238,6 +243,15 @@ public function getCurrentFilePath(array $context, string $varName = 'full_name' return $path; } + public function getMediaWithoutUploadDir(string $path): string + { + if (0 === strpos($path, $this->uploadDirectory)) { + return substr($path, \strlen($this->uploadDirectory)); + } + + return $path; + } + private function isAdmin(array $context): bool { /** @var ?AppVariable $app */ From a194152b5f0b9748f9951bf60ffd80d4de6708a0 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Thu, 9 May 2024 16:51:01 +0200 Subject: [PATCH 2/2] Use env var to define upload dir and image upload dir --- recipes/2.0/manifest.json | 4 ++++ src/Resources/config/config.yaml | 8 +++++--- src/Resources/config/richeditor.yaml | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/recipes/2.0/manifest.json b/recipes/2.0/manifest.json index a33a848a..b13c2b36 100644 --- a/recipes/2.0/manifest.json +++ b/recipes/2.0/manifest.json @@ -6,5 +6,9 @@ }, "copy-from-recipe": { "config/": "%CONFIG_DIR%/" + }, + "env": { + "MONSIEURBIZ_SYLIUS_RICH_EDITOR_UPLOAD_DIR": "/media/rich-editor", + "MONSIEURBIZ_SYLIUS_RICH_EDITOR_IMAGE_UPLOAD_DIR": "/media/image" } } diff --git a/src/Resources/config/config.yaml b/src/Resources/config/config.yaml index 0e8ad07d..fc87185a 100644 --- a/src/Resources/config/config.yaml +++ b/src/Resources/config/config.yaml @@ -14,11 +14,13 @@ knp_gaufrette: monsieurbiz_rich_editor_fixture_file: adapter: 'monsieurbiz_rich_editor_fixture_file' +parameters: + env(MONSIEURBIZ_SYLIUS_RICH_EDITOR_UPLOAD_DIR): /media/rich-editor + env(MONSIEURBIZ_SYLIUS_RICH_EDITOR_IMAGE_UPLOAD_DIR): /media/image + liip_imagine: loaders: default: filesystem: data_root: - # @TODO manage to use the configuration parameter - # - "%sylius_core.public_dir%/%monsieurbiz.richeditor.config.upload_directory%" - - "%sylius_core.public_dir%/media/rich-editor" + - "%sylius_core.public_dir%/%env(MONSIEURBIZ_SYLIUS_RICH_EDITOR_UPLOAD_DIR)%" diff --git a/src/Resources/config/richeditor.yaml b/src/Resources/config/richeditor.yaml index 812cb3ba..5262b866 100644 --- a/src/Resources/config/richeditor.yaml +++ b/src/Resources/config/richeditor.yaml @@ -1,6 +1,6 @@ monsieurbiz_sylius_richeditor: - upload_directory: '/media/rich-editor' - image_upload_directory: '/media/image' + upload_directory: "%env(resolve:MONSIEURBIZ_SYLIUS_RICH_EDITOR_UPLOAD_DIR)%" + image_upload_directory: "%env(resolve:MONSIEURBIZ_SYLIUS_RICH_EDITOR_IMAGE_UPLOAD_DIR)%" ui_elements: monsieurbiz.html: alias: html