19.x

getLayoutServiceContextパイプラインを拡張する

日本語翻訳に関する免責事項

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

レイアウトサービスが提供するgetLayoutServiceContextパイプラインを拡張することで、標準プロパティを有効化・無効化したり、自分のデータをcontextオブジェクトに追加したりできます。

getLayoutServiceContextパイプラインを拡張するには:

  1. Headless Servicesが提供するJssGetLayoutServiceContextProcessorベースを拡張してプロセッサを作成する:

    !注Headless Services以外でレイアウトサービスの利用者に拡張機能を提供したい場合は、Sitecore.LayoutService.ItemRendering.Pipelines.GetLayoutServiceContext.IGetLayoutServiceContextProcessorインターフェースを実装できます。

    public class ExampleContextExtension : Sitecore.JavaScriptServices.ViewEngine.LayoutService.Pipelines.GetLayoutServiceContext.JssGetLayoutServiceContextProcessor { public ExampleContextExtension(IConfigurationResolver configurationResolver) : base(configurationResolver) { }

    protected override void DoProcess(GetLayoutServiceContextArgs args, AppConfiguration application) { args.ContextData.Add("securityInfo", new { isAnonymous = !Sitecore.Context.User.IsAuthenticated }); } }

  2. プロセッサにパッチを当て設定するには、プロセッサ用の設定パッチを作成し、プロセッサが実行すべきアプリやルートを設定してください。以下の例で示されています。

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
     <sitecore>
     <pipelines>
     <group groupName="layoutService">
     <pipelines>
     <getLayoutServiceContext>
     <processor type="SitecoreJss.Examples.ExampleContextExtension, SitecoreJss.Examples" resolve="true">
     <AllowedConfigurations hint="list">
     <!-- Unless you change the Layout Service config used by your JSS app, this should always be present. -->
     <jss>jss</jss>
     </AllowedConfigurations>
     <Applications hint="list">
     <!-- Restrict the JSS apps for which this processor will execute. -->
     <reactApp>JssReactWeb</reactApp>
     </Applications>
     <Routes hint="list">
     <!-- Restrict the route items for which this processor will execute. IDs, item paths, and wildcard item paths are allowed. -->
     <services>/sitecore/content/JssReactWeb/Home/Services\*</services>
     <portfolio>{BAD2C001-1746-4312-8422-B28806A1191E}</portfolio>
     </Routes>
     </processor>
     </getLayoutServiceContext>
     </pipelines>
     </group>
     </pipelines>
     </sitecore>
    </configuration>
  3. 修正されたコンテキストの出力を確認するために、レイアウトサービスのエンドポイントを呼び出しhttp://{YOUR_SITECORE_HOST}/sitecore/api/layout/render/jss?item=/Services&sc_apikey={YOUR_API_KEY}:

    "context": { "securityInfo": { "isAnonymous": true }, "pageEditing": false, "site": { "name":"JssReactWeb" }, "navigation": { "name":"Home", "path":"/", "children": { "name":"About", "path":"/about" }, { "name":"Portfolio", "path":"/portfolio" }, { "name":"Services", "path":"/services" }

    }

    }

この記事を改善するための提案がある場合は、 お知らせください!