Joomla 3.x – What’s the big deal?

Joomla 3.x is a radical departure from the 2.5 series, but 3.1 brings native tagging functionality, something previously only possible with extensions or solutions such as creative usage of metadata in the database.

One tempting implication is to question the usefulness of the powerful, well-known Joomla component, K2. Arguably a valuable part of Joomla, bringing modern features found in other CMS systems well before core components, K2 also brings some overhead and redundancy.

K2 or Not K2, That is the Question

Certain extensions have questionable, non-existent, or handcrafted K2 compatibility. For my own needs, I decided to create an external PHP script that would bootstrap the Joomla libraries and import K2 items (articles), ratings, comments, tags, and categories.

Some features of K2 are left behind, such as easy custom fields, related posts, etc, but I believe it is a nice step to use the core content component for using articles, using other core Joomla pieces to provide a complete CMS solution, with extra functionality provided by other extensions as necessary.

I’d rather do this as a series of focused posts to generalize too much, but I hope my point is clear. While I hope to provide specific code examples along the way, I’ll try to be brief.

Joomla’s Tagging System: An Overview

Joomla’s tags are:

  • Applicable to multiple extensions, not just articles
  • Hierarchical
  • Many-to-Many Associations (One tag can be associated with many items; An item can have many tags)

Tagging in the Joomla Administrator Backend

From the Tags Component

You can manage a list of tags available on your site through the tags component. You can trash a tag and then delete the tag from the trash without any warnings, even if it was associated with one or more articles/items.

From the Content (Article) Component

You can also manage tags when editing an article. You can simply start typing the name of the tag you’d like to apply, and existing tags will be searched as you type and autocompleted for you. New tags can be typed in all the same, and they’ll be created (and available to be reused elsewhere) when the article is saved.

Applying Joomla Tagging Programmatically

Joomla uses the following logic to prepare tags:

When an article is saved, the json-encoded metadata for the article will contain a “tags” key which refers to an array of integer tag-ids. Any tags that do not yet exist will contain the tag’s text prefixed by the string “#new#”.

When the article is saved, the meatdata will be json-decoded and the array will be parsed. Any non-integer values will have the prefix removed and the remaining string will be used for the tag name.

If you want to import K2 tag associations, you will have several caveats:

  • If you select old tags from your database and decide to iterate over them in a loop, you will want to let the tag be automatically created the first time it is referenced, then just refer to the tag-id integer into your array.
  • If you want to use a loop, get the old tags and create them all at once. Save mappings of the old K2 tag ids to the new Joomla tags Ids. Save the article metadata in a separate step (again, so your array will only contain the shiny new ints).
  • When you’re creating associations between tags and articles, you need to keep track of which K2 item ids correspond to each new article (content_item) id as well.

I suggest you do not overwrite metadata from scratch. Instead, fetch the current value from the database and json-decode the content. Manipulate the resulting object and add the tags. Below is a sample code snippet:

// Joomla article object: $article
$metadata = $article->meta_data;
// JSON-decode the metadata
$metadata = json_decode($metadata);
// Begin inserting tags
foreach ($tag_array[$article->id] as $tag) { // Assume you have a multi-dimensional array of tags for each article
	$metadata->tags[] = $tag; // Add the tag to an array in a "tags" property
}
// Serialize the metadata
$metadata = json_encode($metadata);
// Assign to the article object
$article->meta_data = $metadata;
// Proceed and eventually save -- Call the store() method if you've got a JTable object

That’s all for now; check back for more as I document what I have already done.