Skip to content

Commit fc425e8

Browse files
committed
Finalize menu url providers
1 parent 8be298d commit fc425e8

File tree

7 files changed

+183
-2
lines changed

7 files changed

+183
-2
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ parameters:
1212

1313
# Menu Provider
1414
- 'src/Menu/BlogUrlProvider.php'
15+
- 'src/Menu/BlogListUrlProvider.php'
1516
- 'src/Menu/CaseStudyUrlProvider.php'
17+
- 'src/Menu/CaseStudyListUrlProvider.php'
1618

1719
ignoreErrors:
1820
# Replace checkMissingIterableValueType parameter to avoid deprecated warning

src/Menu/BlogListUrlProvider.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz's Blog plugin for Sylius.
5+
* (c) Monsieur Biz <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MonsieurBiz\SyliusBlogPlugin\Menu;
13+
14+
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface;
15+
use MonsieurBiz\SyliusBlogPlugin\Entity\TagInterface;
16+
use MonsieurBiz\SyliusBlogPlugin\Repository\TagRepositoryInterface;
17+
use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider;
18+
use Symfony\Component\Routing\RouterInterface;
19+
use Symfony\Contracts\Translation\TranslatorInterface;
20+
use Webmozart\Assert\Assert;
21+
22+
class BlogListUrlProvider extends AbstractUrlProvider
23+
{
24+
public const PROVIDER_CODE = 'blog_list';
25+
26+
protected string $code = self::PROVIDER_CODE;
27+
28+
protected string $icon = 'newspaper';
29+
30+
protected int $priority = 35;
31+
32+
public function __construct(
33+
RouterInterface $router,
34+
private TagRepositoryInterface $tagRepository,
35+
private TranslatorInterface $translator
36+
) {
37+
parent::__construct($router);
38+
}
39+
40+
protected function getResults(string $locale, string $search = ''): iterable
41+
{
42+
$queryBuilder = $this->tagRepository->createEnabledListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE);
43+
44+
if (!empty($search)) {
45+
$queryBuilder
46+
->andWhere('translation.name LIKE :search OR translation.slug LIKE :search')
47+
->setParameter('search', '%' . $search . '%')
48+
;
49+
}
50+
51+
$queryBuilder->setMaxResults($this->getMaxResults());
52+
53+
/** @phpstan-ignore-next-line */
54+
return $queryBuilder->getQuery()->getResult();
55+
}
56+
57+
protected function addItemFromResult(object $result, string $locale): void
58+
{
59+
Assert::isInstanceOf($result, TagInterface::class);
60+
/** @var TagInterface $result */
61+
$result->setCurrentLocale($locale);
62+
$this->addItem(
63+
(string) $result->getName(),
64+
$this->router->generate('monsieurbiz_blog_tag_show', ['slug' => $result->getSlug(), '_locale' => $locale])
65+
);
66+
}
67+
68+
public function getItems(string $locale, string $search = ''): array
69+
{
70+
parent::getItems($locale, $search);
71+
72+
// Add item to link to all articles
73+
$firstItemLabel = $this->translator->trans('sylius.ui.all', [], null, $locale);
74+
if (empty($search) || false !== strpos($search, $firstItemLabel)) {
75+
$this->addItem($firstItemLabel, $this->router->generate('monsieurbiz_blog_index', ['_locale' => $locale]));
76+
// Add this last element to the beginning of the array
77+
$lastElement = array_pop($this->items);
78+
array_unshift($this->items, $lastElement);
79+
}
80+
81+
return $this->items;
82+
}
83+
}

src/Menu/BlogUrlProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class BlogUrlProvider extends AbstractUrlProvider
2525

2626
protected string $icon = 'newspaper';
2727

28-
protected int $priority = 30;
28+
protected int $priority = 25;
2929

