【问题标题】:Orchard CMS Migrations and TaxonomiesOrchard CMS 迁移和分类法
【发布时间】:2014-07-03 07:48:12
【问题描述】:

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

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

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”),如果未找到,请添加一个带有一些默认术语的分类法,以便该部分无需人工干预即可使用? 谢谢

【问题讨论】:

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


    【解决方案1】:

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

    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 取代,但想法就在那里

    【讨论】:

      【解决方案2】:

      感谢 Hazza 为我指明了正确的方向...

      我最终得到了创建分类并添加术语(如果它们不存在)的代码:

          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) 上的原始方法之后创建的私有方法

      【讨论】:

        【解决方案3】:

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

        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 类中,以便在启用该功能时运行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多