Eduardo
Eduardo

Reputation: 1831

Yii2 - Sluggable Behavior

I have configured Sluggable behavior on my model as follows:

public function behaviors() {
        return [
          [
              'class' => SluggableBehavior::className(),
              'attribute' => 'title',
              'ensureUnique' => true,
          ]
        ];
    }

I need to do:

I have found that Sluggable Behaviour has an attribute "immutable" but I do not see a method to manipulate it.

Also I do not see a way to stop automatic generation if value is given.

Any ideas?

Upvotes: 2

Views: 417

Answers (1)

rob006
rob006

Reputation: 22144

For such unusual requirements you should probably extend SluggableBehavior and overwrite getValue() and isNewSlugNeeded() methods to feat your needs.

You may also play with $value property and/or change some behavior settings in beforeValidate() of model:

public function beforeValidate() {
    $this->getBahavior('my-behavior-name')->immutable = !$this->changeSlugCheckbox;

    return parent::beforeValidate();
}

But custom behavior is much more clean solution.

Upvotes: 3

Related Questions