Goal
AEM Cloud Version : 2021.2.4944.20210221T230729Z-210225 (Feb 21, 2021)
Add support for Required in Assets Metadata Editor for fields using the Tags widget. Product out of the box does not support required attribute for Tag fields in the schema builder
Solution
1) Add a service user eaem-service-user in repo init script ui.config\src\main\content\jcr_root\apps\eaem-meta-tags-required\osgiconfig\config.author\org.apache.sling.jcr.repoinit.RepositoryInitializer-eaem.config
scripts=[
"
create service user eaem-service-user with path system/cq:services/experience-aem
set principal ACL for eaem-service-user
allow jcr:all on /conf
end
"
]
2) Provide the service user to bundle mapping in ui.config\src\main\content\jcr_root\apps\eaem-meta-tags-required\osgiconfig\config.author\org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended-ea.xml
3) Add a client library /apps/eaem-meta-tags-required/clientlibs/metadata-tags-required/tags-required-config with categories=dam.admin.ui.coral.schemaeditor.formbuilder.v2 to extend the Metadata Schema Form Builder and add configuration for Tags Required
(function($, $document) {
var TAG_FIELD_RES_TYPE = "cq/gui/components/coral/common/form/tagfield",
RULES_PANEL = "#field-rules",
REQUIRED_CASCADING = "/granite:data/requiredCascading",
F_CONTENT_PATH = "foundation-content-path",
REQUIRED_CHECKBOX_CSS = "eaem-dam-required";
$document.on("foundation-contentloaded", init);
function init(){
$document.on("click", ".form-fields > li", function(e) {
e.stopPropagation();
e.preventDefault();
addTagsRequiredConfig(this);
});
}
function addTagsRequiredConfig(field){
var $tagsCheck = $(field).find("[value='" + TAG_FIELD_RES_TYPE + "']"),
$rulesPanel = $(RULES_PANEL);
if(_.isEmpty($tagsCheck) || !_.isEmpty($rulesPanel.find("." + REQUIRED_CHECKBOX_CSS))){
return;
}
var $tagsReadonlyConfig = $(field).find("coral-checkbox[name$='/readOnly']");
if(_.isEmpty($tagsReadonlyConfig)){
return;
}
var configName = $tagsReadonlyConfig.attr("name"),
reqConfigName = configName.substring(0, configName.lastIndexOf("/")) + REQUIRED_CASCADING,
nodeName = configName.substring(0, configName.lastIndexOf("/"));
$tagsReadonlyConfig = $rulesPanel.find("coral-checkbox[name='" + configName + "']");
nodeName = nodeName.substring(nodeName.lastIndexOf("/") + 1);
$(getRequiredCheckbox(reqConfigName, isRequiredSet(nodeName))).insertAfter($tagsReadonlyConfig);
}
function isRequiredSet(nodeName){
var schemaPath = $("." + F_CONTENT_PATH).data(F_CONTENT_PATH),
isRequired = false;
if(!schemaPath){
return isRequired;
}
schemaPath = "/bin/querybuilder.json?p.hits=full&p.nodedepth=2&path=" + schemaPath + "&nodename=" + nodeName;
$.ajax({url : schemaPath, async: false}).done(function(data){
if(!data || _.isEmpty(data.hits)){
return;
}
isRequired = (data.hits[0]["granite:data"]["requiredCascading"] == "always");
});
return isRequired;
}
function getRequiredCheckbox(configName, checked){
return 'Required'
+ '';
}
}(jQuery, jQuery(document)));