是否可以从TBB动态设置元数据字段的值?或者是否可以从TBB动态添加模式中不一定存在的元数据字段?
我想这样做的原因是我正在使用DD4T,我想将面包屑自动添加到DD4T xml中.
我尝试过以下方法:
public override void Transform(Engine engine,Package package) { Initialize(engine,package); var page = GetPage(); string output = page.OrganizationalItem.Title; var parent = page.OrganizationalItem as StructureGroup; while (parent != null) { output = GetLinkToStructureGroupIndexPage(parent) + Separator + output; parent = parent.OrganizationalItem as StructureGroup; } // I tried this to dynamically add the field //var Metadata = page.Metadata.OwnerDocument.CreateElement("breadcrumb"); //Metadata.InnerText = output; //page.Metadata.AppendChild(Metadata); //I tried this to dynamically set an existing field on the schema foreach (XmlNode xml in page.Metadata) { Log.Debug("Metadata field:" +xml.Name); if(xml.Name == "breadcrumb") { xml.InnerText = output; } } package.PushItem(Package.PageName,package.CreateTridionItem(ContentType.Page,page)); }
但是,这些方法似乎都不起作用.这不可能吗?
解决方法
最简单的方法是创建一个实现DD4T.Templates.Base.BasePageTemplate的模板类.在该类中,您实现了TransformPage方法,该方法将DD4T页面作为其参数.您可以使用GetTcmPage()方法访问“TCM页面”.
例:
using TCM = Tridion.ContentManager.CommunicationManagement; using Dynamic = DD4T.ContentModel; public class MyTemplate : BasePageTemplate { protected override void TransformPage(Dynamic.Page page) { TCM.Page tcmPage = GetTcmPage(); string breadCrumbs = GetBreadCrumbs (tcmPage); // TODO: implement GetBreadCrumbs Field f = new Field(); f.Name = "breadCrumbs"; f.FieldType = FieldType.Text; f.Values.Add(breadCrumbs); page.MetadataFields.Add("breadCrumbs",f); } }