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 App\Core\Slugify\Slugify;
|
||||||
use Symfony\Component\HttpKernel\KernelInterface;
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
use App\Core\Slugify\CodeSlugify;
|
use App\Core\Slugify\CodeSlugify;
|
||||||
|
use App\Core\Slugify\RouteParameterSlugify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class NodeEventSubscriber.
|
* class NodeEventSubscriber.
|
||||||
|
@ -25,19 +26,22 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber
|
||||||
protected KernelInterface $kernel;
|
protected KernelInterface $kernel;
|
||||||
protected Slugify $slugify;
|
protected Slugify $slugify;
|
||||||
protected CodeSlugify $codeSlugify;
|
protected CodeSlugify $codeSlugify;
|
||||||
|
protected RouteParameterSlugify $routeParameterSlugify;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
NodeFactory $nodeFactory,
|
NodeFactory $nodeFactory,
|
||||||
NodeRepository $nodeRepository,
|
NodeRepository $nodeRepository,
|
||||||
EntityManager $entityManager,
|
EntityManager $entityManager,
|
||||||
Slugify $slugify,
|
Slugify $slugify,
|
||||||
CodeSlugify $codeSlugify
|
CodeSlugify $codeSlugify,
|
||||||
|
RouteParameterSlugify $routeParameterSlugify
|
||||||
) {
|
) {
|
||||||
$this->nodeFactory = $nodeFactory;
|
$this->nodeFactory = $nodeFactory;
|
||||||
$this->nodeRepository = $nodeRepository;
|
$this->nodeRepository = $nodeRepository;
|
||||||
$this->entityManager = $entityManager;
|
$this->entityManager = $entityManager;
|
||||||
$this->slugify = $slugify;
|
$this->slugify = $slugify;
|
||||||
$this->codeSlugify = $codeSlugify;
|
$this->codeSlugify = $codeSlugify;
|
||||||
|
$this->routeParameterSlugify = $routeParameterSlugify;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function support(EntityInterface $entity)
|
public function support(EntityInterface $entity)
|
||||||
|
@ -75,16 +79,22 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber
|
||||||
}
|
}
|
||||||
|
|
||||||
$generatedUrl = rtrim($generatedUrl, '/');
|
$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']);
|
$routeParameter = sprintf('{%s}', $parameter['name']);
|
||||||
$regex = '/'.preg_quote($routeParameter).'/';
|
$regex = '/'.preg_quote($routeParameter).'/';
|
||||||
|
|
||||||
if (!preg_match($regex, $generatedUrl)) {
|
if (!preg_match($regex, $generatedUrl)) {
|
||||||
$generatedUrl.= '/'.$routeParameter;
|
$generatedUrl.= '/'.$routeParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$parameters[$key] = $parameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$node->setParameters($parameters);
|
||||||
|
|
||||||
$urlExists = $this->nodeRepository->urlExists($generatedUrl, $node);
|
$urlExists = $this->nodeRepository->urlExists($generatedUrl, $node);
|
||||||
|
|
||||||
if ($urlExists) {
|
if ($urlExists) {
|
||||||
|
|
|
@ -14,9 +14,8 @@ class CodeSlugify extends Slugify
|
||||||
public function slugify($data): ?string
|
public function slugify($data): ?string
|
||||||
{
|
{
|
||||||
$slug = parent::slugify($data);
|
$slug = parent::slugify($data);
|
||||||
$slug = preg_replace('/[^\w]+/', '', $slug);
|
|
||||||
|
|
||||||
return $slug;
|
return preg_replace('/[^\w]+/', '', $slug);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function create(): BaseSlugify
|
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