@@ -43,6 +43,8 @@ class SlugBehavior extends Behavior
4343 * `['Model.beforeSave' => 'beforeSave']`.
4444 * - onUpdate: Boolean indicating whether slug should be updated when
4545 * updating record, defaults to `false`.
46+ * - onDirty: Boolean indicating whether slug should be updated when
47+ * slug field is dirty, defaults to `false`.
4648 *
4749 * @var array
4850 */
@@ -72,13 +74,14 @@ class SlugBehavior extends Behavior
7274 'implementedMethods ' => [
7375 'slug ' => 'slug ' ,
7476 ],
75- 'onUpdate ' => false
77+ 'onUpdate ' => false ,
78+ 'onDirty ' => false
7679 ];
7780
7881 /**
79- * Slugger instance
82+ * Slugger instance or callable
8083 *
81- * @var \Muffin\Slug\SluggerInterface
84+ * @var \Muffin\Slug\SluggerInterface|callable
8285 */
8386 protected $ _slugger ;
8487
@@ -121,16 +124,16 @@ public function initialize(array $config)
121124 /**
122125 * Get/set slugger instance.
123126 *
124- * @param callable $slugger Sets slugger instance if passed.
127+ * @param \Muffin\Slug\SluggerInterface| callable $slugger Sets slugger instance if passed.
125128 * If no argument is passed return slugger intance based on behavior config.
126- * @return callable|void
129+ * @return callable|\Muffin\Slug\SluggerInterface|null
127130 */
128131 public function slugger ($ slugger = null )
129132 {
130133 if ($ slugger !== null ) {
131134 $ this ->_slugger = $ slugger ;
132135
133- return ;
136+ return null ;
134137 }
135138
136139 if ($ this ->_slugger !== null ) {
@@ -190,22 +193,31 @@ public function buildValidator(Event $event, Validator $validator, $name)
190193 */
191194 public function beforeSave (Event $ event , Entity $ entity , ArrayObject $ options )
192195 {
193- $ config = $ this ->_config ;
194-
195- if (!$ entity ->isNew () && !$ config ['onUpdate ' ]) {
196+ $ onUpdate = $ this ->config ('onUpdate ' );
197+ if (!$ entity ->isNew () && !$ onUpdate ) {
196198 return ;
197199 }
198200
199- if ($ entity ->dirty ($ config ['field ' ]) &&
200- (!$ entity ->isNew () || (!empty ($ entity ->{$ config ['field ' ]})))
201+ $ onDirty = $ this ->config ('onDirty ' );
202+ $ field = $ this ->config ('field ' );
203+ if (!$ onDirty
204+ && $ entity ->dirty ($ field )
205+ && (!$ entity ->isNew () || (!empty ($ entity ->{$ field })))
201206 ) {
202207 return ;
203208 }
204209
205- $ fields = (array )$ config ['displayField ' ];
210+ $ separator = $ this ->config ('separator ' );
211+ if ($ entity ->dirty ($ field ) && !empty ($ entity ->{$ field })) {
212+ $ slug = $ this ->slug ($ entity , $ entity ->{$ field }, $ separator );
213+ $ entity ->set ($ field , $ slug );
214+
215+ return ;
216+ }
217+
206218 $ parts = [];
207- foreach ($ fields as $ field ) {
208- $ value = Hash::get ($ entity , $ field );
219+ foreach (( array ) $ this -> config ( ' displayField ' ) as $ displayField ) {
220+ $ value = Hash::get ($ entity , $ displayField );
209221
210222 if ($ value === null && !$ entity ->isNew ()) {
211223 return ;
@@ -220,8 +232,8 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
220232 return ;
221233 }
222234
223- $ slug = $ this ->slug ($ entity , implode ($ config [ ' separator ' ] , $ parts ), $ config [ ' separator ' ] );
224- $ entity ->set ($ config [ ' field ' ] , $ slug );
235+ $ slug = $ this ->slug ($ entity , implode ($ separator , $ parts ), $ separator );
236+ $ entity ->set ($ field , $ slug );
225237 }
226238
227239 /**
@@ -245,11 +257,15 @@ public function findSlugged(Query $query, array $options)
245257 *
246258 * @param \Cake\ORM\Entity|string $entity Entity to create slug for
247259 * @param string $string String to create slug for.
248- * @param string $separator Separator.
260+ * @param string|null $separator Separator.
249261 * @return string Slug.
250262 */
251- public function slug ($ entity , $ string = null , $ separator = ' - ' )
263+ public function slug ($ entity , $ string = null , $ separator = null )
252264 {
265+ if ($ separator === null ) {
266+ $ separator = $ this ->config ('separator ' );
267+ }
268+
253269 if (is_string ($ entity )) {
254270 if ($ string !== null ) {
255271 $ separator = $ string ;
@@ -269,7 +285,8 @@ public function slug($entity, $string = null, $separator = '-')
269285
270286 $ slug = $ this ->_slug ($ string , $ separator );
271287
272- if (isset ($ entity ) && $ unique = $ this ->config ('unique ' )) {
288+ $ unique = $ this ->config ('unique ' );
289+ if (isset ($ entity ) && $ unique ) {
273290 $ slug = $ unique ($ entity , $ slug , $ separator );
274291 }
275292
@@ -284,7 +301,7 @@ public function slug($entity, $string = null, $separator = '-')
284301 * @param string $separator Separator.
285302 * @return string Unique slug.
286303 */
287- protected function _uniqueSlug (Entity $ entity , $ slug , $ separator = ' - ' )
304+ protected function _uniqueSlug (Entity $ entity , $ slug , $ separator )
288305 {
289306 $ primaryKey = $ this ->_table ->primaryKey ();
290307 $ field = $ this ->_table ->aliasField ($ this ->config ('field ' ));
0 commit comments