Skip to content

Commit

Permalink
Merge pull request #32 from etienne-monsieurbiz/feature/add-attributes
Browse files Browse the repository at this point in the history
Add attributes on item links
  • Loading branch information
jacquesbh authored Oct 30, 2023
2 parents 1af9b98 + 2a4a2db commit b78f119
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/Entity/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class MenuItem implements MenuItemInterface

protected ?int $position = null;

protected bool $targetBlank = false;

protected bool $noreferrer = false;

protected bool $noopener = false;

protected bool $nofollow = false;

/**
* MenuItem constructor.
*/
Expand Down Expand Up @@ -158,6 +166,46 @@ public function removeItem(MenuItemInterface $item): void
}
}

public function isTargetBlank(): bool
{
return $this->targetBlank;
}

public function setTargetBlank(bool $targetBlank): void
{
$this->targetBlank = $targetBlank;
}

public function isNoreferrer(): bool
{
return $this->noreferrer;
}

public function setNoreferrer(bool $noreferrer): void
{
$this->noreferrer = $noreferrer;
}

public function isNoopener(): bool
{
return $this->noopener;
}

public function setNoopener(bool $noopener): void
{
$this->noopener = $noopener;
}

public function isNofollow(): bool
{
return $this->nofollow;
}

public function setNofollow(bool $nofollow): void
{
$this->nofollow = $nofollow;
}

/**
* @inheritdoc
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Entity/MenuItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ public function addItem(self $item): void;
*/
public function removeItem(self $item): void;

public function isTargetBlank(): bool;

public function setTargetBlank(bool $targetBlank): void;

public function isNoreferrer(): bool;

public function setNoreferrer(bool $noreferrer): void;

public function isNoopener(): bool;

public function setNoopener(bool $noopener): void;

public function isNofollow(): bool;

public function setNofollow(bool $nofollow): void;

public function getLabel(): ?string;

