made it easier to extend SluggableBehavior slug generation

fixes #10118
This commit is contained in:
Carsten Brandt
2015-11-07 16:59:24 +01:00
parent effc4facda
commit e2c8c08875
2 changed files with 15 additions and 1 deletions

View File

@@ -52,6 +52,7 @@ Yii Framework 2 Change Log
- Enh #9901: Default `Cache.SerializerPermissions` configuration option for `HTMLPurifier` is set to `0775` (klimov-paul) - Enh #9901: Default `Cache.SerializerPermissions` configuration option for `HTMLPurifier` is set to `0775` (klimov-paul)
- Enh #10056: Allowed any callable to be passed to `ActionColumn::$urlCreator` (freezy-sk) - Enh #10056: Allowed any callable to be passed to `ActionColumn::$urlCreator` (freezy-sk)
- Enh #10061: `yii\helpers\BaseInflector::transliterate()` is now public. Introduced different levels of transliteration strictness (silverfire) - Enh #10061: `yii\helpers\BaseInflector::transliterate()` is now public. Introduced different levels of transliteration strictness (silverfire)
- Enh #10118: Allow easy extension of slug generation in `yii\behaviors\SluggableBehavior` (cebe, hesna)
- Enh: Added last resort measure for `FileHelper::removeDirectory()` fail to unlink symlinks under Windows (samdark) - Enh: Added last resort measure for `FileHelper::removeDirectory()` fail to unlink symlinks under Windows (samdark)
- Chg #9369: `Yii::$app->user->can()` now returns `false` instead of erroring in case `authManager` component is not configured (creocoder) - Chg #9369: `Yii::$app->user->can()` now returns `false` instead of erroring in case `authManager` component is not configured (creocoder)
- Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark) - Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark)

View File

@@ -154,7 +154,7 @@ class SluggableBehavior extends AttributeBehavior
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$slugParts[] = $owner->{$attribute}; $slugParts[] = $owner->{$attribute};
} }
$slug = Inflector::slug(implode('-', $slugParts)); $slug = $this->generateSlug($slugParts);
} else { } else {
$slug = $owner->{$this->slugAttribute}; $slug = $owner->{$this->slugAttribute};
} }
@@ -173,6 +173,19 @@ class SluggableBehavior extends AttributeBehavior
return $slug; return $slug;
} }
/**
* This method is called by [[getValue]] to generate the slug.
* You may override it to customize slug generation.
* The default implementation calls [[\yii\helpers\Inflector::slug()]] on the input strings
* concatenated by dashes (`-`).
* @param array $slugParts an array of strings that should be concatenated and converted to generate the slug value.
* @return string the conversion result.
*/
protected function generateSlug($slugParts)
{
return Inflector::slug(implode('-', $slugParts));
}
/** /**
* Checks if given slug value is unique. * Checks if given slug value is unique.
* @param string $slug slug value * @param string $slug slug value