Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Display the related tags

real34 edited this page Sep 14, 2010 · 2 revisions

Now that your model is Taggable and your forms allow users to add tags to objects, you may want to display the related tags.

First, you need to ensure that the related HABTM Tag association is fetched in your find (using the recursive model attribute or the Containable behavior).

Then you could display the tags using either the Model.tags field value (containing a comma separated list of related tags), or the related Tag model data.

Here is a sample data array (Recipe acts as Tags.Taggable):

Array 
	(
		[Recipe] => Array
		   (
		        [id] => 1
		        [name] => Recipe #1
		        [content] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
		        [created] => 2010-01-13 10:03:20
		        [modified] => 2010-01-13 10:23:45
		        [tags] => Dessert, Cake
		    )
		[Tag] => Array
		    (
		        [0] => Array
		            (
		                [id] => 4b4d9121-459c-4abd-af4b-2f981380bfcb
		                [identifier] => 
		                [name] => Dessert
		                [keyname] => dessert
		                [weight] => 0
		                [created] => 2010-01-13 10:23:45
		                [modified] => 2010-01-13 10:23:45
		                [Tagged] => Array
		                    (
		                        [id] => 4b4d9121-cb54-4a9e-970d-2f981380bfcb
		                        [foreign_key] => 1
		                        [tag_id] => 4b4d9121-459c-4abd-af4b-2f981380bfcb
		                        [model] => Recipe
		                        [language] => en-us
		                        [created] => 2010-01-13 10:23:45
		                        [modified] => 2010-01-13 10:23:45
		                    )
		            )
		        [1] => Array
		            (
		                [id] => 4b4d9121-fe58-4fe7-b65a-2f981380bfcb
		                [identifier] => 
		                [name] => Cake
		                [keyname] => cake
		                [weight] => 0
		                [created] => 2010-01-13 10:23:45
		                [modified] => 2010-01-13 10:23:45
		                [Tagged] => Array
		                    (
		                        [id] => 4b4d9121-f3ec-4a24-bb32-2f981380bfcb
		                        [foreign_key] => 1
		                        [tag_id] => 4b4d9121-fe58-4fe7-b65a-2f981380bfcb
		                        [model] => Recipe
		                        [language] => en-us
		                        [created] => 2010-01-13 10:23:45
		                        [modified] => 2010-01-13 10:23:45
		                    )
		            )
		    )
	)

Note: if the HABTM relationship is not included in the find, the Model.tags field will be set an empty string value

Next: Display a Tag cloud with the TagCloud helper

Clone this wiki locally