Skip to content

Add compatibility with Menu URL provider #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SYLIUS_FIXTURES_HOSTNAME=${SYMFONY_DEFAULT_ROUTE_HOST:-localhost}
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ parameters:

# Test dependencies
- 'tests/Application/**/*'

# Menu Provider
- 'src/Menu/PageUrlProvider.php'
69 changes: 69 additions & 0 deletions src/Menu/PageUrlProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of Monsieur Biz' Cms Page plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* 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])
);
}
}
3 changes: 3 additions & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ monsieurbiz_cms_page:
actions:
create: 'Create a new page'
preview: 'Preview'
monsieurbiz_menu:
provider:
page: 'Page'
sylius_plus:
rbac:
parent:
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down