From 061eb8660abbbe225e0a2112640fb50d977c7431 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Wed, 19 Jun 2024 14:37:26 +0200 Subject: [PATCH 1/2] Add compatibility with Menu URL provider --- phpstan.neon | 3 + src/Menu/PageUrlProvider.php | 69 +++++++++++++++++++++ src/Resources/translations/messages.en.yaml | 3 + src/Resources/translations/messages.fr.yaml | 3 + 4 files changed, 78 insertions(+) create mode 100644 src/Menu/PageUrlProvider.php diff --git a/phpstan.neon b/phpstan.neon index 68853c9..726b9b7 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -16,3 +16,6 @@ parameters: # Test dependencies - 'tests/Application/**/*' + + # Menu Provider + - 'src/Menu/PageUrlProvider.php' diff --git a/src/Menu/PageUrlProvider.php b/src/Menu/PageUrlProvider.php new file mode 100644 index 0000000..1ee6ab8 --- /dev/null +++ b/src/Menu/PageUrlProvider.php @@ -0,0 +1,69 @@ + + * + * 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\SyliusCmsPagePlugin\Menu; + +use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface; +use MonsieurBiz\SyliusCmsPagePlugin\Repository\PageRepositoryInterface; +use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider; +use Symfony\Component\Routing\RouterInterface; +use Webmozart\Assert\Assert; + +class PageUrlProvider extends AbstractUrlProvider +{ + public const PROVIDER_CODE = 'page'; + + protected string $code = self::PROVIDER_CODE; + + protected string $icon = 'file alternate'; + + protected int $priority = 40; + + public function __construct( + RouterInterface $router, + private PageRepositoryInterface $pageRepository, + ) { + parent::__construct($router); + } + + protected function getResults(string $locale, string $search = ''): iterable + { + $queryBuilder = $this->pageRepository->createListQueryBuilder($locale) + ->andWhere('o.enabled = :enabled') + ->setParameter('enabled', true) + ; + + if (!empty($search)) { + $queryBuilder + ->andWhere('translation.title LIKE :search OR translation.slug LIKE :search') + ->setParameter('search', '%' . $search . '%') + ; + } + + $queryBuilder->setMaxResults($this->getMaxResults()); + + /** @phpstan-ignore-next-line */ + return $queryBuilder->getQuery()->getResult(); + } + + protected function addItemFromResult(object $result, string $locale): void + { + Assert::isInstanceOf($result, PageInterface::class); + /** @var PageInterface $result */ + $result->setCurrentLocale($locale); + $this->addItem( + (string) $result->getTitle(), + $this->router->generate('monsieurbiz_cms_page_show', ['slug' => $result->getSlug(), '_locale' => $locale]) + ); + } +} diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 3585b70..30f6ffb 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -23,6 +23,9 @@ monsieurbiz_cms_page: actions: create: 'Create a new page' preview: 'Preview' +monsieurbiz_menu: + provider: + page: 'Page' sylius_plus: rbac: parent: diff --git a/src/Resources/translations/messages.fr.yaml b/src/Resources/translations/messages.fr.yaml index ea7dc1b..16fffc8 100644 --- a/src/Resources/translations/messages.fr.yaml +++ b/src/Resources/translations/messages.fr.yaml @@ -23,6 +23,9 @@ monsieurbiz_cms_page: actions: create: 'Créer une nouvelle page' preview: 'Prévisualiser' +monsieurbiz_menu: + provider: + page: 'Page' sylius_plus: rbac: parent: From f8eb58e5a4628dc60db12aef68e72af6b8153d09 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Wed, 19 Jun 2024 14:37:50 +0200 Subject: [PATCH 2/2] Define fixtures hostname depending on Symfony server --- dist/.env.local | 1 + 1 file changed, 1 insertion(+) create mode 100644 dist/.env.local diff --git a/dist/.env.local b/dist/.env.local new file mode 100644 index 0000000..6c1a99e --- /dev/null +++ b/dist/.env.local @@ -0,0 +1 @@ +SYLIUS_FIXTURES_HOSTNAME=${SYMFONY_DEFAULT_ROUTE_HOST:-localhost}