Alfresco Share custom search form - non-mandatory search field for constrained aspect

cancel
Showing results for 
Search instead for 
Did you mean: 
marcollirite
Active Member II

Alfresco Share custom search form - non-mandatory search field for constrained aspect

Jump to solution

Hi all

I am currently working on defining search forms for my custom content model. In this model, I have an aspect (projectRelated) which allows the selection of a value (a project) from a constrained list. However, this is not a mandatory aspect, and not all of my documents will have a value for this aspect.

This leads to a problem when configuring my search form for my custom content type. If I want to include this aspect as a searchable parameter, I currently have to choose a value from this list, which means any documents which don't have this aspect applied to them are automatically excluded from the search. I am looking for a way to search without having to set this value (e.g. by choosing an "unknown" or "none" value from the dropdown list). Any ideas?

Thanks

Marcus

1 Solution

Accepted Solutions
marcollirite
Active Member II

Re: Alfresco Share custom search form - non-mandatory search field for constrained aspect

Jump to solution

I managed to figure out a solution which involves creating a new freemarker template based on selectone.ftl. It uses the same code, but with an empty <option></option> tag included, as below:

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

<#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>

<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>
<#assign valueToShow=fieldValue>
<#if field.control.params.options?? && field.control.params.options != "">
<#list field.control.params.options?split(optionSeparator) as nameValue>
<#if nameValue?index_of(labelSeparator) == -1>
<#if nameValue == fieldValue?string || (fieldValue?is_number && fieldValue?c == nameValue)>
<#assign valueToShow=nameValue>
<#break>
</#if>
<#else>
<#assign choice=nameValue?split(labelSeparator)>
<#if choice[0] == fieldValue?string || (fieldValue?is_number && fieldValue?c == choice[0])>
<#assign valueToShow=msgValue(choice[1])>
<#break>
</#if>
</#if>
</#list>
</#if>
</#if>
<span class="viewmode-value">${valueToShow?html}</span>
</div>
<#else>
<label for="${fieldHtmlId}">${field.label?html}:<#if field.mandatory><span class="mandatory-indicator">${msg("form.required.fields.marker")}</span></#if></label>
<#if field.control.params.options?? && field.control.params.options != "">
<select id="${fieldHtmlId}" name="${field.name}" tabindex="0"
<#if field.description??>title="${field.description}"</#if>
<#if field.indexTokenisationMode??>class="non-tokenised"</#if>
<#if field.control.params.size??>size="${field.control.params.size}"</#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></option>
<#list field.control.params.options?split(optionSeparator) as nameValue>
<#if nameValue?index_of(labelSeparator) == -1>
<option value="${nameValue?html}"<#if nameValue == fieldValue?string || (fieldValue?is_number && fieldValue?c == nameValue)> selected="selected"</#if>>${nameValue?html}</option>
<#else>
<#assign choice=nameValue?split(labelSeparator)>
<option value="${choice[0]?html}"<#if choice[0] == fieldValue?string || (fieldValue?is_number && fieldValue?c == choice[0])> selected="selected"</#if>>${msgValue(choice[1])?html}</option>
</#if>
</#list>
</select>
<@formLib.renderFieldHelp field=field />
<#else>
<div id="${fieldHtmlId}" class="missing-options">${msg("form.control.selectone.missing-options")}</div>
</#if>
</#if>
</div>

Then include this template in your search form for the relevant properties/aspects. This will generate an "empty" option in the dropdown list. Selecting this option means that the parameter will not be used as a search term for that particular query.

View solution in original post

4 Replies
marcollirite
Active Member II

Re: Alfresco Share custom search form - non-mandatory search field for constrained aspect

Jump to solution

I managed to figure out a solution which involves creating a new freemarker template based on selectone.ftl. It uses the same code, but with an empty <option></option> tag included, as below:

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

<#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>

<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>
<#assign valueToShow=fieldValue>
<#if field.control.params.options?? && field.control.params.options != "">
<#list field.control.params.options?split(optionSeparator) as nameValue>
<#if nameValue?index_of(labelSeparator) == -1>
<#if nameValue == fieldValue?string || (fieldValue?is_number && fieldValue?c == nameValue)>
<#assign valueToShow=nameValue>
<#break>
</#if>
<#else>
<#assign choice=nameValue?split(labelSeparator)>
<#if choice[0] == fieldValue?string || (fieldValue?is_number && fieldValue?c == choice[0])>
<#assign valueToShow=msgValue(choice[1])>
<#break>
</#if>
</#if>
</#list>
</#if>
</#if>
<span class="viewmode-value">${valueToShow?html}</span>
</div>
<#else>
<label for="${fieldHtmlId}">${field.label?html}:<#if field.mandatory><span class="mandatory-indicator">${msg("form.required.fields.marker")}</span></#if></label>
<#if field.control.params.options?? && field.control.params.options != "">
<select id="${fieldHtmlId}" name="${field.name}" tabindex="0"
<#if field.description??>title="${field.description}"</#if>
<#if field.indexTokenisationMode??>class="non-tokenised"</#if>
<#if field.control.params.size??>size="${field.control.params.size}"</#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></option>
<#list field.control.params.options?split(optionSeparator) as nameValue>
<#if nameValue?index_of(labelSeparator) == -1>
<option value="${nameValue?html}"<#if nameValue == fieldValue?string || (fieldValue?is_number && fieldValue?c == nameValue)> selected="selected"</#if>>${nameValue?html}</option>
<#else>
<#assign choice=nameValue?split(labelSeparator)>
<option value="${choice[0]?html}"<#if choice[0] == fieldValue?string || (fieldValue?is_number && fieldValue?c == choice[0])> selected="selected"</#if>>${msgValue(choice[1])?html}</option>
</#if>
</#list>
</select>
<@formLib.renderFieldHelp field=field />
<#else>
<div id="${fieldHtmlId}" class="missing-options">${msg("form.control.selectone.missing-options")}</div>
</#if>
</#if>
</div>

Then include this template in your search form for the relevant properties/aspects. This will generate an "empty" option in the dropdown list. Selecting this option means that the parameter will not be used as a search term for that particular query.

douglascrp
Advanced II

Re: Alfresco Share custom search form - non-mandatory search field for constrained aspect

Jump to solution

Or an even easier workaround would be adding an empty value in the list constraint. It would have the same result in the end.

That is how I do that.

marcollirite
Active Member II

Re: Alfresco Share custom search form - non-mandatory search field for constrained aspect

Jump to solution

Hi Douglas

Thanks for your comment. I am not sure if I did something wrong but this was actually the first solution I tried, which gave me the problem that the search would only return documents which have the "empty value" as the value in the dropdown list (rather than returning all documents with any value, as I had desired).

douglascrp
Advanced II

Re: Alfresco Share custom search form - non-mandatory search field for constrained aspect

Jump to solution

Sorry, but I missed your answer/question here.

Have you tried using the select-many control inside the advanced-search form configuration, like this?

<field id='my:lovproperty'>
    <control template='/org/alfresco/components/form/controls/selectmany.ftl' />
</field>

That is what the following doc recommends: Share Advanced Search 

You can even use the following parameter to define the search operator used for this property:

<control-param name='mode'>OR</control-param>