インポートパイプラインの拡張
このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。
コードファーストワークフロー のインポートパイプライン を拡張して、アプリケーションのデータのさまざまな側面をカスタマイズできます。
この手順では、インポート プロセスを拡張して、作成するテンプレートのパスをカスタマイズできるようにする方法を示します。
インポートパイプラインを拡張するには:
-
マニフェスト データを拡張するには 、コンポーネントをJSSマニフェストに追加します。
RequestResponseexport default manifest => { manifest.addComponent({ name: "Welcome", // we added this property to our welcome component path: "/sitecore/templates/Welcome", fields: [ { name: "title", type: manifest.fieldTypes.singleLineText }, ] }); };
-
カスタムパスを機能させるには、インポートパイプラインを拡張して、インポートプロセスによるテンプレートの作成方法を変更する必要があります。具体的には、importパイプラインでProcessTemplatesプロセッサをターゲットにし、そこから派生し、その動作を変更する必要があります。例えば:
RequestResponseusing System; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.JavaScriptServices.AppServices.Data; using Sitecore.JavaScriptServices.AppServices.Extensions; using Sitecore.JavaScriptServices.AppServices.Models; using Sitecore.JavaScriptServices.Configuration; namespace Sitecore.JavaScriptServices.AppServices.Pipelines.Import { public class ProcessTemplatesWithPath : ProcessTemplates { protected override TemplateItem CreateTemplate(TemplateDef templateDef, Item parent, Item multilistRoot, AppConfiguration app, IdManager idManager) { // the AdditionalData property is a dictionary of any JSON properties that were _not_ mapped to a TemplateDef property // so our path property - and any other custom properties that are added - will show up here. It will be null if no unmapped // properties exist so make sure to check everything for nulls with ? // // All import pipeline model objects have an AdditionalData property like this. var path = templateDef.AdditionalData?["path"]?.ToString(); // path not set, use default behavior if (path == null) return base.CreateTemplate(templateDef, parent, multilistRoot, app, idManager); // resolve the custom path's parent (to keep things simple, we're not creating any parent hierarchy) var parentItemPath = path.Substring(0, path.LastIndexOf("/")); var customParentItem = parent.Database.GetItem(parentItemPath, parent.Language); if (customParentItem == null) throw new InvalidOperationException($"Parent item for custom import path item {templateDef.Name} ({path}) did not exist."); var templateName = ItemUtil.ProposeValidItemName(templateDef.Name); // JSS imports use _consistent IDs_ so that imports across instances use the same Sitecore item GUIDs // This code generates the consistent ID and ensures that it is unique. var templateId = idManager.GetTemplateId(app.Name, templateDef); idManager.AssertIdIsUnused(templateId, $"template {templateDef.Name}"); // ensure we have permissions to create the item if (!customParentItem.CanImportChild(templateName, templateId, out var templateItem)) { return null; } if (templateItem == null) { Log.Info($"[JSS] - Creating template - {templateName} (Custom path: {path})", this); templateItem = parent.Database.Templates.CreateTemplate(templateName, customParentItem); // make the db see our new template parent.Database.Engines.TemplateEngine.Reset(); } else { Log.Debug($"[JSS] - Found existing template - {templateName}", this); } // sets the requisite fields on the template item UpdateTemplateItem(templateItem, templateDef, parent, multilistRoot, app, idManager, templateItem.DisplayName); return templateItem; } } }
-
ここで、デフォルトのProcessTemplatesプロセッサの代わりにカスタム プロセッサProcessTemplatesWithPathを使用するようにSitecoreに指示する必要があります。設定パッチ (App_Config/Include/PickAnyFileName.configなど) を作成し、次の設定を追加します。
RequestResponse<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/"> <sitecore role:require="ContentManagement or Standalone"> <pipelines> <group groupName="javaScriptServices"> <pipelines> <import> <processor type="Sitecore.JavaScriptServices.AppServices.Pipelines.Import.ProcessTemplatesWithPath, Sitecore.JavaScriptServices.AppServices" patch:instead="*[contains(@type, 'ProcessTemplates')]" /> </import> </pipelines> </group> </pipelines> </sitecore> </configuration>
-
JSSアプリをデプロイします。
-
アプリを初めてデプロイする場合は、ターミナルで次のコマンドを実行します。
RequestResponsejss deploy app -c
-
それ以外の場合は、次のコマンドを実行します。
RequestResponsejss deploy items
メモスクリプトの1つを実行すると、JSSによってマニフェストがファイルsitecore/manifest/sitecore-import.jsonに自動的に生成されます。
このスクリプトは、次のようなメッセージを出力します。
RequestResponse[JSS] - Creating template - Welcome (Custom path: /sitecore/templates/Welcome)
-
-
Sitecoreコンテンツ ツリーで、リクエストしたパスでテンプレートが作成されていることを確認します。
手記以前にJSSアプリをデプロイし、デフォルトの場所に既存のテンプレート項目がある場合は、それを削除し、その使用方法を新しいテンプレートに変更します。