创建自定义Cloud Service creating-a-custom-cloud-service
默认的Cloud Services集可以使用自定义Cloud Service类型进行扩展。 这允许您以结构化方式将自定义标记注入页面。 这将主要用于第三方分析提供商,例如Google Analytics、Chartbeat等。 Cloud Services从父页面继承到子页面,能够在任何级别中断继承。
-
在CRXDE Lite中,在
/apps
:- 名称:
acs
- 类型:
nt:folder
- 名称:
-
在下创建新节点
/apps/acs
:- 名称:
analytics
- 类型:
sling:Folder
- 名称:
-
在下创建2个新节点
/apps/acs/analytics
:- 名称:组件
- 类型:
sling:Folder
和
- 名称:模板
- 类型:
sling:Folder
-
右键单击
/apps/acs/analytics/components
. 选择 创建…… 后跟 创建组件…… 打开的对话框允许您指定:- 标签:
googleanalyticspage
- 标题:
Google Analytics Page
- 超级类型:
cq/cloudserviceconfigs/components/configpage
- 组:
.hidden
- 标签:
-
单击 下一个 两次,指定:
- 允许的父项:
acs/analytics/templates/googleanalytics
单击 下一个 两次单击 确定.
- 允许的父项:
-
将资产添加到
googleanalyticspage
:- 名称:
cq:defaultView
- 值:
html
- 名称:
-
创建名为的新文件
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>
-
在下创建新节点
/apps/acs/analytics/components/googleanalyticspage/
:-
名称:
dialog
-
类型:
cq:Dialog
-
属性:
- 名称:
title
- 类型:
String
- 值:
Google Analytics Config
- 名称:
xtype
- 类型:
String
- 值:
dialog
- 名称:
-
-
在下创建新节点
/apps/acs/analytics/components/googleanalyticspage/dialog
:-
名称:
items
-
类型:
cq:Widget
-
属性:
- 名称:
xtype
- 类型:
String
- 值:
tabpanel
- 名称:
-
-
在下创建新节点
/apps/acs/analytics/components/googleanalyticspage/dialog/items
:- 名称:
items
- 类型:
cq:WidgetCollection
- 名称:
-
在下创建新节点
/apps/acs/analytics/components/googleanalyticspage/dialog/items/items
:-
名称:tab1
-
类型:
cq:Panel
-
属性:
- 名称:
title
- 类型:
String
- 值:
Config
- 名称:
-
-
在下创建新节点
/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
-
-
-
复制
/libs/cq/cloudserviceconfigs/components/configpage/body.jsp
to/apps/acs/analytics/components/googleanalyticspage/body.jsp
更改libs
toapps
在第34行,并使第79行上的脚本引用成为完全限定的路径。 -
在下创建新模板
/apps/acs/analytics/templates/
:- with 资源类型 =
acs/analytics/components/googleanalyticspage
- with 标签 =
googleanalytics
- with 标题=
Google Analytics Configuration
- with allowedPath =
/etc/cloudservices/googleanalytics(/.*)?
- with allowedChildren =
/apps/acs/analytics/templates/googleanalytics
- with sling:resourceSuperType =
cq/cloudserviceconfigs/templates/configpage
(在模板节点上,而不是jcr:content节点上) - with cq:designPath =
/etc/designs/cloudservices/googleanalytics
(在jcr:content上)
- with 资源类型 =
-
创建新组件:
/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><% } } %>
这应该根据配置属性输出自定义标记。
-
导航到
http://localhost:4502/miscadmin#/etc/cloudservices
并创建新页面:- 标题:
Google Analytics
- 名称:
googleanalytics
返回CRXDE Lite,然后返回
/etc/cloudservices/googleanalytics
,将以下资产添加到jcr:content
:- 名称:
componentReference
- 类型:
String
- 值:
acs/analytics/components/googleanalytics
- 标题:
-
导航到新创建的服务页面(
http://localhost:4502/etc/cloudservices/googleanalytics.html
),然后单击 + 要创建新配置,请执行以下操作:- 父配置:
/etc/cloudservices/googleanalytics
- 标题:
My First GA Config
选择 Google Analytics配置 单击 创建.
- 父配置:
-
输入 帐户ID,例如
AA-11111111-1
. 单击 确定。 -
导航到页面,并在页面属性中的 Cloud Services 选项卡。
-
页面将在其中添加自定义标记。