【问题标题】:Find Parent Block properties from child block in episerver从 Episerver 中的子块中查找父块属性
【发布时间】:2021-06-03 05:04:18
【问题描述】:

episerver 中有没有办法找到块层次结构。 我的结构是 Carousal Block 包括 Carousal Items 的 ContentArea 属性 狂欢项目1 细绳 选厂 狂欢项目2 细绳 选厂 在 Carousal Item 1 中,我有一个选择工厂。我需要在这个选择工厂中获取 Carousal Block 属性。 任何帮助表示赞赏。 提前致谢。

【问题讨论】:

    标签: episerver


    【解决方案1】:

    简短的回答,不,没有办法,因为块不会以这种方式继承结构。它们可以在任何地方使用。因此,如果您请求块的父级,它将始终返回 SysContentAssetFolder

    为了解决您的问题(我敢肯定还有其他方法),我会选择一种使用方法。例如。块在哪里使用并找到所有者,块实例将父母称为所有者,因为它是一个可以有多个父母/所有者的实例。

    现在我们知道这是一些代码

    public class ImageBlockController : BlockController<ImageBlock>
    {
        private readonly IContentRepository contentRepository;
        private readonly IContentTypeRepository contentTypeRepository;
    
        public ImageBlockController(IContentRepository contentRepository, IContentTypeRepository contentTypeRepository)
        {
            this.contentRepository = contentRepository;
            this.contentTypeRepository = contentTypeRepository;
        }
    
        public override ActionResult Index(ImageBlock currentBlock)
        {
            var blockContent = (IContent)currentBlock;
    
            // Get block references
            var usageReferences = contentRepository.GetReferencesToContent(blockContent.ContentLink, false);
    
            if(usageReferences.Count() == 1)
            {
                // easy peasy, only one usage
                var owner = contentRepository.Get<IContent>(usageReferences.First().OwnerID);
    
                // example
                var ownerType = contentTypeRepository.Load(owner.ContentTypeID);
                if (ownerType.ModelType == typeof(ImageBlock))
                {
                    // do stuff
                }
            }
            else
            {
                // handle if the block is used at multiple locations
                // ...
            }
    
            return PartialView(currentBlock);
        }
    }
    

    本例中的技巧是获取所有对已加载块的引用contentRepository.GetReferencesToContent,如果我们对结果感到满意,请执行某些操作并继续。如果您有多个usageReferences,您可能必须在加载的页面中搜索正确的所有者块。在这种情况下,加载IPageRouterHelper,使用var parentPage = contentRepository.Get&lt;IContent&gt;(pageRouteHelper.Page.ContentLink); 之类的内容获取当前页面,并将usageReferences 与内容区域中的项目匹配。

    【讨论】:

    • 感谢埃里克的回复。我使用了 GetReferencesToContent ,它工作正常,但没有引用时它不会。例如,我正在创建一个技术上尚未发布的新 Carousal 项目。在这种情况下,我需要先发布该项目,然后我们才能使用它 GetReferencesToContent .its 那种在旅途中的场景。
    • @user15532895 当然,这将是一个限制,但你必须忍受它。请问为什么不直接在 Carousal Block 上实现选择工厂(一个枚举)来跳过 Carousal 项?
    • 我理解它很奇怪,但它就像父块中的基本位置列表和从子块中的位置中选择的个人项目。
    • @user15532895 当然,我明白了,但在这种情况下,策略必须是自上而下而不是自下而上。而是从 ContentAreas 中找到您需要的结构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多