update node parameter slugifier
This commit is contained in:
parent
d6dc699dfe
commit
b44a40eba3
|
@ -12,6 +12,7 @@ use App\Core\Repository\Site\NodeRepository;
|
|||
use App\Core\Slugify\Slugify;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use App\Core\Slugify\CodeSlugify;
|
||||
use App\Core\Slugify\RouteParameterSlugify;
|
||||
|
||||
/**
|
||||
* class NodeEventSubscriber.
|
||||
|
@ -25,19 +26,22 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber
|
|||
protected KernelInterface $kernel;
|
||||
protected Slugify $slugify;
|
||||
protected CodeSlugify $codeSlugify;
|
||||
protected RouteParameterSlugify $routeParameterSlugify;
|
||||
|
||||
public function __construct(
|
||||
NodeFactory $nodeFactory,
|
||||
NodeRepository $nodeRepository,
|
||||
EntityManager $entityManager,
|
||||
Slugify $slugify,
|
||||
CodeSlugify $codeSlugify
|
||||
CodeSlugify $codeSlugify,
|
||||
RouteParameterSlugify $routeParameterSlugify
|
||||
) {
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->nodeRepository = $nodeRepository;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->slugify = $slugify;
|
||||
$this->codeSlugify = $codeSlugify;
|
||||
$this->routeParameterSlugify = $routeParameterSlugify;
|
||||
}
|
||||
|
||||
public function support(EntityInterface $entity)
|
||||
|
@ -75,16 +79,22 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber
|
|||
}
|
||||
|
||||
$generatedUrl = rtrim($generatedUrl, '/');
|
||||
$parameters = $node->getParameters();
|
||||
|
||||
foreach ($node->getParameters() as $parameter) {
|
||||
foreach ($parameters as $key => $parameter) {
|
||||
$parameter['name'] = $this->routeParameterSlugify->slugify($parameter['name']);
|
||||
$routeParameter = sprintf('{%s}', $parameter['name']);
|
||||
$regex = '/'.preg_quote($routeParameter).'/';
|
||||
|
||||
if (!preg_match($regex, $generatedUrl)) {
|
||||
$generatedUrl.= '/'.$routeParameter;
|
||||
}
|
||||
|
||||
$parameters[$key] = $parameter;
|
||||
}
|
||||
|
||||
$node->setParameters($parameters);
|
||||
|
||||
$urlExists = $this->nodeRepository->urlExists($generatedUrl, $node);
|
||||
|
||||
if ($urlExists) {
|
||||
|
|
|
@ -11,12 +11,11 @@ use Cocur\Slugify\Slugify as BaseSlugify;
|
|||
*/
|
||||
class CodeSlugify extends Slugify
|
||||
{
|
||||
public function slugify($data): ? string
|
||||
public function slugify($data): ?string
|
||||
{
|
||||
$slug = parent::slugify($data);
|
||||
$slug = preg_replace('/[^\w]+/', '', $slug);
|
||||
|
||||
return $slug;
|
||||
return preg_replace('/[^\w]+/', '', $slug);
|
||||
}
|
||||
|
||||
protected function create(): BaseSlugify
|
||||
|
|
32
core/Slugify/RouteParameterSlugify.php
Normal file
32
core/Slugify/RouteParameterSlugify.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\Slugify;
|
||||
|
||||
use Cocur\Slugify\Slugify as BaseSlugify;
|
||||
|
||||
/**
|
||||
* class CodeSlugify.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class RouteParameterSlugify extends Slugify
|
||||
{
|
||||
public function slugify($data): ?string
|
||||
{
|
||||
$slug = parent::slugify($data);
|
||||
|
||||
return preg_replace('/[^\w]+/', '', $slug);
|
||||
}
|
||||
|
||||
protected function create(): BaseSlugify
|
||||
{
|
||||
$slugify = new BaseSlugify([
|
||||
'separator' => '_',
|
||||
'lowercase' => false,
|
||||
]);
|
||||
|
||||
$slugify->activateRuleSet('french');
|
||||
|
||||
return $slugify;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue