For the module I am developing I would like to add a new menu button in the control panel. I could do that manually, but I was wondering if there is a standard EE function to add a new menu button when the module is being installed?
1 Answer
There isn't a standard function, but there is a standard event hook, so within your module you can create an extension too, and use this to input your menu item. This way when you install the module, you can install the extension as well and job done.
Check out the below, its a strip down of an extension I use to add items to the admin menu, as I have it to hand it might provide a short cut for you:
In the below the premise is we hook cp_menu_array
on a fucntion called add_menu_items
, this method recieves 1 parameter, the CP menu as a multi-dimentional array. Re-order, add to, remove from the array as much as you like, remember to return the array and your done.
class MY_admin_menu_ext {
var $name = 'Admin Menu Manager';
var $version = '1.0';
var $settings_exist = 'n';
var $docs_url = '';
var $settings = array();
var $EE;
/**
* Extension Constructor
*/
function __construct($settings = array()){
$this->EE =& get_instance();
$this->settings = $settings;
}
// - Extensions -----------------------------------------------------------
/**
* Activate Extension
*/
function activate_extension() {
$this->settings = array();
$this->EE->db->insert('extensions', array(
'class' => __CLASS__,
'hook' => 'cp_menu_array',
'method' => 'add_menu_items',
'settings' => serialize($this->settings),
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
));
}
/**
* Update Extension
*/
function update_extension($current = FALSE) {
if (! $current || $current == $this->version) { return FALSE; }
$this->EE->db->where('class',__CLASS__);
$this->EE->db->update('extensions', array('version' => $this->version));
}
/**
* Disable Extension
*/
function disable_extension() {
$this->EE->db->where('class', __CLASS__);
$this->EE->db->delete('extensions');
}
// - Hooks -----------------------------------------------------------------
//modify the control panel menu
function add_menu_items($menu){
//Add General Content Settings Link to the Content Menu after 'Files', before hr & overview
$i = 0;
$newMenu = array();
foreach ( $menu['content'] as $key => $item ){
$newMenu[$key] = $item;
if ( $i == 2 ){
$newMenu['global_settings'] = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=DESIRED-MODULE';
}
$i++;
}
$menu['content'] = $newMenu;
return $menu;
}
}
-
Thank you for your detailed answer. This was exactly what I was looking for. Commented Jun 20, 2015 at 13:19
-
It would be useful to mention which EE version this code applies to.– laurentCommented Feb 1, 2019 at 5:25
-
Sure, was version 2 of EE, I think this was the defacto version back in 2015.– BlatantCommented Feb 2, 2019 at 21:03