インポートパイプラインの拡張

Version: 22.x
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

コードファーストワークフロー のインポートパイプライン を拡張して、アプリケーションのデータのさまざまな側面をカスタマイズできます。

この手順では、インポート プロセスを拡張して、作成するテンプレートのパスをカスタマイズできるようにする方法を示します。

インポートパイプラインを拡張するには:

  1. マニフェスト データを拡張するには 、コンポーネントをJSSマニフェストに追加します

    RequestResponse
    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 },
        ]
      });
    };
  2. カスタムパスを機能させるには、インポートパイプラインを拡張して、インポートプロセスによるテンプレートの作成方法を変更する必要があります。具体的には、importパイプラインでProcessTemplatesプロセッサをターゲットにし、そこから派生し、その動作を変更する必要があります。例えば:

    RequestResponse
    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;
            }
        }
    }
  3. ここで、デフォルトの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>
  4. JSSアプリをデプロイします。

    • アプリを初めてデプロイする場合は、ターミナルで次のコマンドを実行します。

      RequestResponse
      jss deploy app -c
    • それ以外の場合は、次のコマンドを実行します。

      RequestResponse
      jss deploy items
    メモ

    スクリプトの1つを実行すると、JSSによってマニフェストがファイルsitecore/manifest/sitecore-import.jsonに自動的に生成されます

    このスクリプトは、次のようなメッセージを出力します。

    RequestResponse
    [JSS] - Creating template - Welcome (Custom path: /sitecore/templates/Welcome)
  5. Sitecoreコンテンツ ツリーで、リクエストしたパスでテンプレートが作成されていることを確認します。

    手記

    以前にJSSアプリをデプロイし、デフォルトの場所に既存のテンプレート項目がある場合は、それを削除し、その使用方法を新しいテンプレートに変更します。

何かフィードバックはありますか?

この記事を改善するための提案がある場合は、