component to list documents in subfolder

49 views
Skip to first unread message

Luke McDowell

unread,
Mar 21, 2018, 7:15:33 AM3/21/18
to Hippo Community
I needed a component to list all documents in a subfolder. It took a lot of hunting and piecing together to get working code so I wanted to share here.

The component code returns all published documents in the specified subfolder (in this case "news"). There is no pagination or filtering:

import java.util.List;

import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.content.beans.standard.HippoDocumentBean;
import org.hippoecm.hst.content.beans.standard.HippoFolder;
import org.onehippo.cms7.essentials.components.EssentialsListComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DocumentListComponent extends EssentialsListComponent {


   
public static final Logger log = LoggerFactory.getLogger(DocumentListComponent.class);


   
@Override
   
public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {


       
try {
           
final HippoFolder folder = getHippoBeanForPath("news", HippoFolder.class);
           
final List<HippoDocumentBean> documents = folder.getDocuments();
           
           
if (documents != null) {
                request
.setAttribute("documents", documents);
           
}
       
} catch (Exception e) {
            e
.printStackTrace();
       
}


   
}


}


And the code for the FreeMarker template used by the component (in my case I gave the document type a field called "title"):

<ul>
<#list documents as item>
    <@hst.link var="link" hippobean=item/>
   
<li><a href="${link}">${item.title}</a></li>
</#list>

</ul>


I hope this helps for someone looking for a similar solution. Suggestions for improvement are welcome.

Woonsan Ko

unread,
Mar 30, 2018, 11:40:35 AM3/30/18
to hippo-c...@googlegroups.com
Thank you so much for sharing this, Luke! :-)
Everything looks really good and simple. Cool.
I have just one remark for potential improvements in the product. See it inline below.

On Wed, Mar 21, 2018 at 7:15 AM, Luke McDowell <migh...@gmail.com> wrote:
I needed a component to list all documents in a subfolder. It took a lot of hunting and piecing together to get working code so I wanted to share here.

The component code returns all published documents in the specified subfolder (in this case "news"). There is no pagination or filtering:

import java.util.List;

import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.content.beans.standard.HippoDocumentBean;
import org.hippoecm.hst.content.beans.standard.HippoFolder;
import org.onehippo.cms7.essentials.components.EssentialsListComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DocumentListComponent extends EssentialsListComponent {


   
public static final Logger log = LoggerFactory.getLogger(DocumentListComponent.class);


   
@Override
   
public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {


       
try {
           
final HippoFolder folder = getHippoBeanForPath("news", HippoFolder.class);
           
final List<HippoDocumentBean> documents = folder.getDocuments();
           
           
if (documents != null) {
                request
.setAttribute("documents", documents);
           
}
       
} catch (Exception e) {
            e
.printStackTrace();
       
}


   
}


}

So, I assume your requirement is to list the 1-level child documents only under the specific folder, right?
Then, would it be an idea to introduce a new parameter in org.onehippo.cms7.essentials.components.info.EssentialsListComponentInfo like the following?

    @Parameter(name = "includeAllDescendant", defaultValue = "true", required = false)
    Boolean getIncludeAllDescendant();

If it's set in the param dialog or component parameters and we set the page size to max number, your use case could be covered.

What do you think?

Regards,

Woonsan
 

And the code for the FreeMarker template used by the component (in my case I gave the document type a field called "title"):

<ul>
<#list documents as item>
    <@hst.link var="link" hippobean=item/>
   
<li><a href="${link}">${item.title}</a></li>
</#list>

</ul>


I hope this helps for someone looking for a similar solution. Suggestions for improvement are welcome.

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-community@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-community+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.



--

Luke McDowell

unread,
Apr 2, 2018, 4:23:45 PM4/2/18
to Hippo Community
Thanks Woonsan. You are correct, my requirement was to include all first level children of a specified folder. I think the component, as I wrote it, does that. I have not tested it with a large number of documents yet.

The component does not go through sub-folders. I was interested in setting it up to do that but have not been able to figure that out yet.

Regarding adding the includeAllDescendant parameter to EssentialsListComponentInfo, it seems like that would be helpful.

- Luke
Reply all
Reply to author
Forward
0 new messages