Skip to content

Commit 626dec5

Browse files
authored
Merge pull request #53 from lanfisis/2.x
Add enabled / disabled capability for menu items for Sylius 2
2 parents bfcf290 + 9b33d3d commit 626dec5

File tree

21 files changed

+138
-23
lines changed

21 files changed

+138
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
/.phpunit.result.cache
1717
/node_modules
1818
/yarn.lock
19+
/.php-cs-fixer.cache

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CONSOLE=${SYMFONY} console
88
export COMPOSE_PROJECT_NAME=menu
99
PLUGIN_NAME=sylius-${COMPOSE_PROJECT_NAME}-plugin
1010
COMPOSE=docker compose
11-
YARN=$$(command -v n >/dev/null 2>&1 && echo "n --download exec auto yarn" || echo "yarn")
11+
YARN=yarn
1212

1313
###
1414
### DEVELOPMENT

src/DataProvider/Tree/MenuItemTreeProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function getArrayResult(MenuInterface $menu): array
4848
'mni.parent_id as parent_id',
4949
'mm.id as menu_id',
5050
'COALESCE(current_translation.label, fallback_translation.label) as name',
51+
'mni.enabled as enabled',
5152
])
5253
->from('monsieurbiz_menu_item', 'mni')
5354
->join(

src/Entity/MenuItem.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class MenuItem implements MenuItemInterface
4949

5050
protected bool $nofollow = false;
5151

52+
protected bool $enabled = true;
53+
5254
/**
5355
* MenuItem constructor.
5456
*/
@@ -228,4 +230,20 @@ protected function createTranslation(): MenuItemTranslationInterface
228230
{
229231
return new MenuItemTranslation();
230232
}
233+
234+
/**
235+
* @inheritdoc
236+
*/
237+
public function isEnabled(): bool
238+
{
239+
return $this->enabled;
240+
}
241+
242+
/**
243+
* @inheritdoc
244+
*/
245+
public function setEnabled(bool $enabled): void
246+
{
247+
$this->enabled = $enabled;
248+
}
231249
}

src/Entity/MenuItemInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ public function setNofollow(bool $nofollow): void;
6666
public function getLabel(): ?string;
6767

6868
public function getUrl(): ?string;
69+
70+
public function isEnabled(): bool;
71+
72+
public function setEnabled(bool $enabled): void;
6973
}

src/Fixture/Factory/MenuFixtureFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ private function createMenuItem(array $item, int $position, MenuInterface $menu,
8383
$menuItem->setPosition($position);
8484
$menuItem->setMenu($menu);
8585

86+
$menuItem->setEnabled($item['enabled'] ?? true);
8687
$menuItem->setTargetBlank($item['targetBlank'] ?? false);
8788
$menuItem->setNoreferrer($item['noreferrer'] ?? false);
8889
$menuItem->setNoopener($item['noopener'] ?? false);

src/Fixture/MenuFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function configureResourceNode(ArrayNodeDefinition $resourceNode): void
4444
->end()
4545
->end()
4646
->end()
47+
->booleanNode('enabled')->defaultTrue()->end()
4748
->booleanNode('targetBlank')->defaultFalse()->end()
4849
->booleanNode('noreferrer')->defaultFalse()->end()
4950
->booleanNode('noopener')->defaultFalse()->end()

src/Form/Type/MenuItemType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3939
/** @var MenuItemInterface $item */
4040
Assert::isInstanceOf($item, MenuItemInterface::class);
4141
$event->getForm()
42+
->add('enabled', CheckboxType::class, [
43+
'label' => 'monsieurbiz_menu.ui.enabled',
44+
'required' => false,
45+
])
4246
->add('parent', EntityType::class, [
4347
'class' => $this->dataClass,
4448
'required' => false,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz' Menu plugin for Sylius.
5+
*
6+
* (c) Monsieur Biz <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace MonsieurBiz\SyliusMenuPlugin\Migrations;
15+
16+
use Doctrine\DBAL\Schema\Schema;
17+
use Doctrine\Migrations\AbstractMigration;
18+
19+
/**
20+
* Auto-generated Migration: Please modify to your needs!
21+
*/
22+
final class Version20250717143849 extends AbstractMigration
23+
{
24+
public function getDescription(): string
25+
{
26+
return '';
27+
}
28+
29+
public function up(Schema $schema): void
30+
{
31+
// this up() migration is auto-generated, please modify it to your needs
32+
$this->addSql('ALTER TABLE monsieurbiz_menu_item ADD enabled TINYINT(1) DEFAULT 1 NOT NULL, CHANGE target_blank target_blank TINYINT(1) DEFAULT 0 NOT NULL, CHANGE noreferrer noreferrer TINYINT(1) DEFAULT 0 NOT NULL, CHANGE noopener noopener TINYINT(1) DEFAULT 0 NOT NULL, CHANGE nofollow nofollow TINYINT(1) DEFAULT 0 NOT NULL');
33+
}
34+
35+
public function down(Schema $schema): void
36+
{
37+
// this down() migration is auto-generated, please modify it to your needs
38+
$this->addSql('ALTER TABLE monsieurbiz_menu_item DROP enabled, CHANGE target_blank target_blank TINYINT(1) NOT NULL, CHANGE noreferrer noreferrer TINYINT(1) NOT NULL, CHANGE noopener noopener TINYINT(1) NOT NULL, CHANGE nofollow nofollow TINYINT(1) NOT NULL');
39+
}
40+
}

src/Repository/MenuRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public function findOneByLocaleAndCode(string $localeCode, string $code): ?MenuI
2828
->innerJoin('o.items', 'item')
2929
->innerJoin('item.translations', 'item_translation', 'WITH', 'item_translation.locale = :locale')
3030
->where('o.code = :code')
31+
->andWhere('item.enabled = :enabled')
3132
->setParameter('locale', $localeCode)
3233
->setParameter('code', $code)
34+
->setParameter('enabled', true)
3335
;
3436

3537
/** @phpstan-ignore-next-line */

0 commit comments

Comments
 (0)