The goal of this bundle is to allow you to associate tags with several entities and manage them.
Add the bundle in your composer.json :
"require": {
"mykees/symfony2-tag-bundle": "1.0.*@dev"
}
or
$ php composer.phar require mykees/symfony2-tag-bundle "1.0.*@dev"
Enable the bundle :
$bundles = array(
// ...
new Mykees\TagBundle\MykeesTagBundle(),
);
Add routing in your app/config/routing.yml :
mykees_tag_admin:
resource: "@MykeesTagBundle/Resources/config/routing_admin.yml"
prefix: /admin/tag
Update your database :
php app/console doctrine:schema:update --force
Add Taggable interface and TaggableTrait trait, in your entity as in the following example :
namespace YourProject\YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Mykees\TagBundle\Interfaces\Taggable;
use Mykees\TagBundle\Traits\TaggableTrait;
/**
*Post
*@ORM\Table()
*@ORM\Entity()
*@ORM\HasLifecycleCallbacks()
*/
class Post implements Taggable
{
use TaggableTrait;
.....
}
In your form type, add tags field (You can add text field options if you need) :
$builder
//...
->add('tags','mk_tag',[
'label'=>'Les Tags:',
])
;
Add input text in your template :
{{ form_row(form.tags) }}
Now you can add tags separated by a comma.
In your controller actions, you can use some useful functions for retrieve your tags :
// Find tags associated with a Taggable entity or array of Taggable entities
$this->get('mk.tag_manager')->findTagRelation($model)
// Find tags by name
$tag = $this->get('mk.tag_manager')->findByName($names)
And in your template you can display the tags like that :
{% if post.getTags() %}
{% for tag in post.getTags %}
<span class="label label-primary"> {{ tag.name }} </span>
{% endfor %}
{% endif %}
For more comfort, removing tags uses Ajax.
In your edit content, add javascript extension (don't forget to add jquery in your default template) :
// src/YourProject/YourBundle/Resources/views/Blog/edit.html.twig
{% extends 'base.html.twig' %}
{% block body %}
.....
{% endblock %}
{% block javascripts %}
{{ parent() }}
{{ mk_tag_javascript() }}
{% endblock %}
Now you can delete the tags by adding a delete link on them like the following example :
{% for tag in post.getTags %}
{% if tag %}
<span class="label label-primary">
{{ tag.name }}
{% for tr in tag.getTagRelation %}
<a href="{{ path('mykees_delete_relation',{'relation_id':tr.id}) }}" class="delTag">[x]
</a>
{% endfor %}
</span>
{% endif %}
{% endfor %}
Remove a tag by name :
$this->get('mk.tag_manager')->deleteByName($tag_name)
Remove a tag by id :
$this->get('mk.tag_manager')->delete($tag_id)
Delete relation between tag(s) and an entity :
$this->get('mk.tag_manager')->deleteTagRelation(Taggable $entity, true)
Save relation between tag(s) and entity :
$this->get('mk.tag_manager')->saveRelation(Taggable $entity)
Find entity ids associated to a tag :
$entities_id = $this->get('mk.tag_manager')->findReferer( $slug_tag, $model_type )
Find tags by name. $tag_name parameter can be an array of tags name :
$this->get('mk.tag_manager')->findByName($tag_name)
Create a tag :
$this->get('mk.tag_manager')->create($name)
Create several tags :
$tags = ['tag1','tag2','tag3','tag4'];
$this->get('mk.tag_manager')->manageTags($tags)