创建自定义Cloud Service creating-a-custom-cloud-service

默认Cloud Services集可以通过自定义Cloud Service类型进行扩展。 这样,您就可以采用一种结构化的方式将自定义标记注入页面中。 这主要适用于第三方分析提供商,例如Google Analytics、图表等。 Cloud Service会从父页面继承到子页面,并且能够在任何级别中断继承。

NOTE
此创建Cloud Service的分步指南是使用Google Analytics的示例。 并非所有情况都适用于您的用例。
  1. 在CRXDE Lite中,在 /apps

    • 名称acs
    • 类型nt:folder
  2. 在下创建节点 /apps/acs

    • 名称analytics
    • 类型sling:Folder
  3. 在下创建两个节点 /apps/acs/analytics

    • 名称:组件
    • 类型sling:Folder

    • 名称:模板
    • 类型sling:Folder
  4. 右键单击 /apps/acs/analytics/components. 选择 创建…… 后接 创建组件…… 打开的对话框允许您指定:

    • 标签googleanalyticspage
    • 标题Google Analytics Page
    • 超级类型cq/cloudserviceconfigs/components/configpage
    • .hidden
  5. 单击 下一个 两次,并指定:

    • 允许的父项: acs/analytics/templates/googleanalytics

    单击 下一个 两次,然后单击 确定.

  6. 添加属性到 googleanalyticspage

    • 名称: cq:defaultView
    • 值: html
  7. 创建名为的文件 content.jsp/apps/acs/analytics/components/googleanalyticspage,包含以下内容:

    code language-xml
    <%@page contentType="text/html"
                pageEncoding="utf-8"%><%
    %><%@include file="/libs/foundation/global.jsp"%><div>
    
    <div>
        <h3>Google Analytics Settings</h3>
        <ul>
            <li><div class="li-bullet"><strong>accountID: </strong><br><%= xssAPI.encodeForHTML(properties.get("accountID", "")) %></div></li>
        </ul>
    </div>
    
  8. 在下创建节点 /apps/acs/analytics/components/googleanalyticspage/

    • 名称dialog

    • 类型cq:Dialog

    • 属性

      • 名称title
      • 类型String
      • Google Analytics Config
      • 名称xtype
      • 类型String
      • dialog
  9. 在下创建节点 /apps/acs/analytics/components/googleanalyticspage/dialog

    • 名称items

    • 类型cq:Widget

    • 属性

      • 名称xtype
      • 类型String
      • tabpanel
  10. 在下创建节点 /apps/acs/analytics/components/googleanalyticspage/dialog/items

    • 名称items
    • 类型cq:WidgetCollection
  11. 在下创建节点 /apps/acs/analytics/components/googleanalyticspage/dialog/items/items

    • 名称:tab1

    • 类型cq:Panel

    • 属性

      • 名称title
      • 类型String
      • Config
  12. 在下创建节点 /apps/acs/analytics/components/googleanalyticspage/dialog/items/items/tab1

    • 名称:项目

    • 类型nt:unstructured

    • 属性

      • 名称fieldLabel

      • 类型:字符串

      • :帐户ID

      • 名称fieldDescription

      • 类型String

      • The account ID assigned by Google. Usually in the form UA-NNNNNN-N

      • 名称name

      • 类型String

      • ./accountID

      • 名称validateOnBlur

      • 类型String

      • true

      • 名称xtype

      • 类型String

      • textfield

  13. 复制 /libs/cq/cloudserviceconfigs/components/configpage/body.jsp/apps/acs/analytics/components/googleanalyticspage/body.jsp 和更改 libsapps 第34行并使第79行的脚本引用成为完全限定的路径。

  14. 在下创建模板 /apps/acs/analytics/templates/

    • 替换为 资源类型 = acs/analytics/components/googleanalyticspage
    • 替换为 标签 = googleanalytics
    • 替换为 标题= Google Analytics Configuration
    • 替换为 允许路径 = /etc/cloudservices/googleanalytics(/.*)?
    • 替换为 allowedChildren = /apps/acs/analytics/templates/googleanalytics
    • 替换为 sling:resourceSuperType = cq/cloudserviceconfigs/templates/configpage (在模板节点上,而不是jcr:content节点)
    • 替换为 cq:designPath = /etc/designs/cloudservices/googleanalytics (在jcr:content上)
  15. 创建组件: /apps/acs/analytics/components/googleanalytics.

    将以下内容添加到 googleanalytics.jsp

    code language-xml
    <%@page import="org.apache.sling.api.resource.Resource,
                    org.apache.sling.api.resource.ValueMap,
                    org.apache.sling.api.resource.ResourceUtil,
                    com.day.cq.wcm.webservicesupport.Configuration,
                    com.day.cq.wcm.webservicesupport.ConfigurationManager" %>
    <%@include file="/libs/foundation/global.jsp" %><%
    
    String[] services = pageProperties.getInherited("cq:cloudserviceconfigs", new String[]{});
    ConfigurationManager cfgMgr = resource.getResourceResolver().adaptTo(ConfigurationManager.class);
    if(cfgMgr != null) {
        String accountID = null;
        Configuration cfg = cfgMgr.getConfiguration("googleanalytics", services);
        if(cfg != null) {
            accountID = cfg.get("accountID", null);
        }
    
        if(accountID != null) {
        %>
    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', '<%= accountID %>']);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    
    </script><%
        }
    }
    %>
    

    这应该会根据配置属性输出自定义标记。

  16. 导航到 http://localhost:4502/miscadmin#/etc/cloudservices 并创建页面:

    • 标题Google Analytics
    • 名称googleanalytics

    返回CRXDE Lite,在 /etc/cloudservices/googleanalytics,添加以下属性到 jcr:content

    • 名称componentReference
    • 类型String
    • acs/analytics/components/googleanalytics
  17. 导航到新创建的服务页面( http://localhost:4502/etc/cloudservices/googleanalytics.html),然后单击 + 要创建配置,请执行以下操作:

    • 父配置/etc/cloudservices/googleanalytics
    • 标题: My First GA Config

    选择 Google Analytics配置 并单击 创建.

  18. 输入 帐户ID ​例如, AA-11111111-1. 单击​ 确定

  19. 导航到页面,并在页面属性中添加 Cloud Service 选项卡。

  20. 页面中将会添加自定义标记。

recommendation-more-help
19ffd973-7af2-44d0-84b5-d547b0dffee2