インポートパイプラインを拡張する
このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。
コードファーストワークフローの インポートパイプライン を拡張し、アプリケーションのデータのさまざまな側面をカスタマイズできます。
この手順では、インポートプロセスを拡張して作成したテンプレートのパスをカスタマイズする方法を示しています。
インポートパイプラインを拡張するには:
-
JSSマニフェストにコンポーネントを追加してマニフェストデータを拡張しましょう。
export 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プロセッサをターゲットにし、そこから派生し、その挙動を変更する必要があります。例えば:
using 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; } } }
-
今度はSitecoreにデフォルトのProcessTemplatesプロセッサではなくカスタムプロセッサProcessTemplatesWithPathを使うよう指示しなければなりません。例えばApp_Config/Include/PickAnyFileName.config設定パッチを作成し、以下の構成を追加してください。
-
JSSアプリをデプロイする:
-
もしこれが初めての展開であれば、ターミナルで以下のコマンドを実行してください:
jss deploy app -c
-
それ以外の場合は、以下のコマンドを実行します。
jss deploy items
!注スクリプトを実行すると、JSS自動的にファイルsitecore/manifest/sitecore-import.jsonにマニフェストが生成されます。
スクリプトは以下のようなメッセージを出力します。
JSS - Creating template - Welcome (Custom path: /sitecore/templates/Welcome)
-
-
Sitecoreのコンテンツツリーで、テンプレートがリクエストしたパスで作成されたか確認してください。
!注以前にJSSアプリをデプロイしていて、既存のテンプレート項目がデフォルトの場所にあった場合は、それを削除し、新しいテンプレートに使われているものを変更してください。