yii\behaviors\SluggableBehavior::getValue PHP Method

getValue() protected method

protected getValue ( $event )
    protected function getValue($event)
    {
        if ($this->attribute !== null) {
            if ($this->isNewSlugNeeded()) {
                $slugParts = [];
                foreach ((array) $this->attribute as $attribute) {
                    $slugParts[] = ArrayHelper::getValue($this->owner, $attribute);
                }
                $slug = $this->generateSlug($slugParts);
            } else {
                return $this->owner->{$this->slugAttribute};
            }
        } else {
            $slug = parent::getValue($event);
        }
        return $this->ensureUnique ? $this->makeUnique($slug) : $slug;
    }

Usage Example

コード例 #1
0
ファイル: Slug.php プロジェクト: zelenin/yii2-slug-behavior
 /**
  * @inheritdoc
  */
 protected function getValue($event)
 {
     /* @var $owner ActiveRecord */
     $owner = $this->owner;
     if (!empty($owner->{$this->slugAttribute}) && !$this->slugIsEmpty && $this->immutable) {
         $slug = $owner->{$this->slugAttribute};
     } else {
         if ($owner->getIsNewRecord()) {
             $this->slugIsEmpty = true;
         }
         if ($this->attribute !== null) {
             $attributes = $this->attribute;
             $slugParts = array_map(function ($attribute) {
                 return ArrayHelper::getValue($this->owner, $attribute);
             }, $attributes);
             $slug = $this->slugify(implode($this->replacement, $slugParts), $this->replacement, $this->lowercase);
             if (!$owner->getIsNewRecord() && $this->slugIsEmpty) {
                 $owner->{$this->slugAttribute} = $slug;
                 $owner->save(false, [$this->slugAttribute]);
             }
         } else {
             $slug = parent::getValue($event);
         }
     }
     if ($this->ensureUnique) {
         $baseSlug = $slug;
         $iteration = 0;
         while (!$this->validateSlug($slug)) {
             $iteration++;
             $slug = $this->generateUniqueSlug($baseSlug, $iteration);
         }
     }
     return $slug;
 }
All Usage Examples Of yii\behaviors\SluggableBehavior::getValue