2

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:

  • If the user fills a form field called "URL", this should be used instead of the automatic generated slug.
  • If user changes the title, they will mark a checkbox if they want the slug updated.

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?

Eduardo
  • 1,781
  • 3
  • 26
  • 61

1 Answers1

3

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.

rob006
  • 21,383
  • 5
  • 53
  • 74