Magento CE 1.6.2.0 – Getting Current Admin Username in Body Class or ID

adminhtmlce-1.6.2.0

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.

Best Answer

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;
}
Related Topic