Get containing Area handle in block type controller.php

Permalink 1 user found helpful
I am creating a block type and am trying to display different data based on the Area that the block is contained in. To do this I need the Area handle. So my question is, how do I get the area handle from the controller.php file of my block type?

View Replies: View Best Answer
jasteele12 replied on at Permalink Reply
jasteele12
This should work (but untested):
$blockView = new BlockView();
$blockView->setBlockObject($this->getBlockObject());
$areaName = $blockView->block->getAreaHandle();


Please let me know if that works :)

John
wworsley2 replied on at Permalink Reply
Thanks John, I gave it a shot and got this error message after placing it in my view() method in the controller.php file:

Fatal error: Cannot access protected property BlockView::$block

Any ideas?
jasteele12 replied on at Permalink Best Answer Reply
jasteele12
Maybe you can't do that from the controller.

But it sounds like you want it from the view anyway, which this should work:
$areaName = $this->block->getAreaHandle();
wworsley2 replied on at Permalink Reply
You sir are a genius! Thanks for the help!!
yolk replied on at Permalink Reply
yolk
It might be worth noting here, as I was caught out by this, that this method will not work for Stacks, as the same area handle is always returned.
jordanlev replied on at Permalink Reply
jordanlev
if ($this->block->isBlockInStack()) {
   $area_handle = $this->block->c->getStackName();
} else {
   $area_handle = $this->block->getAreaHandle();
}