Pull out random block content

Permalink Browser Info Environment
I've configured a custom block - let's call it "project_intro" and I'm now wanting to pull out a single entry at random (block content) on a subset of pages by hard-coding the block into the page (doesn't need to be editable - it's editable on it's main project page).

It'll basically show a random project on a series of pages.

Something along the lines of

<?php
$project = BlockType::getByHandle('project_intro');
// get random entry
$project->render('view');
?>

Regards,

Christiaan

Type: Discussion
Status: New
disegno
View Replies:
jordanlev replied on at Permalink Reply
jordanlev
When you say "single entry at random", do you mean pull up the entire content of one block from a bunch of random pages, or do you mean the entire contents of one block from all the blocks on one page, or do you mean one small piece of content from the entire contents of one single block? Or something else?
disegno replied on at Permalink Reply 1 Attachment
disegno
Yes, entire content of one block from a bunch of random pages fits best.

We have project pages with a project summary (Designer Content block) at the top of the page. I wish to use one of these random project summary blocks throughout the site to promote projects, linking back to the main charity project page.

See attached as an example main project page - the project summary (image/yellow area) shown at the top will be displayed randomly throughout the site on other pages to promote their work.

Sorry, am used to developing with ExpressionEngine, but am trying C5 for a non-profit so still getting my head around the different way of thinking/working...

Christiaan
jordanlev replied on at Permalink Reply
jordanlev
In general, you should try to make C5 sites revolve around pages, then use the Page List block (often with a custom template) to show content from one page on another.

As always, though, it depends on your specific situation. You could designate one area on your page for this purpose, then have users put the custom designer content block (and/or any other pertinent content blocks) into that area. Then use the free Page List Teasers addon to pull out the content of that area:
http://www.concrete5.org/marketplace/addons/page-list-teasers...
The advantage of this is it doesn't require custom coding, and it works in a more generic way -- with any kind of block.

But the disadvantage is it doesn't give as much fine-grain control as you may want. Also I don't think you can set the Page List block to return random results.

So in your case you might need to use some custom page list code to make this work. Instead of trying to query for a random block, what you should do is query for a random page then grab the block in that page. It would be best to restrict this to one page type, and also make an assumption that the block will always be on that page (you could put the block in the Page Defaults of the appropriate page type via Dashboard -> Pages & Themes -> Page Types -> Defaults to help facilitate this).

//First, find a random page of a certain type (we're assuming all pages of this type have the custom block on it)...
Loader::model('page_list');
$pl = new PageList;
$pl->filterByCollectionTypeHandle('your_page_type_handle');
$pl->sortBy('RAND()', 'asc');
$pages = $pl->get(1);
$page = $pages[0];
//Now that we have the page, grab the custom block
$blocks = $page->getBlocks('Main'); //<--or whatever area the block is in
foreach ($blocks as $block) {
    if ($block->getBlockTypeHandle() == 'project_intro') {
        $block->display();
        break; //now that we found the custom block, we can stop looping through the other blocks in the area
    }
}


Hope that helps!

-Jordan
disegno replied on at Permalink Reply
disegno
Wow, thank-you so much - that's perfect. Much appreciated.

Christiaan

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.