cancel
Showing results for 
Search instead for 
Did you mean: 

Form control for multiple association on aspect reference

4535992
Star Collaborator
Star Collaborator

Hi i need to emulate the dialog show from the action "manage-aspects" , on a field form control on Alfresco Share.

My point is to make the user select a list of aspect and send that list to the java backend service code with the post of the from and in that moment i use the aspects selected from the user for making things on the java code.

I have tried to emulate the  Multiple association with person nodes

(you can find a example on this project GitHub - angelborroy/alfresco-controls: Samples for Alfresco Share controls )

This is a simple model:

<?xml version="1.0" encoding="UTF-8"?>
<model name="up:testModel" xmlns="http://www.alfresco.org/model/dictionary/1.0">

    <description>eeeee</description>
    <author>eeeeeeeeeeeeee</author>
    <published>eeeeeeeeee</published>
    <version>1.0</version>

    <imports>
         <!-- Import Alfresco Dictionary Definitions -->
         <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
         <!-- Import Alfresco Content Domain Model Definitions -->
         <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/>
         <!-- Import Alfresco Data List Model Definitions -->
         <import uri="http://www.alfresco.org/model/datalist/1.0" prefix="dl"/>
    </imports>

    <namespaces>
        <namespace uri="extension" prefix="up"/>
    </namespaces>
    
    <aspects>
        <aspect name="up:test">
            <title>Test</title>
            <properties>
                <property name="up:testMultipleText">
                  <title>testMultipleText</title>
                  <type>d:text</type>
                  <multiple>true</multiple>
                  <index enabled="true">
                    <atomic>true</atomic>
                    <stored>false</stored>
                    <tokenised>false</tokenised>
                  </index>
                </property>
             </properties>
            <associations>
                <association name="up:testMultipleAssociation">
                    <title>testMultipleAssociation</title>
                    <source>
                        <mandatory>false</mandatory>
                        <many>true</many>
                    </source>
                    <target>
                        <class>cm:content</class>
                        <mandatory>false</mandatory>
                        <many>true</many>
                    </target>
                </association>
            </associations>
            <overrides></overrides>
            <mandatory-aspects></mandatory-aspects>
        </aspect>
    </aspects>

</model>

I need to save  a list of name of the aspects or a list of associations to the aspects on the post of the form.

Anyone have already done this?

Greetings.

1 ACCEPTED ANSWER

roberto_gamiz
Star Contributor
Star Contributor

Hello,

You can not associate aspects with documents using an association in the model.

The simplest and quickest way but of course not the best is using the param option of selectmany control with a text or multitext property:

<field id="up:testMultipleText">
<control template="/org/alfresco/components/form/controls/selectmany.ftl">
<control-param name="options">up:aspect|Aspect,up:aspect2|Aspect2,up:aspect3|Aspect3</control-param>
</control>
</field>
‍‍‍‍‍‍‍‍‍

However, the correct way will be indeed develope a custom control based on selectmany but obtaining the options using an Ajax call to the repository.

Regards.

View answer in original post

4 REPLIES 4

krutik_jayswal
Elite Collaborator
Elite Collaborator

Why you would like to save the list off association/aspect in the node/document.Aspects associated with document can also be retrieved in java using some java services like nodeservice.

If you would like to perform some operation when particular aspect is added in document, this is also possible using policies and behavior.You can get more details on that on below link.

http://docs.alfresco.com/6.0/references/dev-extension-points-behaviors.html

Hi Krutik,

in my use case it's more a "Client Share" solution than a "Alfresco Server" solution (like the behaviours).

I just want to know if anyone (before i started to developed) have already done a specialized FTL like the standard "selectmany.ftl" control  form field for select the aspects and send in to the FormData to alfresco.

I need to force my user to create content with some aspects but i can't use the behaviour because i can't predict what aspects the users want to use so i let them choose from a list in a page form dialog on Share.

Greetings.

