Search code examples
sitecoresitecore7.1weblog

How to override ITagManager in WeBlog module within Sitecore


I am trying to implement the solution in this answer to be able to restrict the number of tags which show in the tag cloud in WeBlog. Additionally, I am using these instructions from the documentation.

I have modified the WeBlog config to point to my own custom TagManager implementation.

<setting name="WeBlog.Implementation.TagManager" value="My.Namespace.CustomTagManager"/>

If I load sitecore/admin/showconfig.aspx I can confirm the config setting has been updated with the new value.

My CustomTagManager is currently a bare bones implementation of the ITagManager interface.

public class CustomTagManager : ITagManager
{
    public string[] GetTagsByBlog(ID blogId)
    {
        throw new System.NotImplementedException();
    }

    public string[] GetTagsByBlog(Item blogItem)
    {
        throw new System.NotImplementedException();
    }

    public Dictionary<string, int> GetTagsByEntry(EntryItem entry)
    {
        throw new System.NotImplementedException();
    }

    public Dictionary<string, int> GetAllTags()
    {
        throw new System.NotImplementedException();
    }

    public Dictionary<string, int> GetAllTags(BlogHomeItem blog)
    {
        throw new System.NotImplementedException();
    }

    public Dictionary<string, int> SortByWeight(IEnumerable<string> tags)
    {
        throw new System.NotImplementedException();
    }
}

I can reflect the deployed DLL and see these changes have definitely be made, but the changes have no affect. None of the exceptions are thrown and the tag cloud continues to populate as if I'd made no changes at all. It is like the config file change is being completely ignored.

What else do I need to change in order to write my own customer TagManager class?

I am using WeBlog 5.2 and Sitecore 7.1.


Solution

  • After looking at the WeBlog code it was clear that the fallback object was being used and my config change was being ignored.

    The cause for this is that WeBlog does:

    var type = Type.GetType(typeName, false);
    

    The GetType method only works when the type is found in either mscorlib.dll or the currently assembly. As such, the fix is as simple as providing the assemblies fully qualified name.

    <setting name="WeBlog.Implementation.TagManager" value="My.Assembly.CustomTagManager, My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    

    This is the WeBlog code:

    private static T CreateInstance<T>(string typeName, Func<T> fallbackCreation) where T : class
    {
        var type = Type.GetType(typeName, false);
        T instance = null;
        if (type != null)
        {
            try
            {
                instance = (T)Sitecore.Reflection.ReflectionUtil.CreateObject(type);
            }
            catch(Exception ex)
            {
                Log.Error("Failed to create instance of type '{0}' as type '{1}'".FormatWith(type.FullName, typeof(T).FullName), ex, typeof(ManagerFactory));
            }
        }
    
        if(instance == null)
            instance = fallbackCreation();
    
        return instance;
    }