How to add wiki articles to the advanced search

Adding an option in the advanced search to look for wiki articles is possible, but It’s not as straightforward as we would like.

By default, wiki articles have “cm:content” type, so there isn’t a way to distinguish them from the rest of nodes using the type. However, we can change its type using a behavior and, in this case, it would be easy to implement an advanced search option for the new wiki articles’ type.

Well, we are going to see the solution step by step. And also, you have a project example with this functionality available on github.

1. Create a model with the new type

<types>
    <!-- Wiki Article Custom Type: This type is used to control what content is a wiki article -->
    <type name="how:wikiArticleType">
        <title>Wiki Article custom type</title>
        <parent>cm:content</parent>
        <properties>
            <property name="how:isWikiArticle">
                <title>Is Wiki Article</title>
                <type>d:boolean</type>
                <default>true</default>
                <index enabled="true">
                    <tokenised>true</tokenised>
                </index>
            </property>
        </properties>
    </type>
</types>

1. Add a behavior to set the new custom type to wiki articles

In this example, we know if a node is a wiki article checking the name and its parent’s type, but we could be more exhaustive if we need it.

@Override
public void onCreateNode(ChildAssociationRef childAssocRef) {
	NodeService nodeService=serviceRegistry.getNodeService();
	NodeRef parent=childAssocRef.getParentRef();
	//Get the name and the type of the node's parent
	String name=(String) nodeService.getProperty(parent, ContentModel.PROP_NAME);
	QName type = nodeService.getType(parent);
	//Check if the node is a new page in the wiki checking the name and type of its parent
	if(type.equals(ContentModel.TYPE_FOLDER) && name.equals("wiki")) {
		//Set a custom type to the node to indicate it's a wiki article
		NodeRef child=childAssocRef.getChildRef();
		nodeService.setType(child, WikiModel.WikiArticle.TYPE);
		//put a true the property "how:isWikiArticle"
		nodeService.setProperty(child, WikiModel.WikiArticle.Prop.IS_WIKI_ARTICLE,  new Boolean(true));
	}
}

3. Add the wiki articles option to the advanced search

<!--== Advanced Search Forms for types (search forms for aspects are not available) We have added a advanced search option for wiki pages == -->

<config evaluator="string-compare" condition="AdvancedSearch" replace="true">
    <advanced-search>
        <forms>
            <form labelId="form.label.advancedsearch.wikiPage" descriptionId="form.description.advancedsearch.wikiPage">
                how:wikiArticleType
            </form>
        </forms>
    </advanced-search>
</config>

    <!--==== Config for CONTENT, SPECIFICALLY FOR WIKI PAGE ==== -->

    <!-- Create and search forms for how:wikiArticleType to search wiki pages -->
<config evaluator="model-type" condition="how:wikiArticleType" replace="true">
    <forms>
        <!-- Search form -->
        <form id="search" replace="true">
            <field-visibility>
                <show id="cm:name"/>
                <show id="cm:taggable" force="true"/>
                <show id="how:isWikiArticle" force="true"/>
            </field-visibility>
            <appearance>
                <field id="cm:name" label="Title"></field>
                <field id="cm:taggable">
                    <control>
                        <control-param name="compactMode">true</control-param>
                        <control-param name="params">aspect=cm:taggable
                        </control-param>
                        <control-param name="createNewItemUri">/api/tag/workspace/SpacesStore
                        </control-param>
                        <control-param name="createNewItemIcon">tag</control-param>
                    </control>
                </field>
                <!-- This field will be hidden and have a true value by default, so the search only provides nodes with this type and this property with the value true-->
                <field id="how:isWikiArticle">
                    <control template="/com/howtobrothers/custom-form/controls/isWikiArticle.ftl">
                        <control-param name="options">true
                        </control-param>
                    </control>
                </field>
            </appearance>
        </form>
    </forms>
</config>

To do that, we add the following configuration to \src\main\resources\META-INF\share-config-custom.xml file.

4. Finally, add the the custom form field definition .ftl used

In our case, we have implemented /com/howtobrothers/custom-form/controls/isWikiArticle.ftl based on Alfresco’s selectone.ftl, adding a style to hidden this field and to set this to “true” by default.

If you need more details, please, check the complete solution here.