2

I'm trying to add an extra js file to an admin block and I would like to do that by php code.

Scenario
My block is a tab block for admin edit form so it extends Mage_Adminhtml_Block_Widget_Form and implements Mage_Adminhtml_Block_Widget_Tab_Interface

My approach
In the tab block class __prepareLayout() method I have added this line:

 $this->getLayout()->getBlock('head')->addJs('folder/gallery.js');

This should be ok but actually is not importing any js.

theB
  • 6,450
  • 1
  • 28
  • 38
WonderLand
  • 5,494
  • 7
  • 57
  • 76
  • possible duplicate of [How to add JS programmatically in Magento?](http://stackoverflow.com/questions/11937348/how-to-add-js-programmatically-in-magento) – benmarks Aug 16 '12 at 12:09
  • I think the problem is similar: ' While your code is successfully updating the head block class instance, it's happening after output has been generated from that instance' this sound as a confirm of what I feel is the problem. – WonderLand Aug 16 '12 at 12:20
  • I'm guessing if there is any code side solution – WonderLand Aug 16 '12 at 12:22

2 Answers2

4

Why not do this using layout XML? Ideally you would specify an adminhtml layout update file for your module, but you can add a local.xml file to the adminhtml/default/default/layout folder and in there do the normal <reference name="head"><action method="addJs">...</reference> call.

benmarks
  • 23,384
  • 1
  • 62
  • 84
  • Your suggestion is not bad (local.xml) however I need this for a module so using that file could lead to a conflict during module installation if this file already exist. Actually I have solved by myself ( look my answer ) – WonderLand Aug 16 '12 at 13:45
  • I don't want to use layout because I'm not using it in my module and I don't want to change the code just for 1 js file. (kind of laziness ;) However layout would be the best approach – WonderLand Aug 16 '12 at 13:46
  • Tabs are rendered as part of an AJAX request which is why your directive is not working as you intend. You are going to have to use layout XML to add your js file to the full action name of the route. – benmarks Aug 16 '12 at 14:11
  • Got your point ... I had to go back to the form block to solve it, so I have avoided the Layout – WonderLand Aug 16 '12 at 16:46
0

As suggested by @benmarks this is a particular situation but similar to the one described How to add JS programmatically in Magento?

The problem is the same 'head' block is already rendered.

I found a solution for this particular case ( add js in a edit tab form block in backend ):

to solve this I have added the above line in the _prepareLayout() method of the block that extends Mage_Adminhtml_Block_Widget_Form

Community
  • 1
  • 1
WonderLand
  • 5,494
  • 7
  • 57
  • 76