-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from delyriand/feature/same-slug-multiple-pages
Allows 2 pages to have the same slug depending on channel and locale
- Loading branch information
Showing
11 changed files
with
161 additions
and
23 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
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
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
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,4 @@ | ||
monsieurbiz_cms_page: | ||
ui: | ||
slug: | ||
unique: 'This slug is already used for another page with channel %channel% and 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,4 @@ | ||
monsieurbiz_cms_page: | ||
ui: | ||
slug: | ||
unique: 'Ce slug est déjà utilisé pour une autre page avec le canal %channel% et la 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
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,31 @@ | ||
<?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\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class UniqueSlugByChannel extends Constraint | ||
{ | ||
public string $message = 'monsieurbiz_cms_page.ui.slug.unique'; | ||
|
||
public function validatedBy(): string | ||
{ | ||
return self::class . 'Validator'; | ||
} | ||
|
||
public function getTargets() | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Validator/Constraints/UniqueSlugByChannelValidator.php
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,63 @@ | ||
<?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\Validator\Constraints; | ||
|
||
use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface; | ||
use MonsieurBiz\SyliusCmsPagePlugin\Repository\PageRepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class UniqueSlugByChannelValidator extends ConstraintValidator | ||
{ | ||
private PageRepositoryInterface $pageRepository; | ||
|
||
public function __construct(PageRepositoryInterface $pageRepository) | ||
{ | ||
$this->pageRepository = $pageRepository; | ||
} | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
/** @var PageInterface $value */ | ||
Assert::isInstanceOf($value, PageInterface::class); | ||
/** @var UniqueSlugByChannel $constraint */ | ||
Assert::isInstanceOf($constraint, UniqueSlugByChannel::class); | ||
|
||
// Check if the slug is unique for each channel and locale | ||
foreach ($value->getTranslations() as $translation) { | ||
foreach ($value->getChannels() as $channel) { | ||
if ($this->pageRepository->existsOneByChannelAndSlug( | ||
$channel, | ||
$translation->getLocale(), | ||
$translation->getSlug(), | ||
$value->getId() ? [$value] : [] | ||
)) { | ||
$this->context->buildViolation($constraint->message, [ | ||
'%channel%' => $channel->getCode(), | ||
'%locale%' => $translation->getLocale(), | ||
]) | ||
->atPath(sprintf('translations[%s].slug', $translation->getLocale())) | ||
->addViolation() | ||
; | ||
} | ||
} | ||
} | ||
} | ||
} |