SluggableBehavior.php 2.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\behaviors;

use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord;
use yii\helpers\Inflector;

/**
Qiang Xue committed
15
 * SluggableBehavior automatically fills the specified attribute with a value that can be used a slug in a URL.
16
 *
17
 * To use SluggableBehavior, insert the following code to your ActiveRecord class:
18 19 20 21 22 23 24 25
 *
 * ```php
 * use yii\behaviors\SluggableBehavior;
 *
 * public function behaviors()
 * {
 *     return [
 *         [
26
 *             'class' => SluggableBehavior::className(),
27
 *             'attribute' => 'title',
Qiang Xue committed
28
 *             // 'slugAttribute' => 'slug',
29 30 31 32
 *         ],
 *     ];
 * }
 * ```
33
 *
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * By default, SluggableBehavior will fill the `slug` attribute with a value that can be used a slug in a URL
 * when the associated AR object is being validated. If your attribute name is different, you may configure
 * the [[slugAttribute]] property like the following:
 *
 * ```php
 * public function behaviors()
 * {
 *     return [
 *         [
 *             'class' => SluggableBehavior::className(),
 *             'slugAttribute' => 'alias',
 *         ],
 *     ];
 * }
 * ```
49 50 51 52 53 54
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
class SluggableBehavior extends AttributeBehavior
{
    /**
Qiang Xue committed
55
     * @var string the attribute that will receive the slug value
56
     */
57
    public $slugAttribute = 'slug';
58
    /**
Qiang Xue committed
59
     * @var string the attribute whose value will be converted into a slug
60 61
     */
    public $attribute;
Qiang Xue committed
62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * @var string|callable the value that will be used as a slug. This can be an anonymous function
     * or an arbitrary value. If the former, the return value of the function will be used as a slug.
     * The signature of the function should be as follows,
     *
     * ```php
     * function ($event)
     * {
     *     // return slug
     * }
     * ```
     */
    public $value;
75

76

77 78 79 80 81
    /**
     * @inheritdoc
     */
    public function init()
    {
82 83 84 85 86 87
        parent::init();

        if (empty($this->attributes)) {
            $this->attributes = [BaseActiveRecord::EVENT_BEFORE_VALIDATE => $this->slugAttribute];
        }

88
        if ($this->attribute === null && $this->value === null) {
Qiang Xue committed
89
            throw new InvalidConfigException('Either "attribute" or "value" property must be specified.');
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        }
    }

    /**
     * @inheritdoc
     */
    protected function getValue($event)
    {
        if ($this->attribute !== null) {
            $this->value = Inflector::slug($this->owner->{$this->attribute});
        }

        return parent::getValue($event);
    }
}