[opencms-dev] Schema Default - advanced logic

Thomas Schmidt t.schmidt at md-network.de
Tue Feb 16 00:37:42 CET 2021


Hello Michael,

thanks a lot for your response. I learned again a lot.

I have now overwritten „getDefault“ in an extended content handler of CmsDefaultXmlContentHandler. I left the method more or less as in the original, I just added an own macro to CmsMacroResolver:


-.-.-.-.-.-. schnipp -.-.-.-.-.
	if (!m_nonMacroResolvableDefaults.contains(path)) {
		CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(newCms).setMessages(getMessages(locale));
                resolver.addMacro(KEY_CURRENT_USER_FIRSTNAME_INITIAL, newCms.getRequestContext().getCurrentUser().getFirstname().substring(0,1)); // << added to original source
                resolver.addMacro(KEY_CURRENT_USER_LASTNAME_INITIAL, newCms.getRequestContext().getCurrentUser().getLastname().substring(0,1)); // << added to original source
		result = resolver.resolveMacros(defaultValue);
	}
-.-.-.-.-.-. schnapp -.-.-.-.-.


I’m not sure this is the right or best way to add new macros - but it works. 

Thanks a lot
Kind Regards
ThomasS



> Am 15.02.2021 um 14:16 schrieb Michael Emmerich <m.emmerich at alkacon.com>:
> 
> Thomas,
> 
> you cannot use anything except %(value:XPATH) for this kind of macro, and its usage is limited to certain use cases like setting the value of the GalleryTitle in the Mercury template.
> 
> Of course, you can use all the macros that are defined in the default macro resolver, see the source for it.
> 
> For anything that goes beyond this, you have to implement your own content handler class. 
> 
> Storing and loading content is performed by a content handler. The default content handler used in OpenCms is org.opencms.xml.content.CmsDefaultXmlContentHandler
> 
> The default content handler makes the following:
> 
> 	• sets the default values for elements
> 	• writes the defined mappings
> 	• validates elements
> 
> To extend the content handler, extend the default content handler class and overwrite some methods.
> 
> In most cases, additional actions should be performed when saving an XML content, e.g., validation, pretty printing, resource modification
> To adjust actions performed on saving, override the method
> 
> CmsXmlContent prepareForWrite(CmsObject cms, CmsXmlContent content, CmsFile file)
> 
> that is always called before the XML content is written to the OpenCms VFS.
> 
> Important! Always call the method of the super class from your implementation first!
> 
> Another possible extension is to perform specific actions before the XML content is used (in the editor or in the frontend)
> In this case, the method
> 
> CmsXmlContent prepareForUse(CmsObject cms, CmsXmlContent content)
> 
> should be overwritten.
> This method is always called after the XML content is unmarshaled, so modifications can be made to the XML content element values. You could add your default value logic in here.
> 
> If you need special validation of content fields (e.g. depending on other fields, access to external sources) validation can be extended. In this case, the method
> 
> CmsXmlContentErrorHandler resolveValidation( CmsObject cms,  I_CmsXmlContentValue value,    CmsXmlContentErrorHandler errorHandler)
> 
> should be overwritten
> This method is called for each content field (value) and allows to access the complete content.
> 
> 
> The XML content handler to use must be specified in the appinfo section of the XSD defining the XML content:
> 
> <xsd:annotation>
>     <xsd:appinfo>
>         <handler class= "myContentHandlerClass" />  …
> 
> If no handler class is specified, OpenCms uses automatically the default XML content handler.
> 
> 
> 
> Hope this helps.
> 
> Kind regards,
> 
> Michael
> 
> 
>> 
>> 
> -- 
> Michael Emmerich
>  



More information about the opencms-dev mailing list