Adding categories to a Midgard blog

Hot on the heels of my previous Midgard CMS tutorial, "Creating custom navigation with MidCOM, here is a new one about adding categories to a Midgard-powered blog. I've written this in response to Robert Guerra's questions.

Midgard CMS contains a powerful PHP-level data abstraction layer called datamanager. With datamanager it is possible to freely modify the content fields and editing widgets used with any component.

The first thing when adding new categories is to make the category field array. Robert's example was:

"category" => array(
  "description" => "Category",
  "datatype" => "text",
  "location" => "parameter",
  "widget" => "select",
  "widget_select_choices" => array (
     "Privaterra" => "Privaterra",
     "Midgard" => "Midgard",
     "Travel" => "Travel",
     "Security" => "Security",

While I would use more descriptive keys than cat1 for the categories, the actual field definition looks correct.

What you need to do next is to copy the default de.linkm.newsticker schema from the component API reference and add your own field definition there.

it is the area starting with:

"default" => array (
    "name"        => "default",
    "description" => "News Article",

Copy that area into a text editor (or just your clip board), and add the category block you've made into the "fields" array. A good place to do that would be between "title" and "abstract".

Then you need to store this new schema definition into your Midgard setup as a snippet.

Go to Aegir, select the "Snippet" tab from the left, and create a snippet directory named "sitegroup-config". Then under that create another snippet directory named "de.linkm.newsticker" by clicking "New" on the right-hand frame's menu.

This is the default location used for MidCOM's configuration. You don't need to follow that convention but I'd recommend it.

Now that you have the snippetdirs created, click "New" (again in the right-hand frame), and create a snippet named "schema-blog", and paste your schema array there.

Then you need to configure your newsticker to use the new schema. Here you have two options, either set it to be used by all "newsticker" folders on your site, or only your blog.

To use it for all newstickers, create another snippet named "config" in the /sitegroup-config/de.linkm.newsticker directory, and in that snippet place the following:

"schemadb" => "/sitegroup-config/de.linkm.newsticker/schema-blog",

If you want to enable it for only one newsticker folder (or override the default specified above), you can do this by going to the newsticker folder on site, and clicking the "Component configuration" toolbar item. Scroll down the form, and enter your schema's path in the "Path to the schema database" field:


Now your blog should have categories enabled. I'm not sure how Ecto handles them, but probably the next time you refresh your posting list there, the category selections should appear to the editor. At least that is how it works in MarsEdit. The categories will also appear in the RSS feed generated from your blog. But they won't yet appear on your site. For that you need to modify the output templates of your blog, and that is the subject of another lesson :-)

Note: the steps needed for adding simple category support to your blog might sounds exceedingly difficult. However, it has to be kept in mind that you can use the exactly same technique for controlling all data structures on your site (adding new content fields to articles, photos or whatever). There is more information about modifying schemas in MidCOM schema documentation.

Read more Midgard posts.