Skip to content

1.8之前添加自定义设置-Add Custom Settings pre 1.8

ARCHIVED

!注意“归档”

This chapter has not been updated for the current version of Orchard, and has been ARCHIVED.

Adding Custom Settings pre Orchard 1.8

在Orchard 1.8之前添加自定义设置

This document traces the process of defining and implementing and individual site setting for a live orchard module that can be added to your site to enable the webservice known as 'AddThis' Content Sharing

本文档描述了为实时果园模块定义和实施以及单个站点设置的过程,该模块可以添加到您的站点以启用称为“AddThis”的Web服务[Content Sharing](http://orchardsharing.codeplex.com/)

The specific goal being "to store my AddThis service login credentials so that all share bars across the site will be able to access my account"

具体目标是“存储我的AddThis服务登录凭据,以便整个网站上的所有共享栏都能够访问我的帐户”

The first thing to understand is that our site setting is really just another form of the basic Orchard building blocks - the ContentPart/ContentPartRecord. While most ContentParts render visual elements to the site's visitor, they can also deliver non-visible chunks of data that can provide dynamic functionality to your pages. A great introduction to the construction of content types can be found

首先要了解的是,我们的网站设置实际上只是基本Orchard构建块的另一种形式 - ContentPart / ContentPartRecord。虽然大多数ContentParts都会向网站的访问者呈现可视元素,但他们还可以提供不可见的数据块,这些数据可以为您的网页提供动态功能。可以找到对内容类型构造的精彩介绍

found here):

