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


  • 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 =]
       [#assign rootElement = document.getRootElement()]
    	 [#list rootElement.elements() as dynamicElement ]
    	   [#if dynamicElement.attributeValue("name") == "Select23481751"]
    	      [#assign selection = dynamicElement.element("dynamic-content").getStringValue()]

    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


  • DXP 7.4


  • 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 =] 
    [#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> 
    With this example, the response will look like:
    option value: Option56521562
    option reference: Option56521562

    option value: Option70933281
    option reference: optref2


Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0