roberto_gamiz
Star Contributor
Star Contributor

Hello,

You can not associate aspects with documents using an association in the model.

The simplest and quickest way but of course not the best is using the param option of selectmany control with a text or multitext property:

<field id="up:testMultipleText">
<control template="/org/alfresco/components/form/controls/selectmany.ftl">
<control-param name="options">up:aspect|Aspect,up:aspect2|Aspect2,up:aspect3|Aspect3</control-param>
</control>
</field>
‍‍‍‍‍‍‍‍‍

However, the correct way will be indeed develope a custom control based on selectmany but obtaining the options using an Ajax call to the repository.

Regards.

4535992
Star Collaborator
Star Collaborator

k the more simple solution i found is like Roberto tell, i ctumozie a sleect-many template .

Here the my ftl for anyone can found useful:

<#include "/org/alfresco/components/form/controls/common/utils.inc.ftl" />

<#if field.control.params.size??><#assign size=field.control.params.size><#else><#assign size=5></#if>

<#if field.control.params.optionSeparator??>
   <#assign optionSeparator=field.control.params.optionSeparator>
<#else>
   <#assign optionSeparator=",">
</#if>
<#if field.control.params.labelSeparator??>
   <#assign labelSeparator=field.control.params.labelSeparator>
<#else>
   <#assign labelSeparator="|">
</#if>

<#assign fieldValue=field.value>

<#if fieldValue?string == "" && field.control.params.defaultValueContextProperty??>
   <#if context.properties[field.control.params.defaultValueContextProperty]??>
      <#assign fieldValue = context.properties[field.control.params.defaultValueContextProperty]>
   <#elseif args[field.control.params.defaultValueContextProperty]??>
      <#assign fieldValue = args[field.control.params.defaultValueContextProperty]>
   </#if>
</#if>

<#if fieldValue?string != "">
   <#assign values=fieldValue?split(",")>
<#else>
   <#assign values=[]>
</#if>

