agarcia
I'm new here

How to programmatically read generated links of CMS_INPUT_DOM

Jump to solution

Sorry if this answer has already been answered, but as I don't speak German I can't read most of the results in the community.

I have a section where I have a CMS_INPUT_DOM field called "st_thumbnail_body".

I access it using the API like this:

section.getFormData().get(language, "st_thumbnail_body").get().toText(true));

Then I get the HTML of the CMS_INPUT_DOM but links look like this:

<p>... <a href=#" data-link-template="lt_external_link" data-link-id="9c02ae01-5bf3-4be3-87da-0eeccd38def1" ...</p>

I need to get the generated code for those links:

<a href="anotherdomain.com">Go to another page</a>

I've check that there is a method called "toHtml" in DomEditorValue, but executing like this gives me error:

section.getFormData().get(language, "st_thumbnail_body").get().toHtml(language));

EDIT

-----------------------------------------

The error code I get is:

ERROR 20.05.2019 16:49:50.467{g-node=1143522} (de.espirit.firstspirit.generate.SiteProduction):  at 0, 0: Script 'get_pages' (id=1143526) - Error in method invocation: Method toHtml(de.espirit.firstspirit.server.projectmanagement.LanguageImpl) not found in class'de.espirit.firstspirit.store.access.DataWrappingFormData$EditorValueWrappingFormField' : at Line: 250 : in file: inline evaluation of: ``__execute() { //!Beanshell import de.espirit.firstspirit.access.AccessUtil; impo . . . '' : .toHtml ( language )

0 Kudos
1 Solution

Accepted Solutions
felix_reinhold
Returning Responder

Hi Andres,

I usually do something like that:

$CMS_SET(mytext)$$CMS_VALUE(st_thumbnail_body)$$CMS_END_SET$

$CMS_SET(mytext,mytext.toString())$

Now myText contains the generated content of the DOM.

If you only need the links then you'd need to get the LinkDomNodes first. You could do sth. like this:

$CMS_SET(ENUM_NODE_TYPE, class("de.espirit.firstspirit.access.editor.value.DomNodeType"))$

$CMS_SET(domRoot, section.getFormData().get(language, "st_thumbnail_body").get().getRoot())$

$CMS_FOR(node, domRoot.getChildren(true))$

     $CMS_IF(node.getNodeType() == ENUM_NODE_TYPE.LINK)$

          $CMS_SET(myLinkHTML)$$CMS_VALUE(node.getLink())$$CMS_END_SET$

          $CMS_SET(myLinkHTML, myLinkHTML.toString())$

          $CMS_SET(void, #global.logInfo(myLinkHTML))$

     $CMS_END_IF$

$CMS_END_FOR$

Replace the logging with the operations you want to perform with the links.

best regards

Felix

View solution in original post

0 Kudos
2 Replies
felix_reinhold
Returning Responder

Hi Andres,

I usually do something like that:

$CMS_SET(mytext)$$CMS_VALUE(st_thumbnail_body)$$CMS_END_SET$

$CMS_SET(mytext,mytext.toString())$

Now myText contains the generated content of the DOM.

If you only need the links then you'd need to get the LinkDomNodes first. You could do sth. like this:

$CMS_SET(ENUM_NODE_TYPE, class("de.espirit.firstspirit.access.editor.value.DomNodeType"))$

$CMS_SET(domRoot, section.getFormData().get(language, "st_thumbnail_body").get().getRoot())$

$CMS_FOR(node, domRoot.getChildren(true))$

     $CMS_IF(node.getNodeType() == ENUM_NODE_TYPE.LINK)$

          $CMS_SET(myLinkHTML)$$CMS_VALUE(node.getLink())$$CMS_END_SET$

          $CMS_SET(myLinkHTML, myLinkHTML.toString())$

          $CMS_SET(void, #global.logInfo(myLinkHTML))$

     $CMS_END_IF$

$CMS_END_FOR$

Replace the logging with the operations you want to perform with the links.

best regards

Felix

0 Kudos

Thanks felix.reinhold

Your solution worked as a charm.

I didn't know I had to traverse the DomNodes.

Just for the record, in case anyone searches for this:

Since I was working inside an script, after getting the Link object I had to access its fields to get the href and text:

linkHref = node.getLink().getFormData().get(language, "lt_external_href"); // Depending on your link templates

linkText = node.getLink().getFormData().get(language, "lt_external_text"); // Depending on your link templates

0 Kudos