Skip to content

Commit

Permalink
Change links to UrlType with correct constraints and manage link type
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Aug 26, 2024
1 parent 3915391 commit 570c8fa
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 27 deletions.
37 changes: 37 additions & 0 deletions src/Form/Type/LinkTypeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of Monsieur Biz' Rich Editor plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusRichEditorPlugin\Form\Type;

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class LinkTypeType extends ChoiceType
{
public const TYPE_INTERNAL = 'internal';

public const TYPE_EXTERNAL = 'external';

public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);
$resolver->setDefaults([
'label' => 'monsieurbiz_richeditor_plugin.common.link_type',
'choices' => [
'monsieurbiz_richeditor_plugin.common.internal_link' => self::TYPE_INTERNAL,
'monsieurbiz_richeditor_plugin.common.external_link' => self::TYPE_EXTERNAL,
],
'multiple' => false,
]);
}
}
21 changes: 12 additions & 9 deletions src/Form/Type/UiElement/ButtonLinkType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
namespace MonsieurBiz\SyliusRichEditorPlugin\Form\Type\UiElement;

use MonsieurBiz\SyliusRichEditorPlugin\Form\Type\AlignmentType;
use MonsieurBiz\SyliusRichEditorPlugin\Form\Type\LinkTypeType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType as FormTextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;

