Skip to content

Commit

Permalink
Merge pull request #19 from joonas1234/master
Browse files Browse the repository at this point in the history
Option to add prefix for slug
  • Loading branch information
benjaminhirsch authored Sep 11, 2019
2 parents 2072919 + 973bc4f commit efe2347
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions resources/js/components/Slug/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default {
return;
}
this.value = slugify(value, this.field.slugifyOptions || {});
this.appendSlugPrefix(this.value);
})
},
Expand Down Expand Up @@ -75,6 +76,18 @@ export default {
*/
setInitialValue() {
this.value = this.field.value || ''
if(this.$router.currentRoute.name === 'create') {
this.appendSlugPrefix()
}
},
/*
* Add slug prefix to slug if it is set
*/
appendSlugPrefix() {
if(this.field.slugPrefix) {
this.value = this.field.slugPrefix + '/' + this.value
}
},
/**
Expand Down
20 changes: 20 additions & 0 deletions src/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Slug extends Field
*/
private $slugifyOptions = [];

/**
* Add prefix for generated slug
*
* @var null|string
*/
private $slugPrefix;

/**
* Specify options to pass to speakingurl.
*
Expand Down Expand Up @@ -73,6 +80,17 @@ public function showUrlPreview(string $url): Element
return $this;
}

/**
* Add prefix to generated url
*
* @return $this
*/
public function slugPrefix(string $prefix): Element
{
$this->slugPrefix = $prefix;
return $this;
}

/**
* Display the field as raw HTML using Vue.
*
Expand All @@ -89,6 +107,8 @@ public function jsonSerialize()
'disableAutoUpdateWhenUpdating' => $this->disableAutoUpdateWhenUpdating,
'slugifyOptions' => $this->slugifyOptions,
'showPreviewUrl' => $this->showUrlPreview,
'slugPrefix' => $this->slugPrefix,

], parent::jsonSerialize());
}
}

0 comments on commit efe2347

Please sign in to comment.