mikeedwards83 / Glass.Mapper

Version 5 of the Glass mapping framework, the best ORM for Sitecore.
Apache License 2.0
124 stars 122 forks source link

AB testing mvc.getModel pipeline GetFromItem processor #429

Open dominik-markowski opened 4 years ago

dominik-markowski commented 4 years ago

The issue is in Glass.Mapper.Sc.Pipelines.Response.GetModelFromView.GetDataSourceItem method. When component has a ab testing variant then: args.Rendering.DataSource is not a ID of valid datasource but a DataUri of rendering. I was trying to overwrite it but can get a valid datasource ID.

` protected override object GetDataSourceItem(GetModelArgs args, Type modelType) { var mvcContext = (IMvcContext) new MvcContext((ISitecoreService) new SitecoreService(Sitecore.Context.Database)); var rendering = args.Rendering; // rendering.DataSource is usually DataSource ID // BUT once variant is created for ab testing - rendering.DataSource is Data Uri to variant item version

        if (rendering.DataSource.HasValue())
        {
            var itemByPathOptions = new GetItemByPathOptions { Type = modelType };
            if (ID.IsID(rendering.DataSource))
            {
                itemByPathOptions.Path = rendering.DataSource;
            }
            else
            {
                var dataUri = DataUri.Parse(rendering.DataSource);

                if (rendering.Item.ID.Equals(dataUri.ItemID))
                {
                    // datasource ID == rendering item ID
                    itemByPathOptions.Path = "{7F1A6C0C-8FA8-4F24-ABF4-89882708CE0A}".ToLower();
                }
                else
                {
                    itemByPathOptions.Path = dataUri.Path == null || dataUri.Path.IsNullOrEmpty() 
                        ? dataUri.ItemID.ToString()
                        : dataUri.Path;
                }
                itemByPathOptions.Version = dataUri.Version;
                itemByPathOptions.Language = dataUri.Language;
            }

            return mvcContext.SitecoreService.GetItem((GetItemOptions) itemByPathOptions) ?? Activator.CreateInstance(modelType);
        }
        if (rendering.RenderingItem.DataSource.HasValue())
        {
            var itemByPathOptions = this.GetItemByPathOptions(rendering.RenderingItem.DataSource, modelType);
            return mvcContext.SitecoreService.GetItem((GetItemOptions) itemByPathOptions) ?? Activator.CreateInstance(modelType);
        }
        if (rendering.Item != null)
        {
            var itemByItemOptions = new GetItemByItemOptions
            {
                Type = modelType,
                Item = rendering.Item
            };
            return mvcContext.SitecoreService.GetItem((GetItemOptions) itemByItemOptions) ?? Activator.CreateInstance(modelType);
        }

        var itemByItemOptions1 = new GetItemByItemOptions
        {
            Type = modelType,
            Item = mvcContext.ContextItem
        };
        return mvcContext.SitecoreService.GetItem((GetItemOptions) itemByItemOptions1) ?? Activator.CreateInstance(modelType);
    }

    private GetItemByPathOptions GetItemByPathOptions(string path, Type modelType)
    {
        var itemByPathOptions = new GetItemByPathOptions { Type = modelType };
        // field can be a ID or DataUri string
        if (ID.IsID(path))
        {
            itemByPathOptions.Path = path;
        }
        else
        {
            var dataUri = DataUri.Parse(path);
            itemByPathOptions.Path = dataUri?.Path == null || dataUri.Path.IsNullOrEmpty() 
                ? dataUri?.ItemID.ToString()
                : dataUri.Path;
            itemByPathOptions.Version = dataUri?.Version;
            itemByPathOptions.Language = dataUri?.Language;
        }
        return itemByPathOptions;
    }

`

muso31 commented 4 years ago

Hi which version of glass are you using? The AB test issue was fixed in a recent version

dominik-markowski commented 4 years ago

Hi @muso31 I already updated to 5.8.170 and it fixed issues with controller renderings, but view rendering with are not working

dominik-markowski commented 4 years ago

And the project uses Sitecore 9.3

sarveshjain1 commented 3 years ago

I am facing a similar issue in Sitecore 9.3 and latest version of Glass. My view renderings are not working when I set it up for A/B testing.