Using menuitemgroup as if-statement

Lennard van Diggelen asked on September 29, 2016 12:05

Hi there,

I'm using a CMSUniView to build a menu and I only want to get certain pages form a menuitemgroup. So what I use is WhereCondition="MenuItemGroup Like '%main%'" which works like a charm.

Now I only want to add a class to a li item if the Child Nodes also contain the menuitemgroup 'main'. like this:

<li class='menu__item <%# ((string)Eval("NodeHasChildren").ToString()) == "True" ? "menu__item--withsub" : "" %>'>

Now it shows the class always because the page has children of course but it must only be triggered if the childpage contains 'main' as menuitemgroup as well. What is the best way to achieve this or how can I get the Menuitemgroup as a value?

I use a ASPX template page for this

Thanks!

Correct Answer

Trevor Fayas answered on September 29, 2016 13:53

Although you may want to write a custom transformation function and use caching (since to do this you'll have to do a sub-query for each item of the transformation, which depending on the items can cause delays so caching the results will be vital for fast performance), the code is as below.

CMS.DocumentEngine.DocumentHelper.GetDocuments("CMS.MenuItem").Where("NodeParentID = " + Eval("NodeID") + " and MenuItemGroup like '%" + CMS.DataEngine.SqlHelper.EscapeQuotes(ItemGroupContainsName) + "%'").Count > 0

This checks only the immediate children. If you want to check for any child at any level, just use

CMS.DocumentEngine.DocumentHelper.GetDocuments("CMS.MenuItem").Where("NodeAliasPath like '" + Eval("NodeAliasPath") + "/%' and MenuItemGroup like '%" + CMS.DataEngine.SqlHelper.EscapeQuotes(ItemGroupContainsName) + "%'").Count > 0

Below is the full app_code class that will add this method with proper caching:

using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.Helpers;
using CMS.Membership;
using CMS.SiteProvider;
using System.Linq;

namespace CMS.Controls
{
/// <summary>
/// Extends the CMSTransformation partial class.
/// </summary>
public partial class CMSTransformation
{
    /// <summary>
    /// Trims text values to the specified length.
    /// </summary>
    /// <param name="txtValue">Original text to be trimmed</param>
    /// <param name="leftChars">Number of characters to be returned</param>
    public bool HasChildrenOfItemGroup(object DocumentIDobj, string ItemGroupContainsName)
    {
        int DocID = ValidationHelper.GetInteger(DocumentIDobj, -1);
        if(DocID < 0) {
            return false;
            // Possibly also report error to event log
        }
        return CacheHelper.Cache<bool>(cs => HasChildrenOfItemGroupHelper(cs, DocID, ItemGroupContainsName), new CacheSettings(10, "hasChildrenOfItemGroup|DocID:" + DocID.ToString() + "|ContainName:" + ItemGroupContainsName));
    }


    private bool HasChildrenOfItemGroupHelper(CacheSettings cs, int DocumentID, string ItemGroupContainsName)
    {

        var theDocument = DocumentHelper.GetDocument(DocumentID, new TreeProvider(MembershipContext.AuthenticatedUser));

        bool result = DocumentHelper.GetDocuments("CMS.MenuItem").Where("NodeParentID = " + theDocument.NodeID + " and MenuItemGroup like '%" + SqlHelper.EscapeQuotes(ItemGroupContainsName) + "%'").Count > 0;

        //bool result = theDocument.Children.Where(x => x.GetStringValue("MenuItemGroup", "").ToLower().Contains(ItemGroupContainsName.ToLower())).Count() > 1;
        // Checks whether the data should be cached (based on the CacheSettings)
        if (cs.Cached)
        {
            // Sets the cache dependency to remove this cache for any changes on the child nodes of this document.
            cs.CacheDependency = CacheHelper.GetCacheDependency("node|"+SiteContext.CurrentSiteName+"|"+theDocument.NodeAliasPath+"|childnodes");
        }
        return result;
    }
}
}

The transformation method call will then be

<%# If(HasChildrenOfItemGroup(Eval("DocumentID"), "top"), "menu__item--withsub", "") %>

Took a bit to get this going and tested, so please mark as answered if helped!

2 votesVote for this answer Unmark Correct answer

Recent Answers


Lennard van Diggelen answered on September 29, 2016 17:21

Worked like a charm! Thanks!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.