public function getUrl(): ?string;
Expand Down
5 changes: 5 additions & 0 deletions src/Fixture/Factory/MenuFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ private function createMenuItem(array $item, int $position, MenuInterface $menu,
$menuItem->setPosition($position);
$menuItem->setMenu($menu);

$menuItem->setTargetBlank($item['targetBlank'] ?? false);
$menuItem->setNoreferrer($item['noreferrer'] ?? false);
$menuItem->setNoopener($item['noopener'] ?? false);
$menuItem->setNofollow($item['nofollow'] ?? false);

if (null !== $parentItem) {
$menuItem->setParent($parentItem);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Fixture/MenuFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function configureResourceNode(ArrayNodeDefinition $resourceNode): void
->children()
->scalarNode('label')->cannotBeEmpty()->end()
->scalarNode('url')->defaultValue('')->end()
->booleanNode('targetBlank')->defaultFalse()->end()
->booleanNode('noreferrer')->defaultFalse()->end()
->booleanNode('noopener')->defaultFalse()->end()
->booleanNode('nofollow')->defaultFalse()->end()
->end()
->end()
->end()
Expand Down
21 changes: 21 additions & 0 deletions src/Form/Type/MenuItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand Down Expand Up @@ -55,6 +56,26 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
return $qb;
},
])
->add('targetBlank', CheckboxType::class, [
'label' => 'monsieurbiz_menu.ui.target_blank',
'help' => 'monsieurbiz_menu.ui.help_target_blank',
'required' => false,
])
->add('noreferrer', CheckboxType::class, [
'label' => 'monsieurbiz_menu.ui.noreferrer',
'help' => 'monsieurbiz_menu.ui.help_noreferrer',
'required' => false,
])
->add('noopener', CheckboxType::class, [
'label' => 'monsieurbiz_menu.ui.noopener',
'help' => 'monsieurbiz_menu.ui.help_noopener',
'required' => false,
])
->add('nofollow', CheckboxType::class, [
'label' => 'monsieurbiz_menu.ui.nofollow',
'help' => 'monsieurbiz_menu.ui.help_nofollow',
'required' => false,
])
->add('translations', ResourceTranslationsType::class, [
'label' => 'sylius.ui.translations',
'entry_type' => MenuItemTranslationType::class,
Expand Down
40 changes: 40 additions & 0 deletions src/Migrations/Version20231027081424.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of Monsieur Biz' Menu 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\SyliusMenuPlugin\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231027081424 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE monsieurbiz_menu_item ADD target_blank TINYINT(1) NOT NULL, ADD noreferrer TINYINT(1) NOT NULL, ADD noopener TINYINT(1) NOT NULL, ADD nofollow TINYINT(1) NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE monsieurbiz_menu_item DROP target_blank, DROP noreferrer, DROP noopener, DROP nofollow');
}
}
12 changes: 12 additions & 0 deletions src/Resources/config/doctrine/MenuItem.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,17 @@
<field name="position" column="position" type="integer" nullable="false">
<options name="default">null</options>
</field>
<field name="targetBlank" column="target_blank" type="boolean" nullable="false">
<options name="default">false</options>
</field>
<field name="noreferrer" column="noreferrer" type="boolean" nullable="false">
<options name="default">false</options>
</field>
<field name="noopener" column="noopener" type="boolean" nullable="false">
<options name="default">false</options>
</field>
<field name="nofollow" column="nofollow" type="boolean" nullable="false">
<options name="default">false</options>
</field>
</mapped-superclass>
</doctrine-mapping>
8 changes: 8 additions & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ monsieurbiz_menu:
menu_items: 'Menu elements'
label: 'Label'
url: 'URL'
target_blank: 'Target _blank'
help_target_blank: 'Open the link in a new tab'
noreferrer: 'noreferer'
help_noreferrer: 'Add "noreferrer" to the rel attribute'
noopener: 'noopener'
help_noopener: 'Add "noopener" to the rel attribute'
nofollow: 'nofollow'
help_nofollow: 'Add "nofollow" to the rel attribute'
8 changes: 8 additions & 0 deletions src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ monsieurbiz_menu:
menu_items: 'Éléments de menu'
label: 'Label'
url: 'URL'
target_blank: 'Target _blank'
help_target_blank: 'Ouvrir le lien dans un nouvel onglet'
noreferrer: 'noreferer'
help_noreferrer: 'Ajoute "noreferrer" à l''attribut rel'
noopener: 'noopener'
help_noopener: 'Ajoute "noopener" à l''attribut rel'
nofollow: 'nofollow'
help_nofollow: 'Ajoute "nofollow" à l''attribut rel'
24 changes: 24 additions & 0 deletions src/Resources/views/Admin/MenuItem/_form.html.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
{% from '@SyliusAdmin/Macro/translationForm.html.twig' import translationForm %}
{% form_theme form '@SyliusAdmin/Form/theme.html.twig' %}

{{ form_errors(form) }}

<div class="ui segment">
<div class="field">
{{ form_row(form.parent) }}

<div class="ui segment">
<div>
{{ form_row(form.targetBlank) }}
{{ form_help(form.targetBlank) }}
</div>
<div class="ui hidden divider"></div>
<div>
{{ form_row(form.noreferrer) }}
{{ form_help(form.noreferrer) }}
</div>
<div class="ui hidden divider"></div>
<div>
{{ form_row(form.noopener) }}
{{ form_help(form.noopener) }}
</div>
<div class="ui hidden divider"></div>
<div>
{{ form_row(form.nofollow) }}
{{ form_help(form.nofollow) }}
</div>
</div>

{{ translationForm(form.translations) }}
</div>
</div>
12 changes: 11 additions & 1 deletion src/Resources/views/Layout/Header/_menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@
</div>
</div>
{% else %}
<a href="{% if menu.url is empty %}#{% else %}{{ menu.url }}{% endif %}" class="item">{{ menu.label }}</a>
{% set noreferrer = menu.noreferrer %}
{% set noopener = menu.noopener %}
{% set nofollow = menu.nofollow %}
{% set hasRel = noreferrer or noopener or nofollow %}
<a href="{% if menu.url is empty %}#{% else %}{{ menu.url }}{% endif %}" class="item" {% if menu.targetBlank %}target="_blank"{% endif %}
{% if hasRel %}
rel="{% if noreferrer %}noreferrer{% endif %}{% if noopener %} noopener{% endif %}{% if nofollow %} nofollow{% endif %}"
{% endif %}
>
{{ menu.label }}
</a>
{% endif %}
{% endmacro %}

Expand Down

0 comments on commit b78f119

Please sign in to comment.