3030
public function __construct(
3131
RouterInterface $router,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz's Blog plugin for Sylius.
5+
* (c) Monsieur Biz <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MonsieurBiz\SyliusBlogPlugin\Menu;
13+
14+
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface;
15+
use MonsieurBiz\SyliusBlogPlugin\Entity\TagInterface;
16+
use MonsieurBiz\SyliusBlogPlugin\Repository\TagRepositoryInterface;
17+
use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider;
18+
use Symfony\Component\Routing\RouterInterface;
19+
use Symfony\Contracts\Translation\TranslatorInterface;
20+
use Webmozart\Assert\Assert;
21+
22+
class CaseStudyListUrlProvider extends AbstractUrlProvider
23+
{
24+
public const PROVIDER_CODE = 'case_study_list';
25+
26+
protected string $code = self::PROVIDER_CODE;
27+
28+
protected string $icon = 'crosshairs';
29+
30+
protected int $priority = 30;
31+
32+
public function __construct(
33+
RouterInterface $router,
34+
private TagRepositoryInterface $tagRepository,
35+
private TranslatorInterface $translator
36+
) {
37+
parent::__construct($router);
38+
}
39+
40+
protected function getResults(string $locale, string $search = ''): iterable
41+
{
42+
$queryBuilder = $this->tagRepository->createEnabledListQueryBuilderByType($locale, ArticleInterface::CASE_STUDY_TYPE);
43+
44+
if (!empty($search)) {
45+
$queryBuilder
46+
->andWhere('translation.name LIKE :search OR translation.slug LIKE :search')
47+
->setParameter('search', '%' . $search . '%')
48+
;
49+
}
50+
51+
$queryBuilder->setMaxResults($this->getMaxResults());
52+
53+
/** @phpstan-ignore-next-line */
54+
return $queryBuilder->getQuery()->getResult();
55+
}
56+
57+
protected function addItemFromResult(object $result, string $locale): void
58+
{
59+
Assert::isInstanceOf($result, TagInterface::class);
60+
/** @var TagInterface $result */
61+
$result->setCurrentLocale($locale);
62+
$this->addItem(
63+
(string) $result->getName(),
64+
$this->router->generate('monsieurbiz_case_study_tag_show', ['slug' => $result->getSlug(), '_locale' => $locale])
65+
);
66+
}
67+
68+
public function getItems(string $locale, string $search = ''): array
69+
{
70+
parent::getItems($locale, $search);
71+
72+
// Add item to link to all articles
73+
$firstItemLabel = $this->translator->trans('sylius.ui.all', [], null, $locale);
74+
if (empty($search) || false !== strpos($search, $firstItemLabel)) {
75+
$this->addItem($firstItemLabel, $this->router->generate('monsieurbiz_case_study_index', ['_locale' => $locale]));
76+
// Add this last element to the beginning of the array
77+
$lastElement = array_pop($this->items);
78+
array_unshift($this->items, $lastElement);
79+
}
80+
81+
return $this->items;
82+
}
83+
}

src/Menu/CaseStudyUrlProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function addItemFromResult(object $result, string $locale): void
6262
$result->setCurrentLocale($locale);
6363
$this->addItem(
6464
(string) $result->getTitle(),
65-
$this->router->generate('monsieurbiz_case_studies_article_show', ['slug' => $result->getSlug(), '_locale' => $locale])
65+
$this->router->generate('monsieurbiz_case_study_article_show', ['slug' => $result->getSlug(), '_locale' => $locale])
6666
);
6767
}
6868
}

src/Resources/translations/messages.en.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ monsieurbiz_blog:
5656
case_studies: Case studies
5757
case_study: Case study
5858
position: Position
59+
60+
monsieurbiz_menu:
61+
provider:
62+
blog_list: Blog articles list
63+
case_study_list: Case studies list
64+
blog: Blog article
65+
case_study: Case study

src/Resources/translations/messages.fr.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ monsieurbiz_blog:
5656
case_studies: Études de cas
5757
case_study: Étude de cas
5858
position: Position
59+
monsieurbiz_menu:
60+
provider:
61+
blog_list: Liste d'articles de blog
62+
case_study_list: Liste d'études de cas
63+
blog: Article de blog
64+
case_study: Étude de cas

0 commit comments

Comments
 (0)