Legacy Knowledge Base
Published Jun. 30, 2025

How to get a drop-down option's field reference in a fragment's html/freemarker code?

Written By

Sorin Pop

How To articles are not official guidelines or officially supporteddocumentation. They are community-contributed content and may not alwaysreflect the latest updates to Liferay DXP. We welcome your feedback toimprove How to articles!

While we make every effort to ensure this Knowledge Base is accurate, itmay not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with anyfeedback or concerns.

Legacy Article

You are viewing an article from our legacy "FastTrack"publication program, made available for informational purposes. Articlesin this program were published without a requirement for independentediting or verification and are provided "as is" withoutguarantee.

Before using any information from this article, independently verify itssuitability for your situation and project.

Issue

  • Steps to reproduce:
    0. In System Settings/Freemarker Engine, remove serviceLocator from the list of restricted variables. (so that we can use it in a fragment code later on)
    1. Create a web content structure and put a Select From List field on it.
    2. Add 2 options for this field: option1 and option2.
    3. For option2, double-click on its autogenerated Field Reference and change it to something, e.g. optref2
    4. Go to the advanced tab and enable Allow Multiple Selections (this is so that later we can see the difference in behaviour between the two kinds of options: the one for which we didn't touch the autogenerated field reference, and the one for which we manually altered it). Save the structure.
    5. Create a new web content of this structure, and tick both options for the field. Publish.
    6. Get the Site ID (groupid of the site) and the articleId of the web content article (journalarticle table): to be used in the html/freemarker fragment code further down.
    7. Go to Design/Fragments and start creating a new basic fragment after creating a new fragment set.
    In the HTML box paste a code like this:

    <div class="fragment_1">
    	 [#assign srv = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService")]
       [#assign article = srv.getArticle(20121,"44153")]
       [#assign document = saxReaderUtil.read(article.getContent())]
       [#assign rootElement = document.getRootElement()]
    	 
    	 [#list rootElement.elements() as dynamicElement ]
    	   [#if dynamicElement.attributeValue("name") == "Select23481751"]
    	      [#assign selection = dynamicElement.element("dynamic-content").getStringValue()]
    	      ${selection}
    	   [/#if]
    	 [/#list]
    </div>
    

    Where:
    20121 is the groupid
    "44153" is the articleid
    Select23481751 is the autogenerated field reference of the drop-down field created at step1 (you can find it on the Advanced tab)
    These values might be different for your test environment.

    Result: in the right-hand lower box you see (screenshot also attached)

    Option44203449 Option62081055

    (so for the second option, you still see the initially autogenerated field reference, not the one to which you changed it to in step 3: optref2)

    Expected result: you should see

    Option44203449 optref2

Environment

  • DXP 7.4

Resolution

  • The DDMStructure from the Article should be used to access the DMFormFieldOptions.
     
    Here is an example for a proper fragment html code:
     
    <div class="fragment_1"> 
    [#assign srv = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService")] 
    [#assign article = srv.getArticle(20119,"44810")] 
    [#assign document = saxReaderUtil.read(article.getContent())] 
    [#assign rootElement = document.getRootElement()] 
    [#assign ddmStructure = article.getDDMStructure()] 

    [#list rootElement.elements() as dynamicElement ] 
    [#if dynamicElement.attributeValue("field-reference") == "Select55199703"] 
    [#assign ddmFormField = ddmStructure.getDDMFormField("Select55199703") /] 
    [#assign ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions() /] 
    [#assign optionsReferences = ddmFormFieldOptions.getOptionsReferences() /] 

    [#assign selections = dynamicElement.element("dynamic-content").getStringValue()?word_list]
    [#list selections as optionValue] 
    ${"option value:"} ${optionValue} <br> 
    ${"option reference:"} ${optionsReferences[optionValue]}<br><br> 
    [/#list] 
    [/#if] 
    [/#list] 
    </div>
     
    With this example, the response will look like:
    option value: Option56521562
    option reference: Option56521562

    option value: Option70933281
    option reference: optref2

 

Did this article resolve your issue ?

Legacy Knowledge Base