Expand All @@ -32,26 +33,30 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$constraintsLabel = [
new Assert\NotBlank([]),
];
$constraintsLinkType = [
new Assert\NotBlank([]),
];
$constraintsLink = [
new Assert\AtLeastOneOf([
'includeInternalMessages' => false,
'message' => 'monsieurbiz_richeditor_plugin.not_valid_url',
'constraints' => [
new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]),
new Assert\Regex(['pattern' => '`^(#|/[^/])`']),
new Assert\Regex(['pattern' => '`^(#|/.*)$`']),
],
]),
new Assert\NotBlank([]),
];
} else {
$constraintsLabel = [];
$constraintsLinkType = [];
$constraintsLink = [
new Assert\AtLeastOneOf([
'includeInternalMessages' => false,
'message' => 'monsieurbiz_richeditor_plugin.not_valid_url',
'constraints' => [
new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]),
new Assert\Regex(['pattern' => '`^(#|/[^/])`']),
new Assert\Regex(['pattern' => '`^(#|/.*)$`']),
],
]),
];
Expand All @@ -63,16 +68,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'monsieurbiz_richeditor_plugin.ui_element.monsieurbiz.button.field.label',
'constraints' => $constraintsLabel,
])
->add('link', FormTextType::class, [
->add('link', UrlType::class, [
'required' => $required,
'label' => 'monsieurbiz_richeditor_plugin.ui_element.monsieurbiz.button.field.link',
'constraints' => $constraintsLink,
])
->add('target', ChoiceType::class, [
'choices' => [
'monsieurbiz_richeditor_plugin.ui_element.monsieurbiz.button.field.target_option.self' => '_self',
'monsieurbiz_richeditor_plugin.ui_element.monsieurbiz.button.field.target_option.blank' => '_blank',
],
->add('link_type', LinkTypeType::class, [
'required' => $required,
'constraints' => $constraintsLinkType,
])
->add('align', AlignmentType::class)
;
Expand Down
9 changes: 7 additions & 2 deletions src/Form/Type/UiElement/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use MonsieurBiz\SyliusMediaManagerPlugin\Form\Type\ImageType as MediaManagerImageType;
use MonsieurBiz\SyliusRichEditorPlugin\Form\Constraints\RichEditorConstraints;
use MonsieurBiz\SyliusRichEditorPlugin\Form\Type\AlignmentType;
use MonsieurBiz\SyliusRichEditorPlugin\Form\Type\LinkTypeType;
use MonsieurBiz\SyliusRichEditorPlugin\MonsieurBizSyliusRichEditorPlugin;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType as FormTextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand Down Expand Up @@ -56,7 +58,7 @@ public function addFields(FormBuilderInterface $builder, array $options): void
'required' => false,
'label' => 'monsieurbiz_richeditor_plugin.ui_element.monsieurbiz.image.field.title',
])
->add('link', FormTextType::class, [
->add('link', UrlType::class, [
'required' => false,
'label' => 'monsieurbiz_richeditor_plugin.ui_element.monsieurbiz.image.field.link',
'constraints' => [
Expand All @@ -65,11 +67,14 @@ public function addFields(FormBuilderInterface $builder, array $options): void
'message' => 'monsieurbiz_richeditor_plugin.not_valid_url',
'constraints' => [
new Assert\Url(['protocols' => ['http', 'https'], 'relativeProtocol' => true]),
new Assert\Regex(['pattern' => '`^(#|/[^/])`']),
new Assert\Regex(['pattern' => '`^(#|/.*)$`']),
],
]),
],
])
->add('link_type', LinkTypeType::class, [
'required' => false,
])
->add('align', AlignmentType::class)
;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ monsieurbiz_richeditor_plugin:
field:
label: 'Label'
link: 'Link'
target: 'Target'
target_option:
self: 'Open in the same frame as it was clicked (this is default)'
blank: 'Open in a new window or tab'
monsieurbiz.youtube:
title: 'Youtube Element'
short_description: 'A Youtube element.'
Expand Down Expand Up @@ -146,3 +142,7 @@ monsieurbiz_richeditor_plugin:
unallowed_uielement: 'Element not allowed here'
ui:
disabled: 'Disabled'
common:
link_type: 'Link type'
internal_link: 'Internal link'
external_link: 'External link'
6 changes: 5 additions & 1 deletion src/Resources/translations/messages.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ monsieurbiz_richeditor_plugin:
field:
label: 'Etiqueta'
link: 'Enlace'
target: 'Destino'
monsieurbiz.youtube:
title: 'Elemento de Youtube'
short_description: 'Un elemento de Youtube.'
Expand Down Expand Up @@ -140,3 +139,8 @@ monsieurbiz_richeditor_plugin:
unallowed_uielement: 'Elemento no permitido aquí'
ui:
disabled: 'Desactivado'
common:
link_type: 'Tipo de enlace'
internal_link: 'Enlace interno'
external_link: 'Enlace externo'

5 changes: 4 additions & 1 deletion src/Resources/translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ monsieurbiz_richeditor_plugin:
field:
label: 'Libellé'
link: 'Lien'
target: 'Cible'
monsieurbiz.youtube:
title: 'Lame Youtube'
short_description: 'Une vidéo youtube.'
Expand Down Expand Up @@ -143,3 +142,7 @@ monsieurbiz_richeditor_plugin:
unallowed_uielement: 'Élément non autorisé ici'
ui:
disabled: 'Désactivé'
common:
link_type: 'Type de lien'
internal_link: 'Lien interne'
external_link: 'Lien externe'
5 changes: 4 additions & 1 deletion src/Resources/translations/messages.hr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ monsieurbiz_richeditor_plugin:
field:
label: 'Natpis na gumbu'
link: 'URL linka'
target: 'Ciljni'
monsieurbiz.youtube:
title: 'YouTube video'
short_description: 'Element s YouTube videom.'
Expand Down Expand Up @@ -131,3 +130,7 @@ monsieurbiz_richeditor_plugin:
cannot_find_type: 'Vrsta dijela elemenata nije podržana.'
cannot_upload_image: 'Postoji tehnički problem. Molimo kontaktirajte svog administratora.'
ajax_error: 'Technikai probléma van. Kérjük, lépjen kapcsolatba rendszergazdájával.'
common:
link_type: 'Tip linka'
internal_link: 'Unutarnji link'
external_link: 'Vanjski link'
5 changes: 4 additions & 1 deletion src/Resources/translations/messages.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ monsieurbiz_richeditor_plugin:
field:
label: 'Label'
link: 'Link'
target: 'Doelkenmerk'
monsieurbiz.youtube:
title: 'YouTube-video'
short_description: 'Een YouTube-element.'
Expand Down Expand Up @@ -105,3 +104,7 @@ monsieurbiz_richeditor_plugin:
cannot_find_type: 'Kan type niet vinden voor sommige UI-elementen'
cannot_upload_image: 'Kan de afbeelding niet uploaden. Het bestand is mogelijk te groot of heeft een onjuist formaat.'
ajax_error: 'Er is een technisch probleem. Neem contact op met uw beheerder.'
common:
link_type: 'Type link'
internal_link: 'Interne link'
external_link: 'Externe link'
5 changes: 4 additions & 1 deletion src/Resources/translations/messages.pl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ monsieurbiz_richeditor_plugin:
field:
label: 'Etykieta'
link: 'Link'
target: 'Docelowy'
monsieurbiz.youtube:
title: 'Element YouTube'
short_description: 'Element YouTube.'
Expand Down Expand Up @@ -143,3 +142,7 @@ monsieurbiz_richeditor_plugin:
unallowed_uielement: 'Element niedozwolony tutaj'
ui:
disabled: 'Wyłączone'
common:
link_type: 'Typ linku'
internal_link: 'Link wewnętrzny'
external_link: 'Link zewnętrzny'
9 changes: 5 additions & 4 deletions src/Resources/views/Admin/UiElement/button.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
element fields :
label
link
target
link_type
align
#}
{% set align = element.align is defined and element.align is not empty ? element.align : 'center' %}
{% set target = element.target is defined and element.target is not empty ? element.target : '_self' %}
{% set rel = element.target is defined and element.target == '_blank' ? 'rel="noopener noreferrer"' : '' %}
{% set linkIsBlank = element.link_type is defined and element.link_type == constant('MonsieurBiz\\SyliusRichEditorPlugin\\Form\\Type\\LinkTypeType::TYPE_EXTERNAL') %}

<p style="text-align: {{align}};">
<a href="{{ element.link }}" target="{{ element.target }}" {{ rel }} class="ui primary button massive">{{ element.label }}</a>
<a href="{{ element.link }}"{% if linkIsBlank %} target="_blank" rel="noopener noreferrer"{% endif %} class="ui primary button massive">
{{ element.label }}
</a>
</p>
4 changes: 3 additions & 1 deletion src/Resources/views/Admin/UiElement/image.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
alt
title
link
link_type
align
#}
{% if element.image is defined %}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<p style="text-align: {{align}};">
{% if element.link is not empty %}
<a href="{{ element.link }}">
{% set linkIsBlank = element.link_type is defined and element.link_type == constant('MonsieurBiz\\SyliusRichEditorPlugin\\Form\\Type\\LinkTypeType::TYPE_EXTERNAL') %}
<a href="{{ element.link }}"{% if linkIsBlank %} target="_blank" rel="noopener noreferrer"{% endif %}>
<img class="ui fluid image" src="{{ monsieurbiz_richeditor_get_media_without_upload_dir(element.image)|imagine_filter('monsieurbiz_rich_editor_image') }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
</a>
{% else %}
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/views/Shop/UiElement/button.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
element fields :
label
link
link_type
align
#}
{% set align = element.align is defined and element.align is not empty ? element.align : 'center' %}
{% set linkIsBlank = element.link_type is defined and element.link_type == constant('MonsieurBiz\\SyliusRichEditorPlugin\\Form\\Type\\LinkTypeType::TYPE_EXTERNAL') %}
<p style="text-align: {{align}};">
<a href="{{ element.link }}" class="ui primary button massive">{{ element.label }}</a>
<a href="{{ element.link }}"{% if linkIsBlank %} target="_blank" rel="noopener noreferrer"{% endif %} class="ui primary button massive">{{ element.label }}</a>
</p>
4 changes: 3 additions & 1 deletion src/Resources/views/Shop/UiElement/image.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
alt
title
link
link_type
align
#}
{% if element.image is defined %}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<p style="text-align: {{align}};">
{% if element.link is not empty %}
<a href="{{ element.link }}">
{% set linkIsBlank = element.link_type is defined and element.link_type == constant('MonsieurBiz\\SyliusRichEditorPlugin\\Form\\Type\\LinkTypeType::TYPE_EXTERNAL') %}
<a href="{{ element.link }}"{% if linkIsBlank %} target="_blank" rel="noopener noreferrer"{% endif %}>
<img class="ui fluid image" src="{{ monsieurbiz_richeditor_get_media_without_upload_dir(element.image)|imagine_filter('monsieurbiz_rich_editor_image') }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
</a>
{% else %}
Expand Down

0 comments on commit 570c8fa

Please sign in to comment.