How to display only the Inventory Tab in admin Product edit section?
-
16-10-2019 - |
문제
The Product edit section (Catalog->Manage Products -> edit) I want to display only the Inventory tab for a particular admin user role.
For instance an admin user with "test" as user role should only see Inventory tab while trying to edit a product.
Currently I use core_block_abstract_prepare_layout_after
event to add a custom tab.
My observer function is as follows:
public function addTestBlock($observer){
$block = $observer->getEvent()->getBlock();
$product = Mage::registry('product');
if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs && $this->_canAddTab($product)){
$block->addTab('myentity', array(
'label' => Mage::helper('mymodule')->__('Test'),
'url' => Mage::helper('adminhtml')->getUrl('adminhtml/marketplace_tendor_catalog_product/tendors', array('_current' => true)),
'class' => 'ajax',
));
}
return $this;
}
I have planned to use
adminhtml_catalog_product_edit_prepare_form
event to display only the "Inventory" tab for test admin user.
- Can I do this using event observer?
- If yes, How?
Can anyone guide me pls?
[EDIT: ]
as suggested by mpaepper I used controller_action_layout_generate_blocks_after
event and my observer function was like below:
public function vendorLayout($evt){
$tabBlock =$evt->getEvent()->getBlock('product_tabs');
$tab_ids = $tabBlock->getTabsIds();
foreach ($tab_ids as $tab){
if($tab != 'inventory'){
$tabBlock->removeTab($tab);
}
}
}
- checking the user role is not a problem I can add it later.
- removing the "General" tab results in the following.
- If i allow "General" (group_4) and "Inventory" (inventory) tabs, then it's working fine, but the requirement is only to display Inventory tab.
nothing happens if I click on that tab, I think the js script isn't loading. what am i doing wrong?
해결책
I used core_block_abstract_prepare_layout_after
event and the observer was as follows:
public function vendorLayout($evt){ // for removing unallowed tabs and buttons from product information edit page.
$tabBlock = $evt->getEvent()->getBlock();
if ($tabBlock instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs) {
$tab_ids = $tabBlock->getTabsIds();
$tabs_to_show = array('inventory');
foreach ($tab_ids as $tab){
if(!in_array($tab, $tabs_to_show)){
$tabBlock->removeTab($tab);
}
else{
$tabBlock->setActiveTab($tab);
}
}
}
elseif($tabBlock instanceof Mage_Adminhtml_Block_Catalog_Product_Edit){
$buttonsToBeRemoved = array('back_button', 'reset_button', 'delete_button', 'duplicate_button', 'save_and_edit_button');
foreach($buttonsToBeRemoved as $button){
$tabBlock->unsetChild($button);
}
}
}
first condition removes all the tabs but "inventory", second condition removes all buttons but "save".
Thanks to @mpaepper and @Alex.
다른 팁
We developed a module which can configure for each user which tabs he/she can edit in the product backend.
To remove the tabs which are not allowed, we are subscribing to the event 'controller_action_layout_generate_blocks_after'
and do something like this:
if ($customHelper->getFullActionName() === 'adminhtml_catalog_product_edit') {
if ($customHelper->isRestrictedUser()) {
$tabNames = $tabModel->getForbiddenTabs(Mage::getSingleton('admin/session')->getUser()->getId());
$tabBlock = Mage::app()->getLayout()->getBlock('product_tabs');
foreach ( $tabNames as $tabName ) {
// Removes forbidden tabs
$tabBlock->removeTab( $tabName );
}
// Added this to select the top-most tab
$firstTab = current($tabBlock->getTabsIds());
$tabBlock->setActiveTab($firstTab);
}
}
The $tabModel is our model which fetches the permissions from the database, but this is all custom code. Instead, you could just provide a method here which fetches all tab names except the one for the stock, so all others will be removed.
EDIT: $customHelper is also custom, but you will need to implement sth. similar, i.e. detect whether the current user has restricted access, because otherwise all users will have the stripped view ;)
I think this should point you in the right direction, but if you are interested in buying a fully working module which handles all this out of the box with a GUI where you can configure the tab permissions per user, then contact me.