在代码中将字段添加到自定义部件的正确方法是什么? [英] What is the proper way to add a Field to a custom Part in code?

查看:41
本文介绍了在代码中将字段添加到自定义部件的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个类似的问题可以解决这个问题,比如 这个这个提供了一个非常hacky的解决方案.没有一个人有明确的令人满意的答案,或者根本没有答案,或者一开始就问完全相同的事情.

There are several similar questions that sort of deal with this issue like this one or this one offering a pretty hacky solution. None of the ones out there have a clear satisfactory answer, or an answer at all, or are asking quite the same thing to begin with.

记录

public class MyPartRecord : ContentPartRecord
{
    public virtual Boolean Property1 { get; set; }
    public virtual string Property2 { get; set; }
}

部分

public class MyPart : ContentPart<MyPartRecord>
{
    public Boolean Property1
    {
        get { return Record.Property1; }
        set { Record.Property1 = value; }
    }

    public string...
}

迁移(由 codegen 生成)

SchemaBuilder.CreateTable("MyPartRecord", table => table
            .ContentPartRecord()
            .Column("Property1", DbType.Boolean)
            .Column("Property2", DbType.String)
);

ContentDefinitionManager.AlterPartDefinition("MyPart", part => part
    .Attachable()
);

编辑器模板

@model Project.Module.Models.MyPart

<fieldset>
<legend>MyPart</legend>

<!-- Property1 -->
@Html.EditorFor(m => m.Property1)
@Html.LabelFor(m => m.Property1)

...

</fieldset>

这一切都取自关于编写内容部分的官方文档,并且工作正常.但是,我希望我的自定义部件也有一个 MediaLibraryPickerField.通过迁移添加一个很容易:

This is all taken from the official documentation on writing Content Parts and works fine. However, I want my custom Part to also have a MediaLibraryPickerField. Adding one through a migration is easy enough:

ContentDefinitionManager.AlterPartDefinition("MyPart", part => part
    .WithField("Image", field => field
        .OfType("MediaLibraryPickerField")
        .WithDisplayName("Image")
        .WithSetting("MediaLibraryFieldSettings.Required", "False")
    )
);

但是我在使用这种方法时遇到了几个问题.

But there are several problems I bump into using this approach.

1) 我无法在我的模板中呈现该字段,只能使用放置使其显示在模板其余部分的上方或下方,因此我无法将其与其所属的属性分组.

1) I can't render the field in my template, only use placement to have it show up somewhere above or below the rest of the template, so I can't group it with the properties that it belongs to.

2) 由于它附加到 MyPart 而不是 MyPart 附加到的类型的 ContentPart,管理员无法通过 GUI 调整其设置,只能将其删除(这是错误还是尚未确定的功能)实施了吗?).

2) Since it's attached to MyPart and not the ContentPart of the Type that MyPart gets attached to, admins can't adjust its settings through the GUI, only remove it (is this a bug or a feature that has yet to be implemented?).

3) 我不确定如何在代码中访问 MediaLibraryField,因为 ContentItem.MyPart 现在返回一个 MyPart 对象,所以 ContentItem.MyPart.Image.FirstMediaUrl 没有更长的工作.

3) I'm unsure how to access the MediaLibraryField in code, since ContentItem.MyPart returns a MyPart object now, so ContentItem.MyPart.Image.FirstMediaUrl no longer works.

我如何解决这些问题?我在这里遗漏了一些明显的东西吗?有没有办法将媒体作为属性添加到我的模型中,而不是使用 Field 并且仍然让 Orchard 保留它?我真的很想避免修改我的 HTML 并将代码从官方实现复制到我的自定义视图中.

How do I get around these issues? Am I missing something obvious here? Is there perhaps a way to add Media as a property to my model instead of using a Field and still have Orchard persist it? I would really like to avoid modifying my HTML and copying code from the official implementation to my custom views.

推荐答案

1) 使用placement.info 和alternates 自定义要呈现字段的位置

1) Use placement.info and alternates to customize where you want to render the field

2) 您应该可以在 Dashboard -> Content Definition -> YourContentType -> Parts -> MyPart 设置下调整设置.

2) You should be able to adjust the settings in Dashboard -> Content Definition -> YourContentType -> Parts -> under the MyPart settings.

您也可以将字段附加到类型上(注意:它并不是真正附加到类型,而是附加到与类型同名的部分):

You could also attach the field to the type instead (note: it isn't really attached to the type, but to the part with the same name as the type):

迁移.cs:

ContentDefinitionManager.AlterPartDefinition("MyType", part => part
    .WithField("Image", field => field
        .OfType("MediaLibraryPickerField")
        .WithDisplayName("Image")
        .WithSetting("MediaLibraryFieldSettings.Required", "False")
    )
);

ContentDefinitionManager.AlterTypeDefinition("MyType", type => type
    .WithPart("MyType");

3) 您可以使用动态符号,也可以搜索字段:

3) You can either use the dynamic notation, or search the field:

// cast to dynamic
var url = ((dynamic)ContentItem).MyPart.Image.FirstMediaUrl

// or search for the field
var field = ContentItem.MyPart.Fields.OfType<MediaLibraryPickerField>().Single(f => f.Name == "Image");
// or ContentItem.MyPart.Fields.Single(f => f.Name == "Image") as MediaLibraryPickerField;
var url = field.FirstMediaUrl;

这篇关于在代码中将字段添加到自定义部件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