5.7 BlockType->render() when hard coding block with custom template

Permalink
Hi,

So I have been playing with 5.7 (5.7.0.1) and hard coding a block into my page type. In this instance it is the tags block. All works fine until I try and apply a custom template.

Before 5.7 the code would have been like this

$p = Page::getByID($c->getCollectionParentID());
$tags = BlockType::getByHandle('tags');
$tags->controller->targetCID = $p->getCollectionID();
$tags->render('templates/my_template/view');


However this doesn't work in 5.7 and from what I can see it is because unless there is an instance of a block it will just ignore what you have passed to the render method and just loads view.php in the blocks core directory. If you want to take a look it is the setupRender method in the BlockView class

So instead I used this method
$p = Page::getByID($c->getCollectionParentID());
$tags = BlockType::getByHandle('tags');
$tags->controller->targetCID = $p->getCollectionID();
$tags->controller->blockViewRenderOverride = 'templates/my_template/view';
$tags->render();


This works! unless you want to keep your block view override within a package. This is because there is no package id so it doesn't go looking in the package directory but directly in the applications/blocks or concrete/blocks directories with the following error:

include(C:\server\htdocs\concrete5.7\concrete/blocks/tags/templates/my_template/view.php): failed to open stream: No such file or directory


is this a bug? or is there something I have missed?

Any help is greatly appreciated.

Thanks

kieranrushby
 
kieranrushby replied on at Permalink Reply
kieranrushby
Anyone have any ideas on this??
weetheme replied on at Permalink Reply
weetheme
You not alone!!4

I just press F5 on this site every hour..

https://github.com/concrete5/concrete5-5.7.0/blob/master/tests/legac...

so you need a block. Some other's i saw tried to add a block,but after when you check if it's already exist (already added) and get it back by name, you got error like "scrapbook not exists".. so it was a dead end..

if you have the block or you can get it by id or name here's a sample code for autonav:

$p = Page::getByID(1);
      $a = new GlobalArea('Header Navigation');
      $blocks = $a->getAreaBlocksArray($c);
      foreach ($blocks as $block){
         if ($block->getBlockTypeHandle()=="autonav"){
            $block->setCustomTemplate('cdrop.php'); // it's templates/cdrop.php -check the select option values when you set custom template manually at edit mode. I think you will need just "my_template" 
            $bv = new BlockView($block);
            $bv->render('view');
         }
      }
weetheme replied on at Permalink Reply
weetheme
about render('view')
check the c57\concrete\src\Block\View\BlockView.php > setupRender function and you will see it not means that it will render the default view, it use the custom settings if the block has any.