How to add custom tags to your documents
In order to properly tag your documents I recommend using a special table that will have all the values you need. By using a table you can easily change the values and also add new ones quite fast.
1. Create a table in your modx database:
In my case the table will be named tags_modx_versions and will have 2 fields id and name
Here's the SQL command for creating this table
CREATE TABLE `tags_modx_versions` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, UNIQUE (`id`))
Insert the first 2 values: Evolution and Revolution
INSERT INTO `tags_modx_versions` (`id`, `name`) VALUES ('', 'Evolution'), ('', 'Revolution');
At this point we're done with the database stuff.
2. Create the TV
Log in into the Manager and create a new TV. I will name it modxVersion.
Enter a caption/description, select Checkbox for Input Type and paste the following into Input Option Values:
@SELECT name,id FROM tags_modx_versions ORDER BY name
Widget: Delimited List
Delimiter: ,
Before saving the TV you have to select the template you want to use it for.
Click here to see a screen grab.
3. Display the tags
At this point if you want to use the TV you will get an ID or a comma separated list of IDs.
To display the names associated with those IDs we have to create a new snippet:
<?php
// [[GetModxVersion? &ids=`[*modxVersion*]`]]
$ids = isset($ids) ? $ids : 0;
if($ids){
$values = array();
$res = $modx->db->select("name", "tags_modx_versions", "id IN ($ids)", "name ASC");
while ($row = $modx->db->getRow( $res )) {
array_push($values, $row['name']);
}
return implode(', ', $values);
}
?>
Call the snippet anywhere in your document like:
[[GetModxVersion? &ids=`[*modxVersion*]` ]]
This was quite a long one, but now you can easily add new tags by using the Insert functionality in phpMyAdmin or your favorite database tool.
Note: A modx module can be created in order to Add or Delete new tags. I will handle this in a future post.
Write a comment
Posts: 2
Reply #3 on : Mon October 19, 2009, 03:41:13
Posts: 1
Reply #2 on : Mon October 19, 2009, 08:51:13
Posts: 2
Reply #1 on : Mon October 19, 2009, 15:53:51