Skip to content

Commit d7fac63

Browse files
committed
Introduce new "before making unique" callback to allow unique tree slugs, fixes #1429 and #1604.
1 parent 529fc5c commit d7fac63

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Gedmo\Sluggable\Handler;
4+
5+
use Gedmo\Sluggable\Mapping\Event\SluggableAdapter;
6+
7+
/**
8+
* This adds the ability to a SlugHandler to change the slug just before its
9+
* uniqueness is ensured. It is also called if the unique options is _not_
10+
* set.
11+
*
12+
* @author Gediminas Morkevicius <[email protected]>
13+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
14+
*/
15+
interface SlugHandlerWithUniqueCallbackInterface extends SlugHandlerInterface
16+
{
17+
/**
18+
* Callback for slug handlers before it is made unique
19+
*
20+
* @param SluggableAdapter $ea
21+
* @param array $config
22+
* @param object $object
23+
* @param string $slug
24+
*
25+
* @return void
26+
*/
27+
public function beforeMakingUnique(SluggableAdapter $ea, array &$config, $object, &$slug);
28+
}

lib/Gedmo/Sluggable/Handler/TreeSlugHandler.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @author Gediminas Morkevicius <[email protected]>
1818
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
1919
*/
20-
class TreeSlugHandler implements SlugHandlerInterface
20+
class TreeSlugHandler implements SlugHandlerWithUniqueCallbackInterface
2121
{
2222
const SEPARATOR = '/';
2323

@@ -128,10 +128,16 @@ public static function validate(array $options, ClassMetadata $meta)
128128
/**
129129
* {@inheritDoc}
130130
*/
131-
public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
131+
public function beforeMakingUnique(SluggableAdapter $ea, array &$config, $object, &$slug)
132132
{
133133
$slug = $this->transliterate($slug, $config['separator'], $object);
134+
}
134135

136+
/**
137+
* {@inheritDoc}
138+
*/
139+
public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
140+
{
135141
if (!$this->isInsert) {
136142
$wrapped = AbstractWrapper::wrap($object, $this->om);
137143
$meta = $wrapped->getMetadata();

lib/Gedmo/Sluggable/SluggableListener.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\Common\EventArgs;
66
use Gedmo\Mapping\MappedEventSubscriber;
7+
use Gedmo\Sluggable\Handler\SlugHandlerWithUniqueCallbackInterface;
78
use Gedmo\Sluggable\Mapping\Event\SluggableAdapter;
89
use Doctrine\Common\Persistence\ObjectManager;
910
use Gedmo\Tool\Wrapper\AbstractWrapper;
@@ -384,6 +385,16 @@ private function generateSlug(SluggableAdapter $ea, $object)
384385
$slug = null;
385386
}
386387

388+
// notify slug handlers --> beforeMakingUnique
389+
if ($hasHandlers) {
390+
foreach ($options['handlers'] as $class => $handlerOptions) {
391+
$handler = $this->getHandler($class);
392+
if ($handler instanceof SlugHandlerWithUniqueCallbackInterface) {
393+
$handler->beforeMakingUnique($ea, $options, $object, $slug);
394+
}
395+
}
396+
}
397+
387398
// make unique slug if requested
388399
if ($options['unique'] && null !== $slug) {
389400
$this->exponent = 0;

0 commit comments

Comments
 (0)