Skip to content

Commit

Permalink
Do not render link to terms when the content is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Feb 28, 2024
1 parent 55a165e commit e3375e7
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Form/Type/TermsTranslationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder
->add('name', TextType::class, [
'label' => 'setono_sylius_terms.form.terms.name',
'help' => 'setono_sylius_terms.form.terms.name_help',
])
->add('slug', TextType::class, [
'label' => 'setono_sylius_terms.form.terms.slug',
])
->add('label', TextType::class, [
'label' => 'setono_sylius_terms.form.terms.label',
'attr' => [
'placeholder' => 'setono_sylius_terms.form.terms.label_placeholder',
],
'help' => 'setono_sylius_terms.form.terms.label_help',
])
->add('content', TextareaType::class, [
'required' => false,
'label' => 'setono_sylius_terms.form.terms.content',
'help' => 'setono_sylius_terms.form.terms.content_help',
])
;
}
Expand Down
11 changes: 8 additions & 3 deletions src/Renderer/LabelRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ public function __construct(private readonly UrlGeneratorInterface $urlGenerator

public function render(TermsInterface $terms): string
{
$slug = $terms->getSlug();
Assert::notNull($slug);

$label = $terms->getLabel();
Assert::notNull($label);

// if the terms has no content, we don't need to create a link because the terms will be empty
if ($terms->getContent() === null || '' === $terms->getContent()) {
return $label;
}

$slug = $terms->getSlug();
Assert::notNull($slug);

$label = htmlspecialchars($label);

$link = $this->urlGenerator->generate('setono_sylius_terms_shop_show_terms', ['slug' => $slug]);
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/translations/messages.da.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ setono_sylius_terms:
terms:
translations: Oversættelser
name: Navn
name_help: Navnet bruges både som reference og som linktekst, når betingelseslinket vises
code: Kode
slug: Slug
label: Tekst til afkrydsningsfelt
label_placeholder: Brug [link:Linktekst] til at fortælle hvor linket skal være i teksten
label_help: Brug [link:Linktekst] til at fortælle hvor linket skal være i teksten
content: Indhold
content_help: Hvis indholdet er tomt vil teksten ved siden af afkrydsningsfeltet ikke have et link
channels: Kanaler
4 changes: 3 additions & 1 deletion src/Resources/translations/messages.de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ setono_sylius_terms:
terms:
translations: Übersetzungen
name: Name
name_help: Der Name wird sowohl als Referenz, als auch als Linktext verwendet, wenn der Konditionen-Link angezeigt wird
code: Code
slug: Slug
label: Etikett
label_placeholder: Verwenden Sie [link:Link text], um anzugeben wo im Text der Link eingefügt werden muss
label_help: Verwenden Sie [link:Link text], um anzugeben wo im Text der Link eingefügt werden muss
content: Inhalt
content_help: Wenn der Inhalt leer ist, wird das Etikett neben dem Kontrollkästchen keinen Link enthalten
channels: Kanäle
4 changes: 3 additions & 1 deletion src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ setono_sylius_terms:
terms:
translations: Translations
name: Name
name_help: The name is used both as a reference, but also as the link text when showing the terms link
code: Code
slug: Slug
label: Label
label_placeholder: Use [link:Link text] to tell where the link should be placed in the text
label_help: Use [link:Link text] to tell where the link should be placed in the text
content: Content
content_help: If the content is empty the label next to the checkbox will not have a link
channels: Channels
forms: Forms
term_form:
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ setono_sylius_terms:
terms:
translations: Traductions
name: Nom
name_help: Le nom est utilisé à la fois comme référence, mais aussi comme texte de lien lors de l'affichage du lien des conditions
code: Code
slug: Slug
label: Étiquette
label_placeholder: "Utiliser [link:Link text] pour indiquer l'endroit où le lien doit être placé dans le texte."
label_help: "Utiliser [link:Link text] pour indiquer l'endroit où le lien doit être placé dans le texte."
content: Contenu
content_help: Si le contenu est vide, l'étiquette à côté de la case à cocher n'aura pas de lien
channels: Canaux
4 changes: 3 additions & 1 deletion src/Resources/translations/messages.nl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ setono_sylius_terms:
terms:
translations: Vertalingen
name: Naam
name_help: De naam wordt zowel als referentie, maar ook als linktekst gebruikt bij het tonen van de voorwaarden link
code: Code
slug: Slug
label: Etiket
label_placeholder: Gebruik [link:Link text] om aan te geven waar in de tekst de link geplaatst dient te worden
label_help: Gebruik [link:Link text] om aan te geven waar in de tekst de link geplaatst dient te worden
content: Content
content_help: Als de content leeg is, zal het etiket naast het selectievakje geen link bevatten
channels: Kanalen
21 changes: 19 additions & 2 deletions tests/Renderer/LabelRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,52 @@ final class LabelRendererTest extends TestCase
*
* @dataProvider getTerms
*/
public function it_renders(string $label, string $slug, string $expected): void
public function it_renders(string $label, string $slug, ?string $content, string $expected): void
{
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('setono_sylius_terms_shop_show_terms', ['slug' => $slug])->willReturn('https://example.com/terms/' . $slug);

$terms = $this->prophesize(TermsInterface::class);
$terms->getLabel()->willReturn($label);
$terms->getSlug()->willReturn($slug);
$terms->getContent()->willReturn($content);

$renderer = new LabelRenderer($urlGenerator->reveal());

self::assertSame($expected, $renderer->render($terms->reveal()));
}

/**
* @return \Generator<array-key, array{label: string, slug: string, expected: string}>
* @return \Generator<array-key, array{label: string, slug: string, content: string|null, expected: string}>
*/
public function getTerms(): \Generator
{
yield [
'label' => 'Accept terms',
'slug' => 'slug',
'content' => 'content',
'expected' => '<a href="https://example.com/terms/slug" target="_blank">Accept terms</a>',
];

yield [
'label' => 'Accept [link:terms]',
'slug' => 'slug',
'content' => 'content',
'expected' => 'Accept <a href="https://example.com/terms/slug" target="_blank">terms</a>',
];

yield [
'label' => 'Accept terms',
'slug' => 'slug',
'content' => null,
'expected' => 'Accept terms',
];

yield [
'label' => 'Accept terms',
'slug' => 'slug',
'content' => '',
'expected' => 'Accept terms',
];
}
}

0 comments on commit e3375e7

Please sign in to comment.