diff --git a/components/BlogTagSearch.php b/components/BlogTagSearch.php index b504c8e..51642a4 100644 --- a/components/BlogTagSearch.php +++ b/components/BlogTagSearch.php @@ -150,7 +150,8 @@ public function onLoadPage($page = false) $this->calculatePagination(); // Query the tag with it's posts - $this->tag = Tag::where('slug', $this->property('tag')) + $this->tag = Tag::where('name', $this->property('tag')) + ->orWhere('slug', $this->property('tag')) ->with(['posts' => function($posts) { $posts->skip($this->resultsPerPage * ($this->currentPage - 1)) ->take($this->resultsPerPage); diff --git a/models/Tag.php b/models/Tag.php index ee4fcd1..1fc539d 100644 --- a/models/Tag.php +++ b/models/Tag.php @@ -30,7 +30,10 @@ class Tag extends Model /** * @var array Fillable fields */ - public $fillable = ['name']; + public $fillable = [ + 'name', + 'slug', + ]; /** * @var array Validation rules @@ -42,20 +45,35 @@ class Tag extends Model public $customMessages = [ 'name.required' => 'bedard.blogtags::lang.form.name_required', 'name.unique' => 'bedard.blogtags::lang.form.name_unique', + 'name.regex' => 'bedard.blogtags::lang.form.name_invalid', ]; /** - * Convert tag names to slugs + * Before create. + * + * @return void + */ + public function beforeCreate() + { + $this->setInitialSlug(); + } + + /** + * Set the initial slug. + * + * @return void */ - public function setSlugAttribute($value) + protected function setInitialSlug() { - $newSlug = str_slug($value); - $this->attributes['slug'] = $newSlug; + $this->slug = str_slug($this->name); + } - if (empty($newSlug)) - { - $this->attributes['slug'] = str_slug($this->attributes['name']); - } + /** + * Convert tag names to lower case + */ + public function setNameAttribute($value) + { + $this->attributes['name'] = mb_strtolower($value); } /** diff --git a/models/tag/fields.yaml b/models/tag/fields.yaml index d239e2c..a4ae8d9 100644 --- a/models/tag/fields.yaml +++ b/models/tag/fields.yaml @@ -5,20 +5,9 @@ fields: name: label: Tag - span: left - type: text - - slug: - label: Slug - span: right - placeholder: slug - preset: - field: name - type: slug posts: label: Posts type: relation nameFrom: title emptyOption: No posts available. - span: auto diff --git a/updates/add_tag_slug.php b/updates/add_slug_column.php similarity index 80% rename from updates/add_tag_slug.php rename to updates/add_slug_column.php index 68dcd8d..f738fe3 100644 --- a/updates/add_tag_slug.php +++ b/updates/add_slug_column.php @@ -3,6 +3,7 @@ use Schema; use October\Rain\Database\Updates\Migration; use Bedard\BlogTags\Models\Tag; +use System\Classes\PluginManager; class AddTagSlug extends Migration { @@ -15,15 +16,10 @@ public function up() Schema::table('bedard_blogtags_tags', function($table) { - $table->string('slug', 255); + $table->string('slug')->unique()->nullable(); }); $this->fillSlugs(); - - Schema::table('bedard_blogtags_tags', function($table) - { - $table->unique('slug'); - }); } public function down() @@ -43,8 +39,7 @@ private function fillSlugs() { $tags = Tag::all(); - foreach ($tags as $tag) - { + foreach ($tags as $tag) { $tag->slug = str_slug($tag->name); $tag->save(); } diff --git a/updates/version.yaml b/updates/version.yaml index c4594a6..12b7afa 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -28,3 +28,8 @@ - Add Hungarian translation (gergo85) 1.3.6: - Add German translation (justb81) +1.3.7: + - Add slug field to tags (Fl0Cri) + - add_slug_column.php +1.4.0: + - !!! A slug is now created for each tag.