This post replies to BobRay’s request to publish a working RandomQuotes solution.
First off, I renamed it in the singular, as "RandomQuote," because it returns just one quotation at a time. Do as you like, but this name seems to me more appropriate semantically.
Second, I took BobRay’s advice (See above post.) to pick a random selection from the Resources rather than explicitly calculate a random index into them. The Snippet works either way, but I prefer BobRay’s semantics.
Third, I also took BobRay’s advice to replace "getActiveChildren" with "getAllChildren." The former results in the Snippet returning "No entries found" if the child Resources are not checked as "published."
So here is the resulting Snippet code:
<?php
// RandomQuote
// Pulls one quotation at random from Resources in a Folder.
// Original by sottwell.
// Posted by web_designer.
// Dated 25th Sepember 2006.
// Modified 28th February 2010 by Halfnium.
$resourceparent = $quoteid; // the folder that contains quote documents
$output = ''; // initialise the quote variable
$resource = $modx->getAllChildren($resourceparent,'createdon', 'DESC', $fields='content');
$limit=count($resource);
if($limit<1) {
$output .= "No entries found.<br />";
return $output;
}
$random = array_rand($resource);
$output .= $resource[$random]['content']; // get a random quote
return $output;
?>
Use: As the Snippet’s original author advised (
http://modxcms.com/extras/package/368),
* Create a new Snippet. Name it RandomQuote. Past the code above into it.
* Create a new Folder to serve as parent for the quotation Resources. Non-check the "Show in menu" box.
* Create any number of quotation Resources as children of the above Folder. Settings for these:
- Template: Blank
- Show in menu: Non-checked
- Log visits: Non-checked
- Searchable: Non-checked
- Cacheable: Non-checked
- Published: See TIP below.
- Others as you please, and there is no need to provide names etc.
* Call RandomQuote in your Page in uncached form, as
[!RandomQuote? "eid=`123`!]
where "123" means the ID of the Folder containing the quotation Resources.
TIP: Verifying the format of a quotation newly added to several already in the "eid Folder is a pain, since it can take many clicks of the browser’s reload button to bring the new Resource around in the random rotation. In that case, temporarily modify RandomQuote by substituting "getActiveChildren" for "getAllChildren." (As mentioned above, this results in non-published quotations no longer being found.) Create a new quotation and check it off as "published." Upon rendering your Page, only that new quotation becomes available, allowing you to see it immediately.
Once you have verified the new quotation’s format, make it disappear by non-checking it as "published."
Once you’ve added all your quotation Resources to the "eid folder, switch the method back to "getAllChildren."