Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
Implement slug field for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Fl0Cri authored and scottbedard committed Apr 18, 2017
1 parent 415b835 commit 3312362
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
2 changes: 1 addition & 1 deletion components/BlogTagSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function onLoadPage($page = false)
$this->calculatePagination();

// Query the tag with it's posts
$this->tag = Tag::where('name', $this->property('tag'))
$this->tag = Tag::where('slug', $this->property('tag'))
->with(['posts' => function($posts) {
$posts->skip($this->resultsPerPage * ($this->currentPage - 1))
->take($this->resultsPerPage);
Expand Down
24 changes: 8 additions & 16 deletions models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,20 @@ 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',
];

public function __construct(array $attributes = [])
{
if (Config::get('bedard.blogtags::slugify', true)) {
$this->rules['name'] .= '|regex:/^[a-z0-9-]+$/';
}

parent::__construct($attributes);
}

/**
* Convert tag names to lower case
* Convert tag names to slugs
*/
public function setNameAttribute($value)
public function setSlugAttribute($value)
{
if (Config::get('bedard.blogtags::slugify', true)) {
$value = str_replace(' ', '-', $value);
}
$newSlug = str_slug($value);
$this->attributes['slug'] = $newSlug;

$this->attributes['name'] = mb_strtolower($value);
if (empty($newSlug))
{
$this->attributes['slug'] = str_slug($this->attributes['name']);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions models/tag/fields.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
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
52 changes: 52 additions & 0 deletions updates/add_tag_slug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php namespace Bedard\BlogTags\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;
use Bedard\BlogTags\Models\Tag;

class AddTagSlug extends Migration
{
public function up()
{
if(!PluginManager::instance()->hasPlugin('RainLab.Blog'))
{
return;
}

Schema::table('bedard_blogtags_tags', function($table)
{
$table->string('slug', 255);
});

$this->fillSlugs();

Schema::table('bedard_blogtags_tags', function($table)
{
$table->unique('slug');
});
}

public function down()
{
if(!PluginManager::instance()->hasPlugin('RainLab.Blog'))
{
return;
}

Schema::table('bedard_blogtags_tags', function($table)
{
$table->dropColumn('slug');
});
}

private function fillSlugs()
{
$tags = Tag::all();

foreach ($tags as $tag)
{
$tag->slug = str_slug($tag->name);
$tag->save();
}
}
}

0 comments on commit 3312362

Please sign in to comment.