Try our conversational search powered by Generative AI!

How to get the current page from the SelectionFactory that using in block

Vote:
 

I have a page type that store some options, and I want to use them in a property (SelectOne) of a block type. When create a block, that property will get options from parent page (get in SelectionFactory). I tried to get current page by IPageRouteHelper but always return the HomePage

        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            var currentContent = metadata.FindOwnerContent();
            var currentPage = currentContent;
            if (currentContent is BlockData)
            {
                currentPage = _pageRouteHelper.Service.Page;
            }
            return GetColorOptions(currentPage);
        }

Thanks for your help.

#221605
Apr 22, 2020 7:20
Vote:
 

Hi Ten Ten,

Quite simply we don't have this context in the selection factory, you're editing a block which has no direct relationship to a page. Is there a way you can rethink what you're trying to achieve?

The only "sort of" work-around I can this of is if you can ensure that the blocks are always created "For this page", then you could retrieve the page like this:

public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
    var content = metadata.FindOwnerContent();

    ContentAssetFolder assetFolder;

    if (!_contentLoader.Service.TryGet(content.ParentLink, out assetFolder))
    {
        // Return defaults here
        return new ISelectItem[0];
    }

    PageData page;

    if (!_contentLoader.Service.TryGet(assetFolder.ContentOwnerID, out page))
    {
        // Return defaults here
        return new ISelectItem[0];
    }

    return GetColorOptions(currentPage);
}

This feels kind of brittle though, so depends how important the integrity of the color selection is.

#221631
Apr 22, 2020 14:57
Ten Ten - Apr 23, 2020 2:13
Thank you, that's exactly what I need
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.