Blog Categories in Blog4Umbraco - Take Two

So my first attempt at manipulating Tag Groups by using Events didn't go so well.  Sure, it created the tags with the correct group, but it also left the original tags in the csmTags table, and if you went back and edited the blog entry, the tags would be gone because the Tag Data Type was ignoring anything that didn't belong to the default group.

So. Back to the drawing board.

Edit (10/11/2010): The first package download didn't go so well and failed dismally.  I've uploaded a new version of the package and this time double-tested it, so it should be all good.  Leave a comment if things go awry...

This time I decided to try and tackle the problem at a deeper level - what if I introduced a new Data Type that could intercept the tag group and inject the Blog Category instead?

Package Download: You can download an Umbraco Package to install the Dll that will do this for you from here.  It contains a single Dll that has a couple of Extensions in it aimed at Blog 4 Umbraco, but is not dependent on it being installed.

Enter the Blog Tag Data Type.

I didn't want to have to re-create the entire Tag Data Type, it's Editor Control, or it's Prevalue Editor, so I took advantage of the fact that almost everything can be extended in Umbraco, and just derived a new Data Type class:

 

public class DataType : umbraco.editorControls.tags.DataType
    {
        #region IDataType Members

        private IDataEditor _Editor;

        public override IDataEditor DataEditor
        {
            get
            {
                if (_Editor == null)
                {
                    var preValues = ((umbraco.editorControls.tags.PrevalueEditor)PrevalueEditor).Prevalues;
                    int? nodeId = int.Parse(umbraco.helper.Request("id"));
                    if (nodeId.HasValue)
                    {
                        string category = (string)BlogExtensionsLibrary.GetValueRecursively(nodeId.Value, "category");

                        if (!string.IsNullOrEmpty(category))
                        {
                            if (preValues["group"] == null || (string)preValues["group"] == "default")
                                preValues["group"] = category;
                        }
                    }
                    _Editor = new umbraco.editorControls.tags.DataEditor(Data, preValues);
                }
                return _Editor;
            }
        }

        public override string DataTypeName
        {
            get { return "Blog Tags"; }
        }

        public override Guid Id
        {
            get { return new Guid("BD8B240F-0DE3-47E5-A172-2DE212CC30B6"); }
            // this was the core umbraco tags datatype GUID.
            //get { return new Guid("4023e540-92f5-11dd-ad8b-0800200c9a66"); }
        }

        #endregion
    }

 

Basically, I stripped out all the stuff I didn't want to duplicate from the tags DataType class, and overrode the DataEditor property.  A few interesting things to note:

  • I could have left the Id property alone and not overridden it.  If had done so, my DataType would have taken over the core Tags DataType, and you would basically not have to do anything more.  However, I'm trying to be a good Umbraco Citizen and behave in a right neighbourly fashion.
  • The code has been designed so that if the category attribute doesn't exist in the Document Type or one of it's Ancestors, then it will not try to inject anything into the group, so it would be quite safe to replace the Core Tags Control with the new one.
  • BlogExtensionsLibrary.GetValueRecursively() is described in my previous post attempting to deal with Blog Categories, so not going to go into it here.

Ok, once you have compiled your code and copied the dll into the bin directory of your Umbraco installation, the remaining steps are as follows:

Update the Tags Data Type, or alternatively create a new one.

You will need to go and change the Tags Data Type so that it uses the new Blog Tags control instead of the Core Tags control.  Note that if you decided not to play nice, and let the new code override the Core Tags control by using the old GUID, then essentially you shouldn't need to do anything here!

Blog Tags Data Type

Update the Blog Post Document Type

You would only need to change the Blog Post Document Type if you created a new Data Type instead of modifying the Core Tags Data Type.