[在此处找到](http://www.szmyd.com.pl/blog/jumpstart-into-orchard-module-development)):

public class ShareBarSettingsPart : ContentPart<ShareBarSettingsPartRecord> {

    public string AddThisAccount {

        get { return Record.AddThisAccount; }

        set { Record.AddThisAccount = value; }

        }

    }

When you create a Content Type you will almost always find yourself also creating a ContentTypeRecord which can be thought of as creating a new field in the database for storing the ContentType values.

创建内容类型时,您几乎总会发现自己也创建了一个ContentTypeRecord,可以将其视为在数据库中创建一个用于存储ContentType值的新字段。

public class ShareBarSettingsPartRecord : ContentPartRecord {

    public virtual string AddThisAccount { get; set; }

}

You can also decorate the content part AddThisAccount property with [Required] attribute

您还可以使用[Required]属性修饰内容部分AddThisAccount属性

to make it a required field when editing site settings.

在编辑网站设置时使其成为必填字段。

After creating the content part and record classes, you need to create appropriate

创建内容部分和记录类后,您需要创建适当的

database mappings, called in Orchard - Data Migration.

数据库映射,在Orchard中调用 - 数据迁移

You shouldn't do it by hand: there is a command-line,

你不应该手工完成:有一个命令行,

codegen datamigration <feature_name>, which will create the appropriate file for you.

You can see how to use it here.

你可以看到如何使用它[这里](使用命令行界面)。

The next step is to create a corresponding driver, which will be responsible for displaying the editor that the end-user invokes when setting the posted values.

下一步是创建一个相应的驱动程序,该驱动程序将负责显示最终用户在设置发布值时调用的编辑器。

If you have already written some content parts, than this part of code should look familiar:

如果您已经编写了一些内容部分,那么这部分代码应该看起来很熟悉:

[UsedImplicitly]

public class ShareBarSettingsPartDriver :

    ContentPartDriver<ShareBarSettingsPart> {

    public ShareBarSettingsPartDriver(

        INotifier notifier,

        IOrchardServices services) {

            _notifier = notifier;

            T = NullLocalizer.Instance;

       }



    public Localizer T { get; set; }

    private const string TemplateName = "Parts/Share.Settings";

    private readonly INotifier _notifier;



    protected override DriverResult Editor(

        ShareBarSettingsPart part, dynamic shapeHelper) {

        return ContentShape("Parts_Share_Settings",

                   () => shapeHelper.EditorTemplate(

                       TemplateName: TemplateName,

                       Model: part,

                       Prefix: Prefix));

       }



    protected override DriverResult Editor(

        ShareBarSettingsPart part, IUpdateModel updater, dynamic shapeHelper) {

        if (updater.TryUpdateModel(part, Prefix, null, null)) {

            _notifier.Information(

                T("Content sharing settings updated successfully"));

        }

        else {

            _notifier.Error(

                T("Error during content sharing settings update!"));

        }

        return Editor(part, shapeHelper);

    }

}

I omitted some code for checking permissions to edit the settings for better readability,

我省略了一些代码来检查编辑设置的权限以获得更好的可读性,

but you are free to take a look at the full source code hosted on GitHub.

但您可以自由地查看[GitHub上托管的完整源代码](https://github.com/OrchardCMS/Orchard)。

To review, so far we have our Content Part (plus it's attendant Content Part Record) and this just added driver. Two more structures are required before we can implement our new site-wide property setting:

要查看,到目前为止,我们有内容部分(加上它的随附内容部分记录),这只是添加了驱动程序。在我们实施新的站点范围的属性设置之前,还需要两个结构:

a Handler (controller) where we define the behavior of our Content Part, and the Shape (view)

一个处理程序(控制器),我们定义内容部分的行为,以及形状(视图)

that will render the HTML markup for our form's editor. The Handler looks like:

这将为我们表单的编辑器呈现HTML标记。处理程序看起来像:

[UsedImplicitly]

public class ShareBarSettingsPartHandler : ContentHandler {

    public ShareBarSettingsPartHandler(

        IRepository<ShareBarSettingsPartRecord> repository) {

        Filters.Add(new ActivatingFilter<ShareBarSettingsPart>("Site"));

        Filters.Add(StorageFilter.For(repository));

    }

}

In most cases its just that simple:

在大多数情况下,它就是这么简单:

  1. Add an activating filter. Tells Orchard which of the existing Content Types

1.添加激活过滤器。告诉Orchard现有的内容类型

our ShareBarSettingsPart should be attached to. Because Site is also a Content Type,

我们的 ShareBarSettingsPart 应附加到。因为 Site 也是一种内容类型,

we can attach our part to it. **Basically, this is the point that differentiates the ordinary

我们可以参与其中。 **基本上,这是区分普通的点

content parts from site settings.**

来自网站设置的内容部分。**

  1. Add the storage filter to register our settings repository - required because we want to be storing records in the database.

2.添加存储过滤器以注册我们的设置存储库 - 这是必需的,因为我们希望将记录存储在数据库中。

So if the above handler can be thought of as a 'controller' the obvious next step is creating the 'view'. Orchard's term is 'shape' and is nothing more than a .cshtml file that combines HTML markup with razor's ability to render database elements.

因此,如果上述处理程序可以被视为“控制器”,那么显而易见的下一步就是创建“视图”。 Orchard的术语是“形状”,只不过是一个.cshtml文件,它结合了HTML标记和razor渲染数据库元素的能力。

First, you have to create a .cshtml file under /Views/EditorTemplates/Parts/.

首先,您必须在/ Views / EditorTemplates / Parts /下创建一个.cshtml文件。

This file, as the naming convention

此文件,作为[命名约定](访问和渲染形状)

informs us, should be named Share.Settings.cshtml.

通知我们,应该命名为Share.Settings.cshtml

This name corresponds to the Parts_Share_Settings shape used inside the driver above.

此名称对应于上面驱动程序中使用的Parts_Share_Settings形状。

We've got only a single field (AddThisAccount) in our settings, so the markup inside

我们的设置中只有一个字段(AddThisAccount),因此内部标记

the Share.Settings.cshtml file will look like this:

Share.Settings.cshtml文件如下所示:

@model Szmyd.Orchard.Modules.Sharing.Models.ShareBarSettingsPart

<fieldset>

    <legend>@T("Content sharing settigs")</legend>

    <div>

        @Html.LabelFor(m => m.AddThisAccount, @T("AddThis service account"))

        @Html.EditorFor(m => m.AddThisAccount)

        @Html.ValidationMessageFor(m => m.AddThisAccount, "*")

    </div>

</fieldset>

Next, we need to tell Orchard where in the Site -> Settings pane our form should be displayed.

接下来,我们需要告诉Orchard在“站点 - >设置”窗格中应该显示哪个表单。

To do this we create a Placement.info file in our module root:

为此,我们在模块根目录中创建一个Placement.info文件:

<Placement>

    <Place Parts_Share_Settings="Content:0"/>

</Placement>

which tells Orchard to display our form field at the beginning.

告诉Orchard在开头显示我们的表单字段。

Now that we've configured our settings we will look at what it takes to actually use them.

现在我们已经配置了设置,我们将看看实际使用它们需要什么。

Using site scope settings

使用站点范围设置

This part is basically a one-liner:

这部分基本上是一个单行:

var shareSettings = _services.WorkContext.CurrentSite.As<ShareBarSettingsPart>();

Where _services is the IOrchardServices object (eg. injected in the constructor).

其中_services是IOrchardServices对象(例如,在构造函数中注入)。

Please note that you have to include "using Orchard.ContentManagement;" on the class.

请注意,您必须包含“使用Orchard.ContentManagement;”在课堂上。

Simple, isn't it? The full (simplified for readability) example from the ShareBarDriver

简单,不是吗? ShareBarDriver中的完整(简化了可读性)示例

from Content Sharing module:

来自[内容共享](http://orchardsharing.codeplex.com/)模块:

[UsedImplicitly]

public class ShareBarPartDriver : ContentPartDriver<ShareBarPart> {

    private readonly IOrchardServices _services;



    public ShareBarPartDriver(IOrchardServices services) {

        _services = services;

    }



    protected override DriverResult Display(

        ShareBarPart part, string displayType, dynamic shapeHelper) {

        var shareSettings = _services.WorkContext.CurrentSite

            .As<ShareBarSettingsPart>();



        // Prevent share bar from showing if account is not set

        if (shareSettings == null ||

            string.IsNullOrWhiteSpace(shareSettings.AddThisAccount)) {

            return null;

        }



        return ContentShape("Parts_Share_ShareBar",

            () => shapeHelper.Parts_Share_ShareBar(

                Account: shareSettings.AddThisAccount));

    }

}