how detect current area out of a block?

Permalink 4 users found helpful
How can I find out the actual area where the current block is inserted.

This code did not work unfortunately:
$b = Block::getByID($this->bID);
$this->set('areaHandle', $b->getAreaHandle());


Thanks

teamorange
 
jordanlev replied on at Permalink Reply
jordanlev
This might work:
$b = Block::getByID($this->bID);
$a = $b->getBlockAreaObject();
$arHandle = $a->getAreaHandle();
teamorange replied on at Permalink Reply
teamorange
Thanks, but then I get this error:
Fatal error: Call to a member function getAreaHandle() on a non-object in /***/controller.php on line 35


This is my Function in the Controller:
public function getCurrentArea() {
   $b = Block::getByID($this->bID);
   $a = $b->getBlockAreaObject();
   return $a->getAreaHandle();
}


What have I done wrong?
jordanlev replied on at Permalink Reply
jordanlev
Hmm... I took a look at the core code involved, and it seems you need to pass in the collection object ($c) to the Block::getByID() function. So maybe try this:
public function getCurrentArea() {
   $c = Page::getCurrentPage();
   $b = Block::getByID($this->bID, $c);
   $a = $b->getBlockAreaObject();
   return $a->getAreaHandle();
}


If that doesn't work, I'm afraid I'm not sure what will (it might not be possible to get the area handle outside the context of the view?).
teamorange replied on at Permalink Reply
teamorange
The code does not work either, unfortunately. "Block:: getByID" returns an empty object.

Only when I enter the area, I get a result.
public function getCurrentArea() {
   $c = Page::getCurrentPage();
   $b = Block::getByID($this->bID, $c, "Teaser");
   $a = $b->getBlockAreaObject();
   return $a->getAreaHandle();
}


This is a pity, then I have to solve the whole thing a bit complicated.
Nevertheless, thanks for your help.
mesuva replied on at Permalink Best Answer Reply
mesuva
It's going to be different depending on whether you are in a block controller or view, but I've had success with using the following from within the view of a block:

$areaname = $this->block->getAreaHandle();


I've used this on the youtube block to detect what area the block is in and automatically adjust the height and width, e.g.

$areaname = $this->block->getAreaHandle(); 
if ($areaname == 'Sidebar') {
     $vWidth=258;
     $vHeight=209;
} else {
     $vWidth=565;
     $vHeight=457;
}
teamorange replied on at Permalink Reply
teamorange
thx
dwayneparton replied on at Permalink Reply
dwayneparton
Thanks! This helped me a lot too.
jordanlev replied on at Permalink Reply
jordanlev
This does look like a great technique, thank you for sharing it.

I'm curious why you didn't just put a different class around each area in the page type template and then set the widths via CSS? Perhaps it's specific to how the youtube block works (like you need to pass the width in directly to some flash vars?)
mesuva replied on at Permalink Reply
mesuva
I would have preferred to do that, but the youtube block outputs some javascript as well - it sets the height and width as part of initialising a 'swfobject'.
JohntheFish replied on at Permalink Reply
JohntheFish
Has anyone got a similar tip for getting the area from a block controller?
ScottC replied on at Permalink Reply
ScottC
I've had problems with this in the past through the api, I've usually had to drop into a few database queries to find it.

I don't have any code open for it but I've found from memory things like $this->getBlockAreaObject() and methods similar to that don't wire up correctly, this might be a bug in the core but you can find them through a few database queries.
jordanlev replied on at Permalink Reply
jordanlev
If the code works from the block view, then you should be able to make it work from the controller by getting the block view object, like so:
$bv = new BlockView();
$bv->setBlockObject($this->getBlockObject());
//Now $bv should be the same as $this was in the view, so you could do this:
$areaname = $bv->block->getAreaHandle();


That isn't tested, but it should work (assuming the block controller is actually being called from the page that it's on -- not some weird scrapbook or alias thing).
JohntheFish replied on at Permalink Reply
JohntheFish
Thanks for the ideas. After much experimenting I ran into the problem that a block doesn't exist during an add dialog (only after)(should have expected that, I ran into the same issue with ajax tasks ages ago).

For now, I have refactored so I don't need to know the area name until the view.

The remaining question, purely of academic interest at the moment, is how to tell what area a block would be added to from within the block add dialog.
ScottC replied on at Permalink Reply
ScottC
it gets sniffed from the request (well a $_GET) to the tools file inside the modal popup by the block type id.
JohntheFish replied on at Permalink Reply
JohntheFish
I was getting along quite well when using this, until I added a block into a stack.

Within a block view in a stack, $this->block->getAreaHandle() appears to always return 'Main' rather than the area the stack has been added to.

1. Is my above conclusion correct?

2. Does anyone have a way round it that is not too convoluted?
jordanlev replied on at Permalink Reply
jordanlev
Yeah, stacks are pages/collections, always in the 'Main' area of that phony stack page/collection.

When you place a stack on a page, it's actually a block (of type 'core_stack_display') that displays the stack page/collection.

I just did a cursory look through the page, collection, and stack models, as well as the core_stack_display block controller... from what I can tell there's only a one-way lookup of the stack contents from the core_stack_display block... it does not appear that a stack itself has any notion of where it's being displayed in a page.
JohntheFish replied on at Permalink Reply
JohntheFish
Thanks.

For a number of things I have built recently, and many other ideas I have on the way, life would be so much easier if a block controller could simply ask 'where am I' and get a sensible answer.

Maybe I need to look into doing some database mining and come up with a helper that answers this, rather than looking for answers in the api.
doterobcn replied on at Permalink Reply
I'm very late to the party, but i found that in the block object, you can access this property:
$this->block->c->cPath
It'll return something like: /!stacks/footer-column-two
enlil replied on at Permalink Reply
enlil
I'm writing a block I don't want to display in stacks or global areas.

Do both stacks and global areas display using the "core_stack_display" ??

Also, I'm having trouble getting "parent block type handle". As in, if the block has been placed inside block type "core_stack_display", I would have it display an error in dashboard / edit mode and display nothing in the view.

If only the Area inside stacks was something other than Main....

Any input is appreciated!!