leekelleher / umbraco-contentment

Contentment for Umbraco - a state of happiness and satisfaction
https://marketplace.umbraco.com/package/umbraco.community.contentment
Mozilla Public License 2.0
157 stars 72 forks source link

contentmentContentContext.GetCurrentContent() unable to find currentContent #384

Closed woblet96 closed 7 months ago

woblet96 commented 8 months ago

Which Contentment version are you using?

4.6.0

Which Umbraco version are you using? For example: 10.3.2 - don't just write v10

13.1.1

Bug summary

We are trying to create a DataType (Race List) that takes a contextual Year value and returns a list of Races. We have created the DataType using the '[Contentment] Data List' With a Custom Data source following this guide Contentment for Umbraco

When calling _contentmentContentContext.GetCurrentContent(out bool isParent); we get a null response:

Steps to reproduce

Data List source code

public class FirstCyclingSeasonRaces : IDataListSource
{
    private readonly IContentmentContentContext _contentmentContentContext;
    public FirstCyclingSeasonRaces(IContentmentContentContext contentmentDataService)
    {
        _contentmentContentContext = contentmentDataService;
    }

    public Dictionary<string, object> DefaultValues => default;

    public IEnumerable<ConfigurationField> Fields => default;
    public string Group => "Custom";

    public OverlaySize OverlaySize => OverlaySize.Medium;

    public string Name => "Season Races";

    public string Description => "data source for first cycling races in a season";

    public string Icon => "icon-globe";

    public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config)
    {
        var currentContent = _contentmentContentContext.GetCurrentContent(out bool isParent);

        var season = currentContent?.GetProperty("season");

        var items = new List<DataListItem>();

        foreach (var item in new List<int> { 1,2,3})
        {
            items.Add(new DataListItem
            {
                Name = item.ToString(),
                Value = item.ToString()
            });
        }

        return items;
    }
}

When viewing the Teams page with breakpoints 'var currentContent' gets a null value

Race List Data Type image

Event Feed block image

Content Page image

Expected result / actual result

Expected result is the currentContent Actual result is Null

Do you have Umbraco ModelsBuilder enabled?

What browsers are you seeing the problem on?

Chrome

leekelleher commented 8 months ago

@woblet96 Thanks for taking the time to report this. It's a known issue of using Data List within the Block List editor, it's not possible to get the current page ID. (The same issue applies with Nested Content and the Block Grid editor too).

woblet96 commented 8 months ago

@leekelleher Hi, thanks for the fast response. We don't actually need the current page Id. We want to use another property on the element as we want to use the 'Year' property (currently "season" in the code) to control the Data source which is accessing an external Api.

So do you think it is possible to get that property?

leekelleher commented 8 months ago

So do you think it is possible to get that property?

@woblet96 Unfortunately not, it's a bit beyond the capabilities of the Data List editor. (Due to how the Block List data can be accessed programmatically, it's effectively blobs of JSON in property data).

If I was tackling this scenario, I'd be tempted to roll my own custom property editor that would watch the value of the season field (warning, it'd be ugly AngularJS traversing) and query an API controller with the value to get the items you need.

woblet96 commented 8 months ago

Thanks for the advice I ended up doing that in the end

leekelleher commented 7 months ago

Closing off this ticket, as unable to resolve it within Contentment/Data List.