Access all blocks in a Scrapbook?

Permalink 1 user found helpful
I've been searching the forums for over an hour and couldn't find this... so perhaps someone can enlighten me.

How do you access all the blocks in a particular scrapbook?

I'd like to be able to access a scrapbook's content from a theme I'm building.

I know a couple add-ons exist, but I really don't need a full-block; I'd just like to know how to grab all the blocks of a scrapbook.

Thanks!

glockops
 
glockops replied on at Permalink Reply
glockops
I found enough information to start working on this in: /concrete/controllers/dashboard/scrapbook/controller.php
ijessup replied on at Permalink Best Answer Reply
ijessup
Assuming you want an array of block objects:
$scrapbookHelper = Loader::helper('concrete/scrapbook');
foreach($scrapbookHelper->getAvailableScrapbooks() as $sb) {
   $scrapBooks[$sb["arHandle"]] = new Area( $sb["arHandle"] );
   $globalScrapbookC = $scrapbookHelper->getGlobalScrapbookPage(); 
   $scrapBooks[$sb["arHandle"]] = $scrapBooks[$sb["arHandle"]]->getAreaBlocksArray( $globalScrapbookC ); 
}
This will give you an array of all the custom scrap books (not personal ones).

So say you have a custom scrap book named "Layout" and you want to display all the blocks in that scrap book.
foreach(scrapBooks["Layout"] as $blocks){
   $bv = new BlockView();
   $bv->render($blocks, "scrapbook")
}
glockops replied on at Permalink Reply
glockops
Thank You!
c5mix replied on at Permalink Reply
I'm trying to implement this and cant seem to get it working. Basically, what I want to do is this: If there is a scrapbook named 'Feature' output all the blocks in that scrapbook within an unordered list (each block within it's own <li>). If there is no scrapbook named 'Feature' then instead output a div with other content. Any help would be much appreciated!
glockops replied on at Permalink Reply
glockops
Using ijessup's code above, you should be able to modify the following to make it work for you.
<?php
// Load the scrapbook helper
$scrapbookHelper = Loader::helper('concrete/scrapbook');
// Loop through all custom scrapbooks and insert them into an array.
// $scrapBooks is an array holding all the custom scrapbooks as arrays holding all their blocks.
foreach($scrapbookHelper->getAvailableScrapbooks() as $sb) {
   $scrapBooks[$sb["arHandle"]] = new Area( $sb["arHandle"] );
   $globalScrapbookC = $scrapbookHelper->getGlobalScrapbookPage(); 
   $scrapBooks[$sb["arHandle"]] = $scrapBooks[$sb["arHandle"]]->getAreaBlocksArray( $globalScrapbookC ); 
}
// For debugging, shows total custom scrapbooks
echo count($scrapBooks);
// If the "Featured" scrapbook is an array output all the blocks
if(is_array($scrapBooks["Featured"])) {
   // If featured scrapbook has blocks, output them as an unordered list
juliandale replied on at Permalink Reply
juliandale
Hi glockops,

Many thanks for this code, it's been very useful for what I've been working on. I had to make a minor tweak to get it to work, perhaps due to the latest C5 release, changing this:
$bv = new BlockView();
$bv->render($blocks, "scrapbook");


to this:
$bv = new BlockView();
$bv->setBlockObject($blocks);
$bv->render($blocks, "view");


I wondered if it was possible to extend the code to allow the blocks within a scrapbook to be sorted by name (or some custom attribute) rather than just list them in the default order that they appear within the dashboard? I know you can re-order the blocks within the dashboard, but it may be impractical if there are a lot of blocks in the list, and also it'd be handy to do this within the code where there may need to be two or three ways of ordering the blocks.

Another extension to this that I'd like to explore would be to apply a filter to the blocks displayed, for example only show blocks beginning with the letter A, show blocks of a specific type, or only show blocks with a custom attribute set to 'special'?

I'm pretty new to PHP and C5, so any help with this would be much appreciated.
hereNT replied on at Permalink Reply
hereNT
This might help you out a little bit. I'm grabbing only one scrapbook, but then outputting a random block instead of putting them into a unordered list. You could pretty easily update it for what you need:

<?php
defined('C5_EXECUTE') or die("Access Denied.");
$scrapbookHelper = Loader::helper('concrete/scrapbook');
$scrapbookPage = $scrapbookHelper->getGlobalScrapbookPage();
$scrapbookArea = new Area("Random Inside Banners");
$bannerBlocks = $scrapbookArea->getAreaBlocksArray($scrapbookPage);
$totalBlocks = count($bannerBlocks) - 1;
$i = rand(0, $totalBlocks);
$bannerBlock = $bannerBlocks[$i];
?>
<div id="static-banner">
     <?php $bannerBlock->display();?>
</div>
ScottC replied on at Permalink Reply
ScottC
very useful Isaac, I just did something like this using the guidance code above.
c5mix replied on at Permalink Reply
Scott,
Mind sharing the code as to how you did it?
kirkroberts replied on at Permalink Reply
kirkroberts
I just added a block to the marketplace that displays an entire scrapbook.
http://www.concrete5.org/marketplace/addons/scrapbook-display-basic...

You can use it as a basis for manipulating a scrapbook's contents, or just grab the code you need out of it.

This thread was *extremely* helpful in getting me what I needed to realize the block, so thanks ijessup and glockops!