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

Commit

Permalink
Use native taglist and add translation file
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbedard committed Feb 5, 2017
1 parent 1592b08 commit 2258b22
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 84 deletions.
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

76 changes: 16 additions & 60 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class Plugin extends PluginBase
*/
public $require = ['RainLab.Blog'];

/**
* @var array Container for tags to be attached
*/
private $tags = [];

/**
* Returns information about this plugin
*
Expand All @@ -31,29 +26,14 @@ class Plugin extends PluginBase
public function pluginDetails()
{
return [
'name' => 'Blog Tags Extension',
'description' => 'Enables tagging blog posts and display related articles.',
'name' => 'bedard.blogtags::lang.plugin.name',
'description' => 'bedard.blogtags::lang.plugin.description',
'author' => 'Scott Bedard',
'icon' => 'icon-tags',
'homepage' => 'https://github.com/scottbedard/blogtags'
];
}

/*
* Owl Registration
*
* @return array
*/
public function registerFormWidgets()
{
return [
'Owl\FormWidgets\Tagbox\Widget' => [
'label' => 'Tagbox',
'code' => 'owl-tagbox'
]
];
}

/**
* Register components
*
Expand All @@ -70,11 +50,11 @@ public function registerComponents()

public function boot()
{
// Extend the navigation
// extend the blog navigation
Event::listen('backend.menu.extendItems', function($manager) {
$manager->addSideMenuItems('RainLab.Blog', 'blog', [
'tags' => [
'label' => 'Tags',
'label' => 'bedard.blogtags::lang.navigation.tags',
'icon' => 'icon-tags',
'code' => 'tags',
'owner' => 'RainLab.Blog',
Expand All @@ -83,50 +63,26 @@ public function boot()
]);
});

// Extend the controller
PostsController::extendFormFields(function($form, $model, $context) {
if (!$model instanceof PostModel) return;
$form->addSecondaryTabFields([
'tagbox' => [
'label' => 'Tags',
'tab' => 'rainlab.blog::lang.post.tab_categories',
'type' => 'owl-tagbox',
'slugify' => Config::get('bedard.blogtags::slugify', true),
]
]);
});

// Extend the model
// extend the post model
PostModel::extend(function($model) {
// Relationship
$model->belongsToMany['tags'] = [
'Bedard\BlogTags\Models\Tag',
'table' => 'bedard_blogtags_post_tag',
'order' => 'name'
];

// getTagboxAttribute()
$model->addDynamicMethod('getTagboxAttribute', function() use ($model) {
return $model->tags()->lists('name');
});

// setTagboxAttribute()
$model->addDynamicMethod('setTagboxAttribute', function($tags) use ($model) {
$this->tags = $tags;
});
});

// Attach tags to model
PostModel::saved(function($model) {
if ($this->tags) {
$ids = [];
foreach ($this->tags as $name) {
$create = Tag::firstOrCreate(['name' => $name]);
$ids[] = $create->id;
}

$model->tags()->sync($ids);
}
// extend the post form
PostsController::extendFormFields(function($form, $model, $context) {
if (!$model instanceof PostModel) return;
$form->addSecondaryTabFields([
'tags' => [
'label' => 'bedard.blogtags::lang.form.label',
'mode' => 'relation',
'tab' => 'rainlab.blog::lang.post.tab_categories',
'type' => 'taglist',
],
]);
});
}
}
16 changes: 0 additions & 16 deletions composer.json

This file was deleted.

27 changes: 27 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
return [
//
// plugin
//
'plugin' => [
'name' => 'Blog Tags Extension',
'description' => 'Adds tagging to RainLab.Blog posts.',
],

//
// form
//
'form' => [
'label' => 'Tags',
'name_invalid' => 'Tag names may only contain alpha-numeric characters and hyphens.',
'name_required' => 'The tag field is required.',
'name_unique' => 'That tag name is already taken.',
],

//
// navigation
//
'navigation' => [
'tags' => 'Tags',
]
];
18 changes: 12 additions & 6 deletions models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ class Tag extends Model
];

public $customMessages = [
'name.required' => 'A tag name is required.',
'name.unique' => 'A tag by that name already exists.',
'name.regex' => 'Tags may only contain alpha-numeric characters and hyphens.'
'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);
}

Expand All @@ -58,21 +59,26 @@ public function __construct(array $attributes = [])
*/
public function setNameAttribute($value)
{
if (Config::get('bedard.blogtags::slugify', true)) {
$value = str_replace(' ', '-', $value);
}

$this->attributes['name'] = mb_strtolower($value);
}

/**
* Sets the "url" attribute with a URL to this object
* @param string $pageName
* @param Cms\Classes\Controller $controller
*
* @param string $pageName
* @param Cms\Classes\Controller $controller
*/
public function setUrl($pageName, $controller)
{
$params = [
'id' => $this->id,
'slug' => $this->slug,
];

return $this->url = $controller->pageUrl($pageName, $params);
}
}

0 comments on commit 2258b22

Please sign in to comment.