Question

This question already has an answer here:

I have my own module in System->Configuration and want to add js and css files for it. Now it is so:

<layout version="0.1.0">
    <default>
        <reference name="head">
            <action method="addItem"><type>js_css</type><name>prototype/windows/themes/default.css</name></action>
            <action method="addCss"><name>mymodule.css</name></action>
        </reference>
    </default>
</layout>

This way files added to all Admin pages. What to do to add this files just to one page, so write not default, but mymodule_admin or smth like that?

Was it helpful?

Solution

You could uset the layout handle adminhtml_system_config_edit instead of default, and your files only under System > Configuration. The XML would look like this:

<layout version="0.1.0">
    <adminhtml_system_config_edit>
        <reference name="head">
            <action method="addItem"><type>js_css</type><name>prototype/windows/themes/default.css</name></action>
            <action method="addCss"><name>mymodule.css</name></action>
        </reference>
    </adminhtml_system_config_edit>
</layout>

If you only need these new files under a certail section, you should add your own layout handle, since Magento doesn't offer this out of the box. To be honest, I think this is not really worth the effort, but to answer your question:

Add an event observer

To achieve this, listen to the event controller_action_layout_load_before. In your config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <!-- ... -->
    <adminhtml>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <my_module_observer>
                        <class>my_module/observer</class>
                        <method>addCustomLayoutHandle</method>
                    </my_module_observer>
                </observers>
            </controller_action_layout_load_before>
        </events>
    </adminhtml>
    <!-- ... -->
</config>

The event is thrown in Mage_Core_Controller_Varien_Action in the method loadLayoutUpdates().

Add your custom layout handle

Now that you have defined your event observer, you can check if the page you are currently on corresponds to the system configuration of your module. In your event observer:

public function addCustomLayoutHandle(Varien_Event_Observer $observer)
{
    $controllerAction = $observer->getEvent()->getAction();
    $layout = $observer->getEvent()->getLayout();
    if ($controllerAction && $layout && $controllerAction instanceof Mage_Adminhtml_System_ConfigController) { // Can be checked in other ways of course
        if ($controllerAction->getRequest()->getParam('section') == 'my_module_section') {
            $layout->getUpdate()->addHandle('my_custom_handle');
        }
    }
    return $this;
}

The custom layout handle should be loaded now. You can use this new handle for your layout definition. Please not that I didn't have time to test this, hope it's working well. Just wanted to point you to a possible solution to your problem.

OTHER TIPS

Change the layout handle name. Instead of default use the default handler for your page. For example if your page url looks like this 'module/adminhtml_entity/index' (or module/adminhtml_entity/ your layout can look like this:

<layout>
    <module_adminhtml_entity_index>
        <reference name="head">
            <action method="addItem"><type>js_css</type><name>prototype/windows/themes/default.css</name></action>
            <action method="addCss"><name>mymodule.css</name></action>
        </reference>
    </module_adminhtml_entity_index>
</layout>

If your url looks like admin/module_entity/index your layout should look like this: (replace admin with adminhtml)

<layout>
    <adminhtml_module_entity_index>
        <reference name="head">
            <action method="addItem"><type>js_css</type><name>prototype/windows/themes/default.css</name></action>
            <action method="addCss"><name>mymodule.css</name></action>
        </reference>
    </adminhtml_module_entity_index>
</layout>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top