Block on mutiple pages

Permalink
I have created a blocktype where i can add an infinite amount of items to each block instance.

But now i want to display the latest item from a block instance on another page.

How can I do this?

 
jordanlev replied on at Permalink Reply
jordanlev
Add a new public method to your block type's controller.php file. For example:
public function getLatestItemContent() {
  $content = ... //You need to put code in here that gets the latest item's content
  $return $content;
}


Then on the other page where you want to show the recent item from a different page, do this:
$content = '';
$page = Page::getByPath('/path/to/page/with/block/on/it');
$blocks = $page->getBlocks('Main'); //or the name of the area the block is in
foreach ($blocks as $b) {
  if ($b->btHandle == 'your_custom_block_handle') {
    $content = $b->getInstance()->getLatestItemContent();
    break;
  }
}
echo $content;
rklomp replied on at Permalink Reply
So i would probably have to make another blocktype? And how would i be able to select wich block instance to use?
jordanlev replied on at Permalink Reply
jordanlev
There are a few different ways to do it -- you could create another blocktype, or you could make a custom template for the page list block, or you could code this into a page type template. It really depends on what the situation is for your particular site.

As for how to select which block instance to use...
My code just uses the first block of your custom blocktype that it comes across. My assumption is that since you said each blocktype has an infinite amount of items, then there would only be one of them on a page. But if that's not the case -- you tell me: how do you want to choose? Do you want it to choose the first block on the page? The last block on the page? Or do you want a manual choice that you can make when setting this up? Or do you want to mark the custom block itself as being worthy of getting syndicated to other pages?