6

what magento frontend events can should I observe if I want the chance to inject blocks to the ("head" block) ?

and while in the observer's code, how do I check if the current layout has some handle (e.g. not_logged_in) set.

epeleg
  • 10,347
  • 17
  • 101
  • 151
  • OK so I found this event list http://masteringmagento.com/2010/06/events-list-in-magento-community-1-4/ and I will use controller_action_layout_render_before. Now how do I check the handles ? – epeleg Feb 15 '11 at 12:38
  • 2
    Why are you not using a layout file? – clockworkgeek Feb 15 '11 at 14:02
  • Because In my case the "new" layout changes the template and not only adds blocks. so if some preconditions are not met yet (extension is not properly configured yet) then I don't want the modification to occur. It would have been nice if the layout xml file had a "onlyIf" attribute pointing to a helper function that could then decide if the override should actually take place. – epeleg Feb 15 '11 at 18:16
  • @epeleg The "Magento Way" to do that would be create custom block classes, override rendering methods, and only call the parent method if your conditions were met. – Alana Storm Feb 15 '11 at 19:36
  • @alan can you please elaborate? suppose I want to conditionally (based on extension's config) change the look of customer_account_login I should unsetChildren in the layout xml and declare my own block. but how can I now "revert back" from the code of my own block to what used to be within the original? – epeleg Feb 16 '11 at 07:04
  • 2
    Too long for a comment: http://alanstorm.com/magento_oop_still_applies – Alana Storm Feb 16 '11 at 08:07
  • Thanks for this post. I did not think of the option to leave template as is while overriding the block and modifying it later on. I guess I will also have to make sure in this situation that any changes to behavior of the block are also dependent on the configuration. How should this be extended if you want to override the "root"? – epeleg Feb 16 '11 at 08:55

2 Answers2

4

Give the

controller_action_layout_generate_blocks_after

event a try. The Layout object and its child blocks should be instantiated by the point that event fires.

There's only ever one Layout object, and you can grab the handles in play with

// get the layout->get the updates manager->get the handles
$handles = Mage::getSingleton('core/layout')->getUpdate()->getHandles();
var_dump($handles);

If you're working on front-end code and trying to stick to magento conventions, it's probably better to add a layout.xml file to your module, and use the layout file to add your blocks. It's less fun than figuring out something new though!

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • thanks for the handles pointer. I was surprised to find that there is almost no support around this topic. i.e. no isHandleSet or something like this... – epeleg Feb 15 '11 at 18:19
  • @epeleg 1. You're surprised? You must be new to Magento :) 2. As a client programmer of the system, you're not really supposed to interact much with handles. Handles let the layout system know which XML Updates to pull from the package layout. Using them for anything else, while interesting, is probably asking for trouble down the line. – Alana Storm Feb 15 '11 at 18:35
  • 1. guilty as charged, but am am getting a better understanding of its workings with every passing day. 2. I am aware of this and basicly this is exactly what I want to do. only that I do it programatically and not decleratively, thus allowing me to toggle it on and of. 3. If I undersood properly the code I was looking at then handles are also used as part of the key for caching so I might also add a handle to invalidate the cache when my extension's status is changed. b.t.w - Is there a reasonable way to see the blocks that the layout holds given the layout object. – epeleg Feb 15 '11 at 19:41
1

I appreciate the plug on the blog, but clockworkgeek is correct. The best way to accomplish this is to use a layout file to add the blocks you need. It is also possible for those blocks to change their own rendering behavior based on arbitrary code.

If there is a reason why you cannot use layouts, please elaborate a bit in your question and we'll be happy to help.

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
Joe Mastey
  • 26,809
  • 13
  • 80
  • 104