<div class="form-field">
   <#if form.mode == "view">
      <div class="viewmode-field">
         <#if field.mandatory && !(fieldValue?is_number) && fieldValue?string == "">
            <span class="incomplete-warning"><img src="${url.context}/res/components/form/images/warning-16.png" title="${msg("form.field.incomplete")}" /><span>
         </#if>
         <span class="viewmode-label">${field.label?html}:</span>
         <#if fieldValue?string == "">
            <#assign valueToShow=msg("form.control.novalue")>
         <#else>
            <#if field.control.params.options?? && field.control.params.options != "" &&
                 field.control.params.options?index_of(labelSeparator) != -1>
                 <#assign valueToShow="">
                 <#assign firstLabel=true>
                 <#list field.control.params.options?split(optionSeparator) as nameValue>
                    <#assign choice=nameValue?split(labelSeparator)>
                    <#if isSelected(choice[0])>
                       <#if !firstLabel>
                          <#assign valueToShow=valueToShow+",">
                       <#else>
                          <#assign firstLabel=false>
                       </#if>
                       <#assign valueToShow=valueToShow+choice[1]>
                    </#if>
                 </#list>
            <#else>
               <#assign valueToShow=fieldValue>
            </#if>
         </#if>
         <span class="viewmode-value">${valueToShow?html}</span>
      </div>
   <#else>
           <script>//<![CDATA[
            // Ensure ExtAlf namespace exists
            if (typeof ExtAlf === "undefined" || !ExtAlf)
            {
                var ExtAlf = {};
            }
            
            (function()
            {
                ExtAlf.SelectManyAspects = function SelectManyAspects_constructor(htmlId)
                {
                    ExtAlf.SelectManyAspects.superclass.constructor.call(this, htmlId);
            
                    this.name = "ExtAlf.SelectManyAspects";
                    this.id = htmlId;
            
                    Alfresco.util.ComponentManager.register(this);
                    Alfresco.util.YUILoaderHelper.require(["button", "container"], this.onComponentsLoaded, this);
            
                    return this;
                };
            
                YAHOO.extend(ExtAlf.SelectManyAspects, Alfresco.component.Base,
                {
                    options:
                    {
                    },
                    setOptions: function SelectManyAspects_setOptions(obj)
                    {
                        this.options = YAHOO.lang.merge(this.options, obj);
                        return this;
                    },
                    setMessages: function SelectManyAspects_setMessages(obj)
                    {
                        Alfresco.util.addMessages(obj, this.name);
                        return this;
                    },
                    onReady: function SelectManyAspects_onReady()
                    {
                        var selectedValue = this.options.selectedValue;
                        var setOptionsCallback =
                        {
                            success: function(o)
                            {
                                var values, i;
                                var listbox = YAHOO.util.Dom.get(this.id + "-entry");
                                if(!listbox){
                                    listbox = document.getElementById(this.id + "-entry");
                                }
            
                                try
                                {
                                    values = YAHOO.lang.JSON.parse(o.responseText);
                                    values = values.data.items;
                                    listbox.remove(listbox.length - 1); // Remove "Loading..." text
                                }
                                catch
                                {
                                    console.error("Json parse failed! " + x);
                                    return;
                                }
            
                                for(i = 0; i < values.length; i++)
                                {
                                    var v = values[i];
                                    var option = new Option('option');
            
                                    option.value = (v.value === null || v.value === "") ? v.label : v.value;
                                    option.text = v.label;
                                    option.selected = (option.value === selectedValue);
                                    listbox.add(option);
                                }
                            },
                              failure: function(o)
                              {
                                  Alfresco.util.PopupManager.displayMessage({text: "Could not load values!"});
                              },
                              scope: this
                        };
            
                        var listUrl = Alfresco.constants.PROXY_URI + "service-custom/form/control/dgAspectsPicker";
                        var transaction = YAHOO.util.Connect.asyncRequest('GET', listUrl, setOptionsCallback, null);
                    }
                });
            })();
            
            var options = {};
            options.selectedValue = "${field.value}";
            options.listBoxName = "${field.control.params.listboxname}";//<control-param name="listboxname">changeRequest_status</control-param>
            new ExtAlf.SelectManyAspects("${fieldHtmlId}").setOptions(options);
        //]]></script>

      <label for="${fieldHtmlId}-entry">${field.label?html}:<#if field.mandatory><span class="mandatory-indicator">${msg("form.required.fields.marker")}</span></#if></label>
      <input id="${fieldHtmlId}" type="hidden" name="${field.name}" value="${fieldValue?string}" />
      <#if field.control.params.listboxname?? && field.control.params.listboxname != "">
         <select id="${fieldHtmlId}-entry" name="-" multiple="multiple" size="${size}" tabindex="0"
               onchange="javascript:Alfresco.util.updateMultiSelectListValue('${fieldHtmlId}-entry', '${fieldHtmlId}', <#if field.mandatory>true<#else>false</#if>);"
               <#if field.description??>title="${field.description}"</#if>
               <#if field.control.params.styleClass??>class="${field.control.params.styleClass}"</#if>
               <#if field.control.params.style??>style="${field.control.params.style}"</#if>
               <#if field.disabled && !(field.control.params.forceEditable?? && field.control.params.forceEditable == "true")>disabled="true"</#if>>
               <option>Loading...</option>
         </select>
         <@formLib.renderFieldHelp field=field />
      <#else>
         <div id="${fieldHtmlId}" class="missing-options">${msg("form.control.selectone.missing-options")}</div>
      </#if>
   </#if>
</div>

<#function isSelected optionValue>
   <#list values as value>
      <#if optionValue == value?string || (value?is_number && value?c == optionValue)>
         <#return true>
      </#if>
   </#list>
   <#return false>
</#function>

<#function isValidMode modeValue>
   <#return modeValue == "OR" || modeValue == "AND">
</#function>