Orchard CMS迁移和分类

时间:2014-07-03 07:48:13

标签: asp.net-mvc orchardcms orchardcms-1.7

我正在创建一个需要附加分类的新部分。

在迁移中附加分类字段很容易:

ContentDefinitionManager.AlterPartDefinition("RecipePart", part => part
    .WithField("Categoria", cfg => cfg
        .OfType("TaxonomyField")
        .WithSetting("TaxonomyFieldSettings.AllowCustomTerms", "false")
        .WithSetting("TaxonomyFieldSettings.SingleChoice", "false")
        .WithSetting("TaxonomyFieldSettings.LeavesOnly", "true")
        .WithSetting("TaxonomyFieldSettings.Required", "true")
        .WithSetting("TaxonomyFieldSettings.Taxonomy", "Ricette Categorie"))
    .WithDescription("Categorie ricette."));

但是......我需要手动创建分类法和相关术语。

是否可以检查是否存在分类法(例如," Ricette Categorie"在此示例中),如果没有找到,则添加一个带有一些默认术语,以便部件可以在没有人工干预的情况下使用? 感谢

3 个答案:

答案 0 :(得分:3)

你正在寻找这样的东西吗?

var itemTypeTaxonomy = _taxonomyService.GetTaxonomyByName("ItemType");
if (itemTypeTaxonomy == null)
{
    itemTypeTaxonomy = _orchardServices.ContentManager.New("Taxonomy").As<TaxonomyPart>();
    itemTypeTaxonomy.Name = "ItemType";
    itemTypeTaxonomy.ContentItem.As<TitlePart>().Title = "ItemType";
    _taxonomyService.CreateTermContentType(itemTypeTaxonomy);
    _orchardServices.ContentManager.Create(itemTypeTaxonomy);
    _orchardServices.ContentManager.Publish(itemTypeTaxonomy.ContentItem);
}

ContentDefinitionManager.AlterPartDefinition("StandardItemPart", cfg => cfg
    .WithField("Description", field => field.OfType("HtmlField"))
    .WithField("Thumbnail", field => field.OfType("MediaPickerField"))
    .WithField("Length", field => field.OfType("TextField"))
    .WithField("Type", field => field.OfType("TaxonomyField").WithSetting("FieldIndexing.Included", "True")
                                                               .WithSetting("TaxonomyFieldSettings.TaxonomyId", itemTypeTaxonomy.Id.ToString())
                                                               .WithSetting("TaxonomyFieldSettings.LeavesOnly", "True")
                                                               .WithSetting("TaxonomyFieldSettings.SingleChoice", "True")));

当RoutePart仍然存在时,这适用于旧的分类法模块,现在已被AutoroutePart取代,但想法是

答案 1 :(得分:1)

谢谢哈扎指出我正确的方向......

我最终创建了这个创建分类标准的代码,并添加了条件(如果它们不存在):

    private void CreateTaxonomy(string taxonomyName, string[] terms)
    {
        var taxonomy = _taxonomyService.GetTaxonomyByName(taxonomyName);

        if (taxonomy == null)
        {
            taxonomy = _orchardServices.ContentManager.New("Taxonomy").As<TaxonomyPart>();
            taxonomy.Name = taxonomyName;
            taxonomy.ContentItem.As<TitlePart>().Title = taxonomyName;
            _taxonomyService.CreateTermContentType(taxonomy);
            _orchardServices.ContentManager.Create(taxonomy);
            _orchardServices.ContentManager.Publish(taxonomy.ContentItem);
        }

        foreach (var term in terms)
        {
            GetOrCreateTerm(taxonomy.Id, term);
        }
    }

GetOrCreateTerm是我在Orchard Source(TaxonomyFieldDriver.cs)上的原始方法之后创建的私有方法

答案 2 :(得分:0)

以下是创建分类法和术语的代码

public void Enabled(Feature feature) {
    if (_taxonomyService.GetTaxonomyByName("Genre") == null) {


        var tax = _contentManager.New<TaxonomyPart>("Taxonomy");
        tax.Name = "Genre";
        _contentManager.Create(tax, VersionOptions.Published);

        CreateTerm(tax, "Action");
        CreateTerm(tax, "Comedy");
        CreateTerm(tax, "Drama");
        CreateTerm(tax, "Panaorama");
        CreateTerm(tax, "Shikorama");
    }

}

private void CreateTerm(TaxonomyPart tax, string termName) {
    var term = _taxonomyService.NewTerm(tax);
    term.Name = termName;
    _contentManager.Create(term, VersionOptions.Published);
}

我将此代码添加到模块的FeatureEventHandler类中,以便在启用该功能时运行。