Thoughts Electrique

Decentralized categories in OpenCms 7.5

This feature was presented by Michael Emmerich during the OpenCms Days 2009 and caused a lot of people to grab their pen.

A short background on categories

Categories are a built-in content relation in OpenCms and allow files in the VFS to be assigned one or more topics/keywords/whatever. They can often be used as a replacement feature for siblings which are mostly misunderstood by users without a technical background. For example consider the typical example of a news site. You have a folder structure for your articles which could be the following way:

/articles/2008/12-24-merry-christmas.html
/articles/2009/09-01-new-site.html
/articles/2009/10-02-a-great-article.html

You now want to categorize the articles into topics so you would create another folder structure:

/topic/must-read
/topic/technical

The sibling-way would be to create a sibling (put simply: an automatically updating copy) of the articles in the desired topic folder. While this is a working solution it’s counterintuitive to most users. OpenCms 7 introduced the long awaited Content Relation Engine (CRE) including a default relation called categories. Similar to the sibling solution a category is effectively a folder but instead of creating it anywhere in the filesystem it’s created in the root site (“/”) under /system/categories/. Users can now assign categories to VFS files using the context-menu entry Relations -> Assign Categories. This is a much easier approach although it has one caveat: Categories are globally defined in the /system/ folder and can therefore only be created by users with access to the root site and can not be specified on a per site or even per folder basis. This can cause problems in multisite environments.

Decentralization to the rescue

With OpenCms 7.5 this limitation is finally gone. You can now define categories on a per-site and even per-folder basis. Simply create a folder named _categories in the folder where you want the categories to be available. Then create new folders inside this folder to define categories. The categories are available in the parent folder of the _categories-folder and all subfolders. They also cascade down. Example:

/de/_categories/
/de/_categories/hot-topic
/de/_categories/support
/de/news/_categories/
/de/news/_categories/marketing
/de/news/_categories/development

Inside the /de/ folder the categories hot-topic and support are available. Inside /de/news/ the categories hot-topic, support, marketing and development are available.

This feature should easily help you to get your content structured more nicely. Speaking from experience it is crucial to create an evolveable content structure upfront so your editors as well as visitors and search engines get the most out of your site.

See also:

Tags: , , ,

6 Responses to “Decentralized categories in OpenCms 7.5”

  1. Michael Says:

    we were just discussing in the office that this might be one of those features that nobody knows or uses but makes our work (for v8) much more complicated ;)
    just for completeness this feature was already available in version 7.0.0…
    and i am happy somebody is really using it!!

  2. Sebastian Says:

    Hi Michael,

    thanks for the info. To be honest: I think there are a lot of features that nobody uses because they are like hidden gems. I already have a lot of draft posts on them and this was also my idea for the 2009 OpenCms Days talk (which I couldn’t do because of time constraints).

    Are you sure about the 7.0.0? Because the history.txt mentions the addition of decentralized categories in the 7.5 beta 1.

    I hope everything is well with you.

    All the best,
    Sebastian

  3. Michael Says:

    unbelievable, you are absolutely right… it is just so much time ago, i think i am getting old :(

  4. Sebastian Says:

    Maybe it’s just part of becoming a dad.. too many thoughts ;)

    Are you actually working again or just in the office because you can’t stay away from Java?

  5. Paul Says:

    When I first set out to test the category concept, I had trouble finding any example code. So to anyone who’s experiencing that, here’s a simple JSP to get you started:

    <%
    final String REFERENCE_PATH = "/en/categories/"; // the reference (category) path
    final String CATEGORY_NAME = "my-category"; // folder name of the category folder
    final boolean INCLUDE_SUB_CATEGORIES = false;

    CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
    CmsObject cmso = cms.getCmsObject();

    CmsCategoryService catService = CmsCategoryService.getInstance(); // Get the singleton
    List res = catService.readCategoryResources(cmso, CATEGORY_NAME, INCLUDE_SUB_CATEGORIES, REFERENCE_PATH); // read resources that belong to the specified category

    // Print the returned list of resources
    out.println("Resources with category ‘\ + CATEGORY_NAME + \’: \ + res.size() + \\);
    Iterator i = res.iterator();
    while (i.hasNext()) {
    out.println(\\ + ((CmsResource)i.next()).getName());
    }
    %>

  6. Sebastian Says:

    Hi Paul,

    thanks for publishing the example! That’s really nice of you.

    All the best,
    Sebastian

Leave a Reply