【问题标题】:sitecore programmatically change imagefield datasourcesitecore 以编程方式更改 imagefield 数据源
【发布时间】:2014-07-03 22:45:17
【问题描述】:

在内容或页面编辑器中创建项目时,我需要根据创建产品项目的文件夹路径更改产品项目的图像字段源。

如果我在/home/bicycles 下创建产品,我需要将产品项目图像字段自动更改为/sitecore/media library/Images/bicycles

如果我在/home/cars 下创建产品,我需要将产品项目图像字段自动更改为/sitecore/media library/Images/cars

如果我在/home/scooters 下创建产品,我需要将产品项目图像字段自动更改为/sitecore/media library/Images/scooters

数据模板中该图像字段源的默认设置是/sitecore/media library/Images/bicycles

我该怎么做?

【问题讨论】:

  • 看来我需要挂钩到 RenderField 管道并根据部件,更改图像字段的来源。我该怎么做呢?我是 Sitecore 新手 :)
  • 这是真正的业务需求吗?例如,如果 bicyclescarsscooters 是具有不同作者的不同部门,您可以通过使用 Sitecore 权限仅授予他们对媒体库某些部分的访问权限的方式来实现这一点。如果您真的需要编写代码,您可以创建一个扩展 Image 字段的自定义字段类型...然后您将有必要的挂钩来调整数据源。
  • 感谢您的建议。是业务需求但不是独立的部门,媒体库下大概有250个作者和400个左右的文件夹和子文件夹,如果图片字段可以直接打开媒体库下的相关文件夹路径就省了很多麻烦用于媒体上传。

标签: sitecore


【解决方案1】:

一个选项是创建一个自定义内容编辑器字段来扩展默认图像字段类型。

首先创建一个继承自Sitecore.Shell.Applications.ContentEditor.Image 的类。然后重写OnPreRender 方法,根据您的位置条件/要求来确定和设置图像字段的Source 属性。

有关详细信息,请参阅下面代码中的 cmets。

public class ContextAwareImageField : Sitecore.Shell.Applications.ContentEditor.Image
{
    /// <summary>
    /// The ItemID proprety is set by the Content Editor via reflection
    /// </summary> 
    public string ItemID { get; set; }

    /// <summary>
    /// Override the OnPreRender method. 
    /// The base OnPreRender method assigns a value to the Source viewstate property and we need to overwrite it.
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        Source = GetSource();
    }

    protected virtual string GetSource()
    {
        //retrieve and return the computed source value if it has already been set
        var contextSource = GetViewStateString("ContextSource");
        if (!string.IsNullOrWhiteSpace(contextSource))
            return contextSource;

        //retrieve the context item (the item containing the image field)
        var contextItem = Sitecore.Context.ContentDatabase.GetItem(ItemID);
        if (contextItem == null)
            return string.Empty;

        //determine the source to be used by the media browser
        //in this case we're just checking based on parent path, but any logic could be inserted
        contextSource = "/sitecore/media library/Images";
        switch (contextItem.Parent.Paths.FullPath.ToLowerInvariant())
        {
            case "/sitecore/content/home/bicycles":
                contextSource = "/sitecore/media library/Images/Bicycles";
                break;
            case "/sitecore/content/home/cars":
                contextSource = "/sitecore/media library/Images/Cars";
                break;
            case "/sitecore/content/home/scooters":
                contextSource = "/sitecore/media library/Images/Scooters";
                break;
        }

        //store the computed source value in view bag for later retrieval
        SetViewStateString("ContextSource", contextSource);

        //return the computed source value
        return contextSource;
    }
}

接下来,执行以下步骤:

  • 以管理员权限登录 Sitecore 桌面,使用右下角的数据库图标切换到 Core 数据库。

  • Core 数据库中,打开 内容编辑器,然后导航至 /sitecore/system/Field Types/Simple Types。在那里您会找到一个表示 Image 字段类型的项目。

  • 复制 Image 字段类型项并将复制的项重命名为相关内容(例如 Context Aware Image)。

  • 编辑重复项

    • Assembly字段中,提供包含您的自定义图像字段类的程序集文件的名称(例如MyClient.MySite.dll
    • Class字段中,提供自定义图像字段类的名称,包括命名空间(例如MyClient.MySite.CustomFields.ContextAwareImageField
  • 删除控制字段中的值

  • 保存更改

  • 切换回Master数据库。

  • 打开内容编辑器,然后导航到应包含新图像字段的模板。

  • 在模板中创建一个新字段,然后在 类型 下拉列表中选择您的新自定义字段类型。或者,更改现有图像字段的类型

  • 保存您的模板更改。

  • 在内容树中,导航到基于上述模板的项目,然后单击包含图像字段的浏览按钮。媒体浏览器对话框应默认为自定义字段中的逻辑指定的源位置。

    • 注意:如果您使用的 Sitecore 版本包含基于 SPEAK 的媒体浏览器对话框,则必须在对话框中切换到 树视图(上方的图标对)以查看您的自定义字段指定的源位置。

【讨论】:

  • 虽然这“解决”了问题,但它是一种可怕的方法。 Sitecore 具有多重继承的模板。您需要为此考虑面向对象。自行车、汽车和踏板车肯定具有相似的属性,但它们并不相同!因此,您可以创建一个涵盖这些的抽象基类车辆并从那里继承以添加您的特定属性。 Sitecore 的工作方式相同,但使用模板。如果你买一辆车,我给你一辆滑板车,你会高兴吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多