Collection Tree : display only parents collections in advanced search dropdown


I use the Collection Tree plugin and I only want parents collections to be displayed in the dropdown of the advanced search under 'collections'.
Maybe I can do it with the $searchParams of get_table_options() in search-form.php, but I don't know how to do it.
Any help or idea ?


This turns out to have some interesting twists.

$searchParams won't help. But, there is code there that looks like it was intended to do what you are looking for anyway.

But, that code wasn't running due to a tiny typo/bug.

But, when that's fixed, I'm still not sure it will get at what you want -- with it fixed, only top-level collections appear, anything at a middle-level (has a parent, and is also a parent of another collection) also does not appear.

Additionally, the search doesn't include items in subcollections -- only the items assigned directly to that top-level parent collection appear in search results.

With that in mind, could you tell more about the details of the search functionality and dropdown display you are looking for?

Thank you Patrick,

Here is my situation. My top-level collections are authors, and I have only one subcollection level, which represent different works of those authors. My items are manuscripts and the majority of them are in those subcollections.

In the collection dropdown of the advanced search, I have both authors and works, mixed together. I don't really want to have works in it, I'd like this dropdown to be a selection by authors.

So the first part of the solution, only top-level collections are displayed, works fine for me. But the second one can be an issue, as it would be nice if the items of the subcollections could appear in the search result when the parent collection is selected.

I hope I explained it right.

Yeah, that's a good explanation, and a really interesting use case for the collection tree.

First, the basic fix to not displaying subcollections is in this github commit. Keep in mind this will affect both the dropdown and the collections browse page if you are using it.

After that, I see two different routes that might do the trick. One is a hack on the theme you are using, and one is a hack on the CollectionTree plugin itself.

I haven't worked through the details of hacking the plugin, but it would basically amount implementing the items_browse_sql hook so that it checks whether a collection has been set in the $params argument, then digs up the subcollection ids and alters the SQL query to join in items in those subcollections. If my SQL skills were stronger, I'd have tried that route.

The other, somewhat dirtier way, is to alter the items/browse.php page for your theme to do a similar job.

Here's about what it would look like. In that file, before the line displaying the page title, add this:

//dig up the request parameters, which will say whether there's been a search by collection
$request = Zend_Controller_Front::getInstance()->getRequest();
$requestArray = $request->getParams();

if (isset($requestArray['collection'])) {
    $collectionId = $requestArray['collection'];
    $childCollections = get_db()->getTable('CollectionTree')->getChildCollections($collectionId);
    $allItems = $items; //items is just what's in the direct collection, so start there
    foreach ($childCollections as $childCollectionArray) {
        $allItems = array_merge($allItems,
                                             array('collection_id' => $childCollectionArray['id'])
    set_loop_records('items', $allItems); //make the loop of items include all the subcollection items
    $total_results = count($allItems); // change total items count to reflect everything

Changing the theme that way is a bit messier, in that it breaks the separation between plugins and themes, but it might also be quicker. With more complicated variations on this code, though, it would also offer the possibility of creating separate sections for each work, thus distinguishing the manuscripts by their collection in the results page.

The theme hack works perfectly for me !
I keep in mind that the plugin hack may be a better long term solution though.

Thank you.