8

In the admin backend I would like to add a class to the body tag of all admin pages which prints out the name of the admin users currently logged in.

In app/design/adminhtml/default/default/template/page.phtml

I've found this (on line 46 - CE 1.6.2.0):

<body id="html-body"<?php echo $this->getBodyClass()?'class="'.$this->getBodyClass().'"':'' ?>>

and I think that I need to use this to get the admin username:

Mage::getSingleton('admin/session')->getUser();

However, no matter what I do, I cannot seem to output the username in either the id or class.

Any ideas would be most welcome.

Thanks.

3 Answers 3

6

The cleanest way is to create an observer that adds the class to the body.
For this you can create a new module (or use an existing one. Just in case, here is how you can create one. Let's call it Easylife_AdminClass).
You will need the following files:
app/etc/modules/Easylife_AdminClass.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_AdminClass>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml />
            </depends>
        </Easylife_AdminClass>
    </modules>
</config>

app/code/local/Easylife/AdminClass/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_AdminClass>
            <version>0.0.1</version>
        </Easylife_AdminClass>
    </modules>
    <global>
        <models>
            <adminclass>
                <class>Easylife_AdminClass_Model</class>
            </adminclass>
        </models>
    </global>
    <adminhtml>
        <events><!-- event used for adding a class to the body-->
            <core_block_abstract_to_html_before>
                <observers>
                    <adminclass>
                        <type>singleton</type>
                        <class>adminclass/adminhtml_observer</class>
                        <method>addBodyClass</method>
                    </adminclass>
                </observers>
            </core_block_abstract_to_html_before>
        </events>
    </adminhtml>
</config>

app/code/local/Easylife/AdminClass/Model/Observer.php - the observer.

<?php
class Easylife_AdminClass_Model_Adminhtml_Observer{
    public function addBodyClass($observer){
        $block = $observer->getEvent()->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Page){ //check if the block is the root block,
            $admin = Mage::getSingleton('admin/session')->getUser();
            if ($admin->getId()){//check if the admin is logged in
                $block->addBodyClass($admin->getUsername());//add the class to the body.
            }
        }
        return $this;
    }
}

I recommend adding a prefix to the class you add to the body.
I mean replace $block->addBodyClass($admin->getUsername()); with $block->addBodyClass('some-prefix-'.$admin->getUsername()); since the username can be almost anything. For example you will have a surprise if you have an admin named 'root'. Here is how the root class looks in the default admin css

.root{
    position: relative;
    height: 260px;
    margin: 0;
    width: 1px;
}
4
  • 1
    This is indeed an incredibly detailed reply - thanks heaps for that. Regrettably, not being a programmer, I don't really understand all of this and when I implemented it, I got a 'There has been an error processing your request' page in the default Magento theme. Any ideas?
    – njwrigley
    Commented Jun 4, 2013 at 18:21
  • First of all clear the cache (contents of folder var/cache. If the error is still there, check the folder var/report. on one of the files in there you should find more details about the error. I really recommend asking for help from a programmer on this.
    – Marius
    Commented Jun 4, 2013 at 18:29
  • Okay - thanks for your really, really fast reply. I will get some folk that I know to look at your solution.
    – njwrigley
    Commented Jun 4, 2013 at 18:52
  • @Marius Great support :)
    – Gem
    Commented Sep 5, 2019 at 11:23
14

The simplest solution is to create a helper method which returns the currently-logged-in username.

class Ns_Mn_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function getAdminUsername()
    {
        if (Mage::getSingleton('admin/session')->getUser()->getId()) {
            return Mage::getSingleton('admin/session')->getUser()->getUsername();
        }
    }
}

Then in layout XML:

<default>
    <action method="addBodyClass" block="root">
         <css_class_name helper="ns_mn/getAdminUsername" />
    </action>
</default>
4
  • I like this solution a lot.
    – philwinkle
    Commented Jun 4, 2013 at 19:42
  • Nice solution. +1 mostly because I'm furious this didn't cross my mind (and it should have).
    – Marius
    Commented Jun 5, 2013 at 6:59
  • 1
    @Marius your solution will catch the case always unless some custom main block template is used, so I'd say it's the most thorough. I personally avoid observing block rendering events because they are generic, but your answer does everything it should (test for block type, instantiate as a singleton).
    – benmarks
    Commented Jun 5, 2013 at 13:52
  • I thought of that, but I safely assumed the risk is very low. The way the Magento admin is built kind of requires the top block to be an instance of Mage_Adminhtml_Block_Page. Of course you can replace that with an other block type but I don't see the point. And even if you replace it, either your replace it with a child class of Mage_Adminhtml_Block_Page (which is ok), or you have the risk of not having a method addBodyClass(). Either way the risk of my extension not working exists but I think it's pretty safe to take it.
    – Marius
    Commented Jun 5, 2013 at 14:12
1

In a custom module, you want to observe the preDispatch for the admin controller action that you're trying to add body classes to and use the following code:

if ($block = $this->getLayout()->getBlock('root')) {
        $block->addBodyClass((string)Mage::getSingleton('admin/session')->getUser());
}
1
  • I like Marius' incredibly detailed answer better than my own (though the premise is the same)
    – philwinkle
    Commented Jun 4, 2013 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.