Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to add prefix for slug #19

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}