In Episerver CMS, how to create a property to display the source URL of an uploaded image or PDF asset (in the Episerver editor interface)?

时间:2018-02-26 17:41:06

标签: c# asp.net-mvc asp.net-mvc-5 episerver episerver-10

Annoyingly, the MediaData class in Episerver doesn't have any basic properties like source URL, alt text, etc. I'm trying to implement a class to inherit from MediaData and provide specific properties for a certain type of media asset (PDF in this example).

I've tried manually setting the property values and also overriding the SetDefaultValues event, to no avail. Although, I do see either a textbox or a URL picker based on the type I use for "SrcUrl", however it is blank and never populates the uploaded PDF's URL.

[ContentType(
    DisplayName = "PDF File",
    GUID = "xxxxxxx-xxxx-xxxxxx-xxxx-xxxxxxxxxxx")]
[MediaDescriptor(ExtensionString = "pdf")]
public class PdfFile : MediaData
{
    [UIHint(UIHint.MediaFile)]
    [Display(Name = "PDF URL",
        Description = "Link to view or reference PDF",
        GroupName = SystemTabNames.Content,
        Order = 10)]
    public virtual string SrcUrl
    {
        get { return UrlResolver.Current.GetUrl(this.ContentLink); }
        set { value = UrlResolver.Current.GetUrl(this.ContentLink); }
    }

    // Sets the default property values
    public override void SetDefaultValues(ContentType contentType)
    {
        base.SetDefaultValues(contentType); 

        this.SrcUrl = UrlResolver.Current.GetUrl(this.ContentLink) ?? "Default";
    }
}

PDF URL property rendering blank in Episerver editor interface when viewing a PDF

****Disclaimer: I'm new to the Episerver CMS and may be missing something stupidly simple (ok with being shamed if appropriate).*

2 个答案:

答案 0 :(得分:2)

我不确定您需要完成什么,但应该注意的是,在编辑MediaData内容时,可以通过点击发布按钮获取其网址(请注意下载此文件链接):

Episerver publish button

编辑:另一个建议是为MediaFile UI提示创建自定义编辑器dijit(使用Dojo)。这样就可以显示URL而无需添加其他属性。

答案 1 :(得分:0)

我能够在Epi支持的帮助下解决这个问题,基本上我需要创建一个可初始化的模块 - 这样我就可以进入内容创建所需的生命周期事件。

[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class ModelDefaultValuesInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        contentEvents.CreatedContent += MediaBlocksDefaultValues;
    }

    private void MediaBlocksDefaultValues(object sender, ContentEventArgs e)
    {
        PopulateAssetURL(e);
    }

    /// <summary>
    /// Get the URL path of the uploaded asset and set it to the SrcUrl field which is easily visible to editors
    /// </summary>
    /// <param name="e"></param>
    private void PopulateAssetURL(ContentEventArgs e)
    {
        var mediaTypeBlock = e.Content as PdfFile;
        if (mediaTypeBlock != null)
        {
            string result = ServiceLocator.Current.GetInstance<UrlResolver>().GetUrl(mediaTypeBlock.ContentLink);
            if (!string.IsNullOrEmpty(result))
            {
                var srvcLoc = ServiceLocator.Current.GetInstance<IContentRepository>();
                var contentClone = mediaTypeBlock.CreateWritableClone() as PdfFile;
                contentClone.SrcUrl = result;
                srvcLoc.Save(contentClone, SaveAction.Publish, EPiServer.Security.AccessLevel.Administer);
            }
        }            
    }

    public void Uninitialize(InitializationEngine context)
    {
        var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        contentEvents.CreatedContent -= MediaBlocksDefaultValues;
    }
}
  • 注意:我发布上述内容是为了保持一致,但实际上我重构了代码以在名为BaseMediaData的类上设置SrcUrl属性,该类继承自MediaData类。这样,所有文件类型(powerpoint,pdfs等)都可以具有此属性,因为它与所有文件类型相关。图像继承自不同的基类(ImageData),因此我还必须创建一个名为ImageFile的新类,它继承自ImageData并应用相同的属性。我很懒,但是使用一个接口来标准化这两个类的实现可能是一个好主意。