Skip to content
kalt edited this page Sep 13, 2010 · 9 revisions

Let’s see how to tag any Model. The plugin provides a Behavior and a Helper to achieve that.

2.1. Models

class Article extends AppModel
{
	var $actsAs = array('Tagging.Taggable');
}

2.2. Controllers

class ArticlesController extends AppController
{
	var $helpers = array('Tagging.Tagging');
}

2.3. Views

A single input field for comma-separated tags, in add/edit views.

2.3.a. Classic input

echo $form->input('tags');

2.3.b. With ajax tag suggestions

echo $tagging->input('tags');

Requires jQuery in <head>...</head>

That’s it, you can tag any Model.

2.3.c. Ajax tag suggestions options

If you choose this solution to add tags to a record, you will see suggested tags as you type.

You can change any option as with the core Paginator Helper:

$tagging->options(array('option_key' => 'value', ...));

Here are the default options :

  • selector : DOM selector to be observed (try to keep it simple, one id (‘#xyz’) or one class (‘.xyz’) only). Defaults to ‘.tagSuggest’.
  • url : url to get suggestions via ajax POST call (JSON formatted response). Defaults to ‘/tagging/tags/suggest’.
  • delay : sets the delay between keyup and the request (in milliseconds). Defaults to 500.
  • matchClass : CSS class applied to the suggestions. Defaults to ‘tagMatches’.
  • sort : boolean to force the sorted order of suggestions. Defaults to false.
  • tagContainer : the type of element used to contain the suggestions. Defaults to ‘span’.
  • tagWrap : the type of element the suggestions a wrapped in. Defaults to ‘span’.
  • tags : array of tags specific to this instance of element matches (if you don’t want to use ajax but a predefined list of tags). Defaults to null.

If you want to change the way tag suggestions are displayed, simply change the ‘matchClass’ option and provide your own CSS styles according to the new ‘matchClass’ name.

The second parameter of $tagging→input() can be an array of options just as the classic $form→input().