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 fileapp/code/local/Easylife/AdminClass/etc/config.xml
- the configuration fileapp/code/local/Easylife/AdminClass/Model/Observer.php
- the observer.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 theroot
class looks in the default admin css