Problem
I'd like to have multiple featured collections displayed on the front page.
Solution 1
It looks at first like you could just call
display_random_featured_collection()
as many times as you need, but this has two problems. First, it will output a header multiple times, too. Second, unless you have many many featured collections, chances are pretty good that you will end up showing duplicates.
Instead, then, we'll need to create our own function to do things in a way that mimics that function. Here it is in its original form:
function display_random_featured_collection() { $featuredCollection = random_featured_collection(); $html = '<h2>Featured Collection</h2>'; if ($featuredCollection) { $html .= '<h3>' . link_to_collection($collectionTitle, array(), 'show', $featuredCollection) . '</h3>'; if ($collectionDescription = collection('Description', array('snippet'=>150), $featuredCollection)) { $html .= '<p class="collection-description">' . $collectionDescription . '</p>'; } } else { $html .= '<p>No featured collections are available.</p>'; } return $html; }
Notice that it starts by calling another function, random_featured_collection(), that just gets a collection, but does not return any html for display. We'll need to chase down how that function works, too, because we really want multiple featured collections, not just one.
random_featured_collection ultimately depends on some deeper-workings of Omeka, in its features for querying the database. There is a class called CollectionTable that does the job with this method:
public function findRandomFeatured() { $select=$this->getSelect()->where("c.featured=1")->order("RAND()")->limit(1); return $this->fetchObject($select); }
And so, we need to recreate what happens here in our new replacement for display_random_featured_collection().
It's pretty clear that the reason we only get one collection back is due to that "limit(1)" part at the end. So, we need to recreate how Omeka gets this data from the database, and make the little adjustment.
Whenever we need to get data from a specific table, the Omeka way is to get a reference to the database itself, then to the table:
$collectionTable = get_db()->getTable('Collection');
Now we can follow the model of findRandomFeatured()
$collectionTable = get_db()->getTable('Collection'); $select = $collectionTable->getSelect()->where("c.featured = 1")->order("RAND()")->limit(3); $featuredCollections = $collectionTable->fetchObjects($select);
So, we've bypassed the usual function for getting a random featured collections by going straight to the database tables.
From there, we just need to rewrite the rest of display_random_featured() to loop through the collections and display them instead of just showing the one. Here's the complete code for the function:
function my_theme_display_random_featured_collections() { $collectionTable = get_db()->getTable('Collection'); $select = $collectionTable->getSelect()->where("c.featured = 1")->order("RAND()")->limit(3); $featuredCollections = $collectionTable->fetchObjects($select); $html = '<h2>Sample Collections</h2>'; if(count($featuredCollections) != 0) { foreach($featuredCollections as $featuredCollection) { if ($featuredCollection) { $html .= '<h3>' . link_to_collection($collectionTitle, array(), 'show', $featuredCollection) . '</h3>'; if ($collectionDescription = collection('Description', array('snippet'=>150), $featuredCollection)) { $html .= '<p class="collection-description">' . $collectionDescription . '</p>'; } } } } else { $html .= '<p>No featured collections are available.</p>'; } return $html; }