From ccb04d9b2525edf0a6917d988702e348dc77c088 Mon Sep 17 00:00:00 2001 From: David Buros Date: Tue, 27 Aug 2024 17:48:23 +0200 Subject: [PATCH] feat(ui-element): Add Badges Ui element and remove header --- src/Form/Type/BadgeType.php | 51 ++++++++++++++++ src/Form/Type/LinkType.php | 46 ++++++++++----- .../Type/UiElement/BadgesUiElementType.php | 58 +++++++++++++++++++ .../Type/UiElement/FiguresUiElementType.php | 1 + .../Type/UiElement/HeaderUiElementType.php | 48 --------------- .../Type/UiElement/LinksUiElementType.php | 1 + .../Type/UiElement/LogosUiElementType.php | 1 + src/Resources/config/images.yaml | 4 ++ src/Resources/translations/messages.en.yaml | 8 ++- src/Resources/translations/messages.fr.yaml | 8 ++- .../UiElement/badges_ui_element.html.twig | 49 ++++++++++++++++ .../UiElement/header_ui_element.html.twig | 13 ----- .../UiElement/badges_ui_element.html.twig | 49 ++++++++++++++++ .../UiElement/header_ui_element.html.twig | 13 ----- .../{header.svg.twig => badges.svg.twig} | 0 15 files changed, 256 insertions(+), 94 deletions(-) create mode 100644 src/Form/Type/BadgeType.php create mode 100644 src/Form/Type/UiElement/BadgesUiElementType.php delete mode 100644 src/Form/Type/UiElement/HeaderUiElementType.php create mode 100644 src/Resources/views/Admin/UiElement/badges_ui_element.html.twig delete mode 100644 src/Resources/views/Admin/UiElement/header_ui_element.html.twig create mode 100644 src/Resources/views/Shop/UiElement/badges_ui_element.html.twig delete mode 100644 src/Resources/views/Shop/UiElement/header_ui_element.html.twig rename src/Resources/views/Wireframe/{header.svg.twig => badges.svg.twig} (100%) diff --git a/src/Form/Type/BadgeType.php b/src/Form/Type/BadgeType.php new file mode 100644 index 0000000..6584848 --- /dev/null +++ b/src/Form/Type/BadgeType.php @@ -0,0 +1,51 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusUiElementsPlugin\Form\Type; + +use MonsieurBiz\SyliusMediaManagerPlugin\Form\Type\ImageType as MediaManagerImageType; +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Validator\Constraints as Assert; + +final class BadgeType extends AbstractType +{ + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder + ->add('title', MinimalWysiwygType::class, [ + 'label' => 'monsieurbiz_ui_elements.common.fields.title', + 'required' => true, + 'constraints' => [ + new Assert\NotBlank(), + ], + ]) + ->add('image', MediaManagerImageType::class, [ + 'label' => 'monsieurbiz_ui_elements.common.fields.image', + 'required' => true, + 'constraints' => [ + new Assert\NotBlank(), + ], + ]) + ->add('link', LinkType::class, [ + 'label' => 'monsieurbiz_ui_elements.common.fields.link', + 'required' => false, + 'with_label' => false, + 'attr' => [ + 'class' => 'ui segment', + ], + ]) + ; + } +} diff --git a/src/Form/Type/LinkType.php b/src/Form/Type/LinkType.php index 25a7966..5d96d79 100644 --- a/src/Form/Type/LinkType.php +++ b/src/Form/Type/LinkType.php @@ -16,6 +16,7 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\UrlType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints as Assert; class LinkType extends AbstractType @@ -25,30 +26,35 @@ class LinkType extends AbstractType public const TYPE_EXTERNAL = 'external'; /** - * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function buildForm(FormBuilderInterface $builder, array $options): void { + $linkConstraints = [ + new Assert\AtLeastOneOf([ + 'includeInternalMessages' => false, + 'message' => 'monsieurbiz_ui_elements.errors.not_valid_url', + 'constraints' => [ + new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]), + new Assert\Regex(['pattern' => '`^(#|/.*)$`']), + ], + ]), + ]; + + if (true === $options['required']) { + $linkConstraints[] = new Assert\NotBlank(); + } + $builder ->add('link', UrlType::class, [ 'label' => 'monsieurbiz_ui_elements.common.fields.link', 'required' => true, - 'constraints' => [ - new Assert\AtLeastOneOf([ - 'includeInternalMessages' => false, - 'message' => 'monsieurbiz_ui_elements.errors.not_valid_url', - 'constraints' => [ - new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]), - new Assert\Regex(['pattern' => '`^(#|/.*)$`']), - ], - ]), - new Assert\NotBlank(), - ], + 'constraints' => $linkConstraints, ]) ->add('label', TextType::class, [ 'label' => 'monsieurbiz_ui_elements.common.fields.label', 'required' => true, - 'constraints' => [new Assert\NotBlank()], + 'constraints' => $options['required'] ? [new Assert\NotBlank()] : [], ]) ->add('type', ChoiceType::class, [ 'label' => 'monsieurbiz_ui_elements.common.fields.link_type', @@ -60,8 +66,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'row_attr' => [ 'class' => 'ui segment', ], - 'constraints' => [new Assert\NotBlank()], + 'constraints' => $options['required'] ? [new Assert\NotBlank()] : [], ]) ; + + if (false === $options['with_label']) { + $builder->remove('label'); + } + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'with_label' => true, + ]); + $resolver->setAllowedTypes('with_label', ['bool']); } } diff --git a/src/Form/Type/UiElement/BadgesUiElementType.php b/src/Form/Type/UiElement/BadgesUiElementType.php new file mode 100644 index 0000000..3b4604b --- /dev/null +++ b/src/Form/Type/UiElement/BadgesUiElementType.php @@ -0,0 +1,58 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusUiElementsPlugin\Form\Type\UiElement; + +use MonsieurBiz\SyliusRichEditorPlugin\Attribute\AsUiElement; +use MonsieurBiz\SyliusRichEditorPlugin\Attribute\TemplatesUiElement; +use MonsieurBiz\SyliusUiElementsPlugin\Form\Type\BadgeType; +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\CollectionType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Validator\Constraints as Assert; + +#[AsUiElement( + code: 'monsieurbiz_ui_elements.badges_ui_element', + icon: 'th', + title: 'monsieurbiz_ui_elements.ui_element.badges_ui_element.title', + description: 'monsieurbiz_ui_elements.ui_element.badges_ui_element.description', + templates: new TemplatesUiElement( + adminRender: '@MonsieurBizSyliusUiElementsPlugin/Admin/UiElement/badges_ui_element.html.twig', + frontRender: '@MonsieurBizSyliusUiElementsPlugin/Shop/UiElement/badges_ui_element.html.twig', + ), + wireframe: 'badges', + tags: [], +)] +class BadgesUiElementType extends AbstractType +{ + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder + ->add('badges', CollectionType::class, [ + 'label' => 'monsieurbiz_ui_elements.ui_element.badges_ui_element.fields.badges', + 'entry_type' => BadgeType::class, + 'prototype_name' => '__badge__', + 'allow_add' => true, + 'allow_delete' => true, + 'constraints' => [ + new Assert\Valid(), + new Assert\Count(['min' => 1]), + ], + 'attr' => [ + 'class' => 'ui segment secondary collection--flex collection--flex-wide', + ], + ]) + ; + } +} diff --git a/src/Form/Type/UiElement/FiguresUiElementType.php b/src/Form/Type/UiElement/FiguresUiElementType.php index 4b2ac33..93a8b52 100644 --- a/src/Form/Type/UiElement/FiguresUiElementType.php +++ b/src/Form/Type/UiElement/FiguresUiElementType.php @@ -43,6 +43,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'button_add_label' => 'monsieurbiz_ui_elements.ui_element.figures_ui_element.buttons.add_element', 'button_delete_label' => 'monsieurbiz_ui_elements.ui_element.figures_ui_element.buttons.delete_element', 'entry_type' => FigureType::class, + 'prototype_name' => '__figure__', 'allow_add' => true, 'allow_delete' => true, 'constraints' => [new Assert\Valid()], diff --git a/src/Form/Type/UiElement/HeaderUiElementType.php b/src/Form/Type/UiElement/HeaderUiElementType.php deleted file mode 100644 index 91dcf52..0000000 --- a/src/Form/Type/UiElement/HeaderUiElementType.php +++ /dev/null @@ -1,48 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace MonsieurBiz\SyliusUiElementsPlugin\Form\Type\UiElement; - -use MonsieurBiz\SyliusRichEditorPlugin\Attribute\AsUiElement; -use MonsieurBiz\SyliusRichEditorPlugin\Attribute\TemplatesUiElement; -use MonsieurBiz\SyliusUiElementsPlugin\Form\Type\MinimalWysiwygType; -use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; - -#[AsUiElement( - code: 'monsieurbiz_ui_elements.header_ui_element', - icon: 'heading', - title: 'monsieurbiz_ui_elements.ui_element.header_ui_element.title', - description: 'monsieurbiz_ui_elements.ui_element.header_ui_element.description', - templates: new TemplatesUiElement( - adminRender: '@MonsieurBizSyliusUiElementsPlugin/Admin/UiElement/header_ui_element.html.twig', - frontRender: '@MonsieurBizSyliusUiElementsPlugin/Shop/UiElement/header_ui_element.html.twig', - ), - wireframe: 'header', - tags: [], -)] -class HeaderUiElementType extends AbstractType -{ - /** - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function buildForm(FormBuilderInterface $builder, array $options): void - { - $builder->add('title', MinimalWysiwygType::class, [ - 'label' => 'monsieurbiz_ui_elements.common.fields.title', - 'required' => false, - ]); - $builder->add('description', MinimalWysiwygType::class, [ - 'label' => 'monsieurbiz_ui_elements.common.fields.subtitle', - 'required' => false, - ]); - } -} diff --git a/src/Form/Type/UiElement/LinksUiElementType.php b/src/Form/Type/UiElement/LinksUiElementType.php index cdb317b..e50cf63 100644 --- a/src/Form/Type/UiElement/LinksUiElementType.php +++ b/src/Form/Type/UiElement/LinksUiElementType.php @@ -78,6 +78,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ]) ->add('links', CollectionType::class, [ 'label' => 'monsieurbiz_ui_elements.common.fields.links', + 'prototype_name' => '__link__', 'entry_type' => LinkType::class, 'allow_add' => true, 'allow_delete' => true, diff --git a/src/Form/Type/UiElement/LogosUiElementType.php b/src/Form/Type/UiElement/LogosUiElementType.php index ac71ae8..5ed595b 100644 --- a/src/Form/Type/UiElement/LogosUiElementType.php +++ b/src/Form/Type/UiElement/LogosUiElementType.php @@ -47,6 +47,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ->add('logos', CollectionType::class, [ 'label' => 'monsieurbiz_ui_elements.common.fields.logos', 'entry_type' => ImageType::class, + 'prototype_name' => '__logo__', 'allow_add' => true, 'allow_delete' => true, 'constraints' => [new Assert\Valid()], diff --git a/src/Resources/config/images.yaml b/src/Resources/config/images.yaml index 3d8a614..5c83627 100644 --- a/src/Resources/config/images.yaml +++ b/src/Resources/config/images.yaml @@ -8,3 +8,7 @@ liip_imagine: filters: thumbnail: { size: [800, 800], mode: inset } relative_resize: { widen: 600 } + monsieurbiz_sylius_ui_elements_badges: + filters: + thumbnail: { size: [600, 600], mode: inset } + relative_resize: { widen: 300 } diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 0c6ad77..7f1e919 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -64,6 +64,8 @@ monsieurbiz_ui_elements: buttons: add_element: "Add logo" delete_element: "Delete logo" - header_ui_element: - title: "Header Element" - description: "Header with title and subtitle" + badges_ui_element: + title: "Badges Element" + description: "Collection of badges with image, title and link" + fields: + badges: "Badges" diff --git a/src/Resources/translations/messages.fr.yaml b/src/Resources/translations/messages.fr.yaml index 9a99140..addac0e 100644 --- a/src/Resources/translations/messages.fr.yaml +++ b/src/Resources/translations/messages.fr.yaml @@ -64,6 +64,8 @@ monsieurbiz_ui_elements: buttons: add_element: "Ajouter un logo" delete_element: "Supprimer un logo" - header_ui_element: - title: "Lame En-tĂȘte" - description: "En-tĂȘte de page avec titre et description" + badges_ui_element: + title: "Lame Badges" + description: "Ensemble de badges avec image, titre et lien" + fields: + badges: "Badges" diff --git a/src/Resources/views/Admin/UiElement/badges_ui_element.html.twig b/src/Resources/views/Admin/UiElement/badges_ui_element.html.twig new file mode 100644 index 0000000..9887cca --- /dev/null +++ b/src/Resources/views/Admin/UiElement/badges_ui_element.html.twig @@ -0,0 +1,49 @@ +{# +UI Element template +type: figures_ui_element +element fields: + - badges + - title + - image + - link + - link + - type +#} + + {% if element.badges|default([])|length > 0 %} + {% set columns = 'four' %} + {% if element.badges|length == 3 %} + {% set columns = 'three' %} + {% endif %} + {% if element.badges|length == 2 %} + {% set columns = 'two' %} + {% endif %} + {% if element.badges|length == 1 %} + {% set columns = 'one' %} + {% endif %} +
+ {% for badge in element.badges %} + {% set isExternalLink = (badge.link.type|default(null)) == constant('MonsieurBiz\\SyliusUiElementsPlugin\\Form\\Type\\LinkType::TYPE_EXTERNAL') %} + + {% endfor %} +
+{% endif %} diff --git a/src/Resources/views/Admin/UiElement/header_ui_element.html.twig b/src/Resources/views/Admin/UiElement/header_ui_element.html.twig deleted file mode 100644 index a69ba38..0000000 --- a/src/Resources/views/Admin/UiElement/header_ui_element.html.twig +++ /dev/null @@ -1,13 +0,0 @@ -{# -UI Element template -type: header_ui_element -element fields: - - title - - description -#} -{% if element.title|default('') is not empty %} -

{{ element.title|raw }}

-{% endif %} -{% if element.description|default('') is not empty %} - {{ element.description|raw }} -{% endif %} diff --git a/src/Resources/views/Shop/UiElement/badges_ui_element.html.twig b/src/Resources/views/Shop/UiElement/badges_ui_element.html.twig new file mode 100644 index 0000000..9887cca --- /dev/null +++ b/src/Resources/views/Shop/UiElement/badges_ui_element.html.twig @@ -0,0 +1,49 @@ +{# +UI Element template +type: figures_ui_element +element fields: + - badges + - title + - image + - link + - link + - type +#} + + {% if element.badges|default([])|length > 0 %} + {% set columns = 'four' %} + {% if element.badges|length == 3 %} + {% set columns = 'three' %} + {% endif %} + {% if element.badges|length == 2 %} + {% set columns = 'two' %} + {% endif %} + {% if element.badges|length == 1 %} + {% set columns = 'one' %} + {% endif %} +
+ {% for badge in element.badges %} + {% set isExternalLink = (badge.link.type|default(null)) == constant('MonsieurBiz\\SyliusUiElementsPlugin\\Form\\Type\\LinkType::TYPE_EXTERNAL') %} + + {% endfor %} +
+{% endif %} diff --git a/src/Resources/views/Shop/UiElement/header_ui_element.html.twig b/src/Resources/views/Shop/UiElement/header_ui_element.html.twig deleted file mode 100644 index a69ba38..0000000 --- a/src/Resources/views/Shop/UiElement/header_ui_element.html.twig +++ /dev/null @@ -1,13 +0,0 @@ -{# -UI Element template -type: header_ui_element -element fields: - - title - - description -#} -{% if element.title|default('') is not empty %} -

{{ element.title|raw }}

-{% endif %} -{% if element.description|default('') is not empty %} - {{ element.description|raw }} -{% endif %} diff --git a/src/Resources/views/Wireframe/header.svg.twig b/src/Resources/views/Wireframe/badges.svg.twig similarity index 100% rename from src/Resources/views/Wireframe/header.svg.twig rename to src/Resources/views/Wireframe/badges.svg.twig