Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A/B testing of components return "Data source invalid." #442

Open
izabelawlodarska opened this issue Nov 15, 2021 · 4 comments
Open

A/B testing of components return "Data source invalid." #442

izabelawlodarska opened this issue Nov 15, 2021 · 4 comments

Comments

@izabelawlodarska
Copy link

izabelawlodarska commented Nov 15, 2021

I am using Sitecore 9.3 with Glass Mapper 5.8.177 and component A/B testing does not work for me.

I set up a simple component that works perfectly fine until I create component A/B testing variants. After that, I am getting the "Data source is invalid." error for both variants. I saw this was an issue previously and there were some PR that was merged saying that this was fixed in the version I am using but it still does not seem to be working for me.

The problematic method is:

var datasource = MvcContext.GetDataSourceItem<T>();

which returns null.

After debugging it seems that the item is resolved correctly until the Glass.Mapper.Sc.Pipelines.ObjectConstruction.ItemVersionCountByRevisionTask, void Execute(ObjectConstructionArgs args) method is reached.

The condition below fails because the Revision field does not exist in the Item Fields

if (scContext != null 
    && options != null 
    && options.VersionCount 
    && scContext.Item != null 
    && scContext.Item.Fields[FieldIDs.Revision].GetValue(allowStandardValue: false).IsNullOrEmpty())
	{
		args.Result = null;
	}

The debugger shows that the scContext.Item.Fields do not contain the Revision field so it returns null. The only field which is visible in my item while debugging is "__Originator"-{F6D8A61C-2F84-4401-BD24-52D2068172BC}. There are also no custom fields mapped as well.

I tried setting A/B testing also for different components - the same issue for all of them. All of the components inherit from StandardTemplate and Revision field is present on the items when I check that from Sitecore. Also, all of the components are being displayed and are working fine as long as no A/B testing is used.

When I check MvcContext.DataSourceItem value after MvcContext.GetDataSourceItem(); it seems that the items were set correctly in the DataSourceItem, they are just unable to be mapped to a model.

Any ideas on what is happening here and how to resolve these issues would be more than welcome.

@mikeedwards83
Copy link
Owner

This is odd because the revision field is a standard field in Sitecore and should get populated when the item is saved.

What do you see in the raw values for the item?

@izabelawlodarska
Copy link
Author

izabelawlodarska commented Nov 30, 2021

Yes, I agree that the issue is odd especially since I tested multiple components and all of them inherit from the standard template and all the standard template fields are populated as expected but not in Glass.

In the raw values for the revision field I see "e07f9b7e-7b11-419d-8ae0-9fd28ee338f5".

Looks like it is not only our issue - https://sitecore.stackexchange.com/questions/30224/a-b-testing-of-components-using-glass-mapper-return-data-source-invalid but the fix does not fully work for me.

Below is the GlassBaseController code we are using with a partial fix for the issue form Stack Exchange, but this is only showing one of the component variants correctly. When I assign two variants to different datasources then only one datasource (always the same) is visible for both variants in Experience Editor. But it is possible to edit at least preview/edit one of them instead of none of them.

   protected T GetDataSource<T>(out bool isProtected) where T : class, IGlassBase
    {
        if (!this.MvcContext.HasDataSource)
        {
            throw new NoDatasourceException();
        }

        var datasource = this.MvcContext.GetDataSourceItem<T>();

        //Workaround for Glass Mapper issue with A/B testing - when component testing in use no datasource mapping
        if (datasource == null)
        {
            var options = CreateItemByIdOptions(
                this.MvcContext.DataSourceItem.ID.Guid,
                Sitecore.Context.Language,
                this.MvcContext.DataSourceItem.Version);

            datasource = this.MvcContext.SitecoreService.GetItem<T>(options);
        }
        if (datasource == null || !this.MvcContext.DataSourceItem.IsDerived(datasource.TemplateId))
        {
            throw new InvalidDatasourceException(); //this exception is being thrown when A/B testing in use because datasource is null
        }

        isProtected = this.IsProtected(this.MvcContext.DataSourceItem);

        return datasource;
    }

this.MvcContext.GetDataSourceItem(); returns null, however this.MvcContext.DataSourceItem is populated correctly.

I also noticed that when trying to CreateItemByIdOptions this.MvcContext.DataSourceItem.Language is incorrect (en) but the Sitecore.Context.Language is correct (nl-NL). So when I change the language like in the code sample above it is coming through. There is no item version in English but there is in Dutch.

@mikeedwards83
Copy link
Owner

I am assuming the model isn't cached

@mikeedwards83
Copy link
Owner

@izabelawlodarska could we setup a screensharing session?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants