Kasturi Chavan
Kasturi Chavan

Reputation: 17

Sitecore Glass Mapper Infer Type Droptree Field with different template items

I have a template BrochuresComponent that has a field named Location which is a droptree. Now using source on the field in sitecore template, a user is only going to be able to add a country item or a continent item to this dropdown option. I want glass to map the country item in option to a ICountry glass Item and the continent Item to a Icontinent glass item.

But when user selects a option in dropdown,one of the glassItem value is filled while other has evaluation errors on it resulting in error on the model. Below is my code snippet.

My glass Interface looks as follows:

    using Collette.Library.GlassItems.Objects.Taxonomy.Locations;
    using   Collette.Library.GlassItemsConstants.Objects.Page_Components.Destination_Page_Components;
    using Glass.Mapper.Sc.Configuration;
    using Glass.Mapper.Sc.Configuration.Attributes;

    namespace Collette.Library.GlassItems.Objects.Page_Components.Destination_Page_Components
    {
     [SitecoreType(TemplateId = BrochureComponentsConstants.TemplateIdString, AutoMap = true)]
     public interface IBrochuresComponent: IGlassItemEx
     {
          //Brochures Data Section
          [SitecoreField(FieldType = SitecoreFieldType.DropTree, FieldId = BrochureComponentsConstants.LocationItemFieldId, Setting = SitecoreFieldSettings.InferType)]
          ICountry Country { get; set; }

          [SitecoreField(FieldType = SitecoreFieldType.DropTree, FieldId = BrochureComponentsConstants.LocationItemFieldId, Setting = SitecoreFieldSettings.InferType)]
          IContinent Continent { get; set; }

   }
}

My Model Looks as follows:

  var sitecoreContext = new SitecoreContext();
  BrochuresComponentItem = sitecoreContext.Cast<IBrochuresComponent>(DisplayItem); 
  //IF we specified a continent in dropdown it works fine since that is the first condition so it does not go to else,
  //but if we specify a country in the dropdown it breaks at the if condition below stating that templateid is empty string since nothing was evaluated.

Empty strings are not allowed. Parameter name: fieldName

  if (BrochuresComponentItem.Continent != null && BrochuresComponentItem.Continent.TemplateId.Equals(ContinentConstants.TemplateId))
   {
      SetBrochures(BrochuresComponentItem.Continent);
   }
   else if (BrochuresComponentItem.Country != null && BrochuresComponentItem.Country.TemplateId.Equals(CountryConstants.TemplateId))
   {
     SetBrochures(BrochuresComponentItem.Country);
   }

Upvotes: 0

Views: 2964

Answers (1)

jammykam
jammykam

Reputation: 16990

Your implementation/understanding of how infertype works is wrong, have a read of the Glass tutorial for Inferred Types.

In order for this to work correctly, your Country and Continent templates need to have a common base, and then you can use the infertype to map to the specific type:

 [SitecoreType(TemplateId = BrochureComponentsConstants.TemplateIdString, AutoMap = true)]
 public interface IBrochuresComponent: IGlassItemEx
 {
      //Brochures Data Section
      [SitecoreField(FieldType = SitecoreFieldType.DropTree, FieldId = BrochureComponentsConstants.LocationItemFieldId, Setting = SitecoreFieldSettings.InferType)]
      ILocationBase Location { get; set; }
}

Then on your code you can check the type it has been mapped to:

if (BrochuresComponentItem.Location != null)
{
    if (BrochuresComponentItem.Location is ICountry)
    {
        //do country specific thing
    }
    else if (BrochuresComponentItem.Location is IContinent)
    {
        // do continent specific thing
    }
}

Make sure the models for both ICountry and IContinent inherit from the common base interface to match the base data template, ILocationBase

Upvotes: 4

Related Questions