Block styles not included when using block->display()

Permalink
I noticed that when inserting a block like this:
public function view() {
   $stack = Stack::getByID(140);
   $block = $stack->getBlocks()[0];
   $this->set('block', $block->display());                  
}

Then the block styles added by clicking on the block and selecting 'Design' aren't added to the block. If adding the stack to a page normally, they are. Anyone know how to include those block styles programmatically?

ConcreteConversion
 
Mainio replied on at Permalink Reply
Mainio
Here's how to output the custom styling for the stack:
$this->addHeaderItem('<style type="text/css">' . $stack->outputCustomStyleHeaderItems(true) . '</style>');


This needs to be in your controller's "on_start" (for single pages) or "on_page_view" (fo blocks) method.

And to wrap the correct CSS classes/id around the block area (so that the styles get applied), do something similar to this file:
/concrete/elements/block_area_header_view.php
enlil replied on at Permalink Best Answer Reply
enlil
Thanks for this mainio. In regards to my PM's and to help others, in my case, i added the following to my on_page_view($page) function in my block controller to get the custom block styles:
(a bit of code borrowed from core_stack_display)
$blocks = $stack->getBlocks();
   foreach($blocks as $b) {
      $btc = $b->getInstance();
      if('Controller' != get_class($btc)){
         $btc->outputAutoHeaderItems();
      }
      $csr = $b->getBlockCustomStyleRule();
      if (is_object($csr)) {
         $styleHeader = '#'.$csr->getCustomStyleRuleCSSID(1).' {'. $csr->getCustomStyleRuleText(). "} \r\n";  
         $btc->addHeaderItem("<style type=\"text/css\"> \r\n".$styleHeader.'</style>', 'VIEW');
      }
      $btc->runTask('on_page_view', array($page));
         }
ConcreteConversion replied on at Permalink Reply
ConcreteConversion
Thanks to you both! This is just what I'm looking for. :)
enlil replied on at Permalink Reply
enlil
Glad it worked out for you. The snip above is now in use within my Enlil Genetic Stacks package available in the marketplace. PM me if either of you are interested in checking it out.