-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add blog and case study URL providers for menu
- Loading branch information
1 parent
867ec52
commit 8be298d
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz's Blog plugin for Sylius. | ||
* (c) Monsieur Biz <sylius@monsieurbiz.com> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MonsieurBiz\SyliusBlogPlugin\Menu; | ||
|
||
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface; | ||
use MonsieurBiz\SyliusBlogPlugin\Repository\ArticleRepositoryInterface; | ||
use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
class BlogUrlProvider extends AbstractUrlProvider | ||
{ | ||
public const PROVIDER_CODE = 'blog'; | ||
|
||
protected string $code = self::PROVIDER_CODE; | ||
|
||
protected string $icon = 'newspaper'; | ||
|
||
protected int $priority = 30; | ||
|
||
public function __construct( | ||
RouterInterface $router, | ||
private ArticleRepositoryInterface $articleRepository, | ||
) { | ||
parent::__construct($router); | ||
} | ||
|
||
protected function getResults(string $locale, string $search = ''): iterable | ||
{ | ||
$queryBuilder = $this->articleRepository->createListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE) | ||
->andWhere('ba.enabled = true') | ||
->andWhere('ba.state = :state') | ||
->setParameter('state', ArticleInterface::STATE_PUBLISHED) | ||
; | ||
|
||
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, ArticleInterface::class); | ||
/** @var ArticleInterface $result */ | ||
$result->setCurrentLocale($locale); | ||
$this->addItem( | ||
(string) $result->getTitle(), | ||
$this->router->generate('monsieurbiz_blog_article_show', ['slug' => $result->getSlug(), '_locale' => $locale]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz's Blog plugin for Sylius. | ||
* (c) Monsieur Biz <sylius@monsieurbiz.com> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MonsieurBiz\SyliusBlogPlugin\Menu; | ||
|
||
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface; | ||
use MonsieurBiz\SyliusBlogPlugin\Repository\ArticleRepositoryInterface; | ||
use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
class CaseStudyUrlProvider extends AbstractUrlProvider | ||
{ | ||
public const PROVIDER_CODE = 'case_study'; | ||
|
||
protected string $code = self::PROVIDER_CODE; | ||
|
||
protected string $icon = 'crosshairs'; | ||
|
||
protected int $priority = 20; | ||
|
||
public function __construct( | ||
RouterInterface $router, | ||
private ArticleRepositoryInterface $articleRepository, | ||
) { | ||
parent::__construct($router); | ||
} | ||
|
||
protected function getResults(string $locale, string $search = ''): iterable | ||
{ | ||
$queryBuilder = $this->articleRepository->createListQueryBuilderByType($locale, ArticleInterface::CASE_STUDY_TYPE) | ||
->andWhere('ba.enabled = true') | ||
->andWhere('ba.state = :state') | ||
->setParameter('state', ArticleInterface::STATE_PUBLISHED) | ||
; | ||
|
||
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, ArticleInterface::class); | ||
/** @var ArticleInterface $result */ | ||
$result->setCurrentLocale($locale); | ||
$this->addItem( | ||
(string) $result->getTitle(), | ||
$this->router->generate('monsieurbiz_case_studies_article_show', ['slug' => $result->getSlug(), '_locale' => $locale]) | ||
); | ||
} | ||
} |