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:
Thanks
This code did not work unfortunately:
$b = Block::getByID($this->bID); $this->set('areaHandle', $b->getAreaHandle());
Thanks
This might work:
Thanks, but then I get this error:
This is my Function in the Controller:
What have I done wrong?
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?
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:
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?).
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?).
The code does not work either, unfortunately. "Block:: getByID" returns an empty object.
Only when I enter the area, I get a result.
This is a pity, then I have to solve the whole thing a bit complicated.
Nevertheless, thanks for your help.
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.
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:
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();
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; }
thx
Thanks! This helped me a lot too.
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?)
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?)
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'.
Has anyone got a similar tip for getting the area from a block controller?
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.
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.
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:
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).
$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).
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.
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.
it gets sniffed from the request (well a $_GET) to the tools file inside the modal popup by the block type id.
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?
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?
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.
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.
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.
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.
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
$this->block->c->cPath
It'll return something like: /!stacks/footer-column-two
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!!
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!!