16 comments for “Blog Categories in Blog4Umbraco - Take Two”

  1. Posted Friday, November 05, 2010 at 5:06:01 AM

    Note: the download package appears to be missing the DLL. It only had the package.xml file in it.

  2. Gravatar of Tim FinucaneTim Finucane
    Posted Friday, November 05, 2010 at 5:56:08 AM

    Also, it throw the following error when attempting to install the package:

    Could not upload file
    System.Exception: Error unpacking extension... ---> ICSharpCode.SharpZipLib.Zip.ZipException: Wrong Local header signature: 0x3CBFBBEF at ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry() at umbraco.cms.businesslogic.packager.Installer.unPack(String ZipName) at umbraco.cms.businesslogic.packager.Installer.Import(String InputFile) --- End of inner exception stack trace --- at umbraco.cms.businesslogic.packager.Installer.Import(String InputFile) at umbraco.presentation.developer.packages.Installer.uploadFile(Object sender, EventArgs e)

  3. Gravatar of Robert FosterRobert Foster
    Posted Wednesday, November 10, 2010 at 10:20:19 AM

    Hi Tim,
    Hmmph. Not sure how that happened - I've rebuilt the package and uploaded it again - this time I tested on another umbraco site to confirm the upload contains the DLL and behaves correctly - in addition to installing the dll, the package also creates the Blog Tag data type for you as well.

    Let me know if you have any further issues or questions...

  4. Posted Thursday, November 11, 2010 at 1:46:38 AM

    After installing the package I get the following error when saving the blog:

    [FileNotFoundException: Could not load file or assembly 'refactored.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.]
    refactored.Blog4UmbracoExtensions.EventHandlers.Comments.Document_BeforeSave(Document sender, SaveEventArgs e) +0
    umbraco.cms.businesslogic.web.Document.FireBeforeSave(SaveEventArgs e) +76
    umbraco.controls.ContentControl.saveClick(Object Sender, ImageClickEventArgs e) +163
    System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +115
    System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +120
    System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

  5. Gravatar of Robert FosterRobert Foster
    Posted Thursday, November 11, 2010 at 3:50:28 AM

    Feel like a guinea pig yet? There were a few missing dependencies fixed and uploaded version 0.3 of the package, give it another shot...

  6. Posted Thursday, November 11, 2010 at 6:32:43 AM

    The latest package worked to get everything working for me. Thanks! Not that the issues are worked out you should submit this as a package on the our.umbraco.org site!

  7. Gravatar of Robert FosterRobert Foster
    Posted Thursday, November 11, 2010 at 1:12:06 PM

    Yes, I was thinking of doing that, I need to add some more comprehensive documentation first I think... will try to find some time over the next week to do so.

    Thanks for the feedback

  8. Gravatar of Owen HopeOwen Hope
    Posted Wednesday, December 08, 2010 at 9:04:54 AM

    I installed your package without error, however my Tag Cloud macros are not including these new "Blog Tag" Tags... is this a problem on my end of was that not supported in this project.

    Thanks!

  9. Posted Wednesday, December 08, 2010 at 11:35:10 AM

    Hi Owen,
    Take a look at the followup Blog entry:
    http://refactored.com.au/blog/2010/10/19/altering-xslt-files-for-blog-categories-with-tags

    It deals with this issue...

  10. Gravatar of OwenOwen
    Posted Wednesday, December 08, 2010 at 11:36:04 AM

    Hi Robert,

    I resolved the issues with the above post so please ignore it.

    However I am still getting mixed Tags in my Blog after installing your package. I installed the .zip file as a local package and changed the Doc Type to "Blog Tags" is there anything I missed?

    Thanks..

    Owen

  11. Gravatar of DougDoug
    Posted Wednesday, February 02, 2011 at 8:40:50 AM

    This solution is what I'm looking for, but the package download from your website isn't able to be installed. In umbraco 4.5.2 or 4.6.1, both versions I get this message when trying to install the package locally:

    Could not upload file
    System.Exception: Error unpacking extension... ---> ICSharpCode.SharpZipLib.Zip.ZipException: Wrong Local header signature: 0x88B1F at ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry() at umbraco.cms.businesslogic.packager.Installer.unPack(String ZipName) at umbraco.cms.businesslogic.packager.Installer.Import(String InputFile) --- End of inner exception stack trace --- at umbraco.cms.businesslogic.packager.Installer.Import(String InputFile) at umbraco.presentation.developer.packages.Installer.uploadFile(Object sender, EventArgs e)

    Please help!
    Thanks

  12. Gravatar of BenBen
    Posted Wednesday, July 27, 2011 at 11:36:26 PM

    Hi,

    I've implemeneted your changes but the tagging doesn't seem to be working correctly.
    When I initiially create a post and tag it, it tags correctly with the correct group. However when I edit that post it loses the tag. If I go to add the tag again it creates the tag in the db with a group of 'default'. When i view on the front end the post now has the same post twice. In Umbraco I can only see one tag.

    Is this to do with your DataType class not overriding the set method? Or am I doing something wrong?

    Thanks in advance

    Ben

  13. Gravatar of PushpendraPushpendra
    Posted Monday, December 31, 2012 at 6:42:16 PM

    Hi,

    I have installed your package successfully, but i cant add any tags in my blog post. Can you please tell me how to add tags in my blog posts..

  14. Posted Tuesday, July 08, 2014 at 6:33:07 PM

    As I were built with a ball investing a couple of days with my buddies and seeking moules and frites, we did not get home having a packed truck like we normally do. Read much more about our Lille trip within this publish where Used to do an entire review.

  15. Posted Tuesday, July 08, 2014 at 6:34:07 PM

    http://www.cropwalkshop.com

  16. Posted Tuesday, July 08, 2014 at 6:34:37 PM

    http://en.trueland.net/index1.html

Post a comment