Skip to content

Commit

Permalink
Merge pull request #68 from maximehuran/feature/cms-menu-provider
Browse files Browse the repository at this point in the history
Add compatibility with Menu URL provider
  • Loading branch information
maximehuran authored Jun 20, 2024
2 parents 9c0e9b9 + f8eb58e commit 2802ac4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
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 <[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\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

0 comments on commit 2802ac4

Please sign in to comment.