mluker
mluker

Reputation: 702

Glass Mapper V4 Language Item Fallback, how to make it work with Autofac?

I am using Glass Mapper v4 with Autofac and cant figure out how to make it work with the Language Item Fall back module. There are examples of creating a class that implements IObjectConstructionTask (see below)

public class FallbackCheckTask : IObjectConstructionTask
{
    public void Execute(ObjectConstructionArgs args)
    {
        if (args.Result == null)
        {
            var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;

            // if the item itself is null, regardless of version, abort
            if (scContext.Item == null)
            {
                args.AbortPipeline();
                return;
            }

            // we could be trying to convert rendering parameters to a glass model, and if so, just return.
            if (String.Compare(scContext.Item.Paths.FullPath, "[orphan]/renderingParameters", true) == 0)
            {
                return;
            }

            // the default glassmapper code would simply abort pipeline if the context items version count for the current langauge was 0
            // but this does not take item fallback into account
            // added here a check on the fallback extension method GetFallbackItem, recursively (for chained fallback)
            // and then if that fallback item is null or it's version count is 0 (and only then) would you go ahead and abort the pipeline
            if (scContext.Item.Versions.Count == 0)
            {
                var fallBackItem = CheckRecursivelyForFallbackItem(scContext.Item);
                if (fallBackItem == null)
                    args.AbortPipeline();
                else if (fallBackItem.Versions.Count == 0)
                    args.AbortPipeline();
                return;
            }
        }
    }

    // in the case of chained fallback, eg fr-CA -> en-CA -> en
    // could be that the middle languages don't have versions either, but DO have a fallback item
    // therefore, must check back further until either a version is found, or there are no more fallback items
    private Item CheckRecursivelyForFallbackItem(Item thisItem)
    {
        var fallBackItem = thisItem.GetFallbackItem();
        if (fallBackItem != null)
        {
            if (fallBackItem.Versions.Count == 0)
                fallBackItem = CheckRecursivelyForFallbackItem(fallBackItem);
        }
        return fallBackItem;
    }
}

Then you register (with Castle Windsor)

public static void CastleConfig(IWindsorContainer container){
            var config = new Config();

            container.Register(
               Component.For<IObjectConstructionTask>().ImplementedBy<FallbackCheckTask>().LifestylePerWebRequest()
              );
          //  config.EnableCaching = false;

            container.Install(new SitecoreInstaller(config));
        }

I am using Autofac and do not know how to perform the same action as above and assure it happens in the right order. I am registering my types the typical way (See below) but it doesn't seem to be hooking my FallbackCheckTask class.

 public static void Register()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(typeof(MvcApplication).Assembly);

        // register our types
        builder.RegisterType<FallbackCheckTask>().As<IObjectConstructionTask>().InstancePerLifetimeScope();

        // build and set the resolver
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

I also have the Language Item Fallback wired up and working as expected if glass is not involved in fetching the items values. I understand why Glass is not mapping the data out of the box, I just cant seem to get the fix working. Any thoughts?

EDIT 2015-05-21 19:00

I edited GlassMapperScCustom.cs as follows:

public static IDependencyResolver CreateResolver(){
        var config = new Glass.Mapper.Sc.Config();
        var resolver = new DependencyResolver(config);

        resolver.ObjectConstructionFactory.Add(() => new FallbackCheckTask());

        return resolver;
    }

And now its calling the Execute method of the the FallbackCheckTask only if there is a version of the item, if there is no version its not calling the method. Also, no matter what I do if I enable this Task my test query items always come back as NULL:

  var test = SitecoreContext.QuerySingle<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}");
            var test1 = SitecoreContext.Query<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}").ToList();
            var test2 = SitecoreContext.GetCurrentItem<Item>();
            var test3 = SitecoreContext.GetItem<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}");

So to sum it up, I am a little better off now then I was before but when the task class is registered queries come back as null for all items no matter if they have a version or not. As mentioned before, any help is appreciated.

Upvotes: 3

Views: 1574

Answers (1)

Michael Edwards
Michael Edwards

Reputation: 6528

I know you mentioned that you were using the VersionCountDisabler but does it look like this:

using(new VersionCountDisabler()){

        var test = SitecoreContext.QuerySingle<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}");
        var test1 = SitecoreContext.Query<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}").ToList();
        var test2 = SitecoreContext.GetCurrentItem<Item>();
        var test3 = SitecoreContext.GetItem<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}");

}

Or are you disabling it in some other manner?

I also notice that your fallback code doesn't seem to update the scContent.Item property. I think you need to update it to the following:

public class FallbackCheckTask : IObjectConstructionTask
{
public void Execute(ObjectConstructionArgs args)
{
    if (args.Result == null)
    {
        var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;

        // if the item itself is null, regardless of version, abort
        if (scContext.Item == null)
        {
            args.AbortPipeline();
            return;
        }

        // we could be trying to convert rendering parameters to a glass model, and if so, just return.
        if (String.Compare(scContext.Item.Paths.FullPath, "[orphan]/renderingParameters", true) == 0)
        {
            return;
        }

        // the default glassmapper code would simply abort pipeline if the context items version count for the current langauge was 0
        // but this does not take item fallback into account
        // added here a check on the fallback extension method GetFallbackItem, recursively (for chained fallback)
        // and then if that fallback item is null or it's version count is 0 (and only then) would you go ahead and abort the pipeline
        if (scContext.Item.Versions.Count == 0)
        {
            var fallBackItem = CheckRecursivelyForFallbackItem(scContext.Item);
            if (fallBackItem == null)
                args.AbortPipeline();
            else if (fallBackItem.Versions.Count == 0)
                args.AbortPipeline();

            //don't just return but update the scContext.Item to the fallback item
            scContext.Item = fallbackItem;
        }
    }
}

// in the case of chained fallback, eg fr-CA -> en-CA -> en
// could be that the middle languages don't have versions either, but DO have a fallback item
// therefore, must check back further until either a version is found, or there are no more fallback items
private Item CheckRecursivelyForFallbackItem(Item thisItem)
{
    var fallBackItem = thisItem.GetFallbackItem();
    if (fallBackItem != null)
    {
        if (fallBackItem.Versions.Count == 0)
            fallBackItem = CheckRecursivelyForFallbackItem(fallBackItem);
    }
    return fallBackItem;
}
}

Upvotes: 2

Related Questions