opencartopencart-module

Create an admin menu in Opencart 4


In Opencart4 I am creating a new module. Installed the module and working fine from accessing Admin-> extensions -> modules -> module name -> (then edit).

Now I would like to add a new menu item under the "Catalogue" menu in the admin part.

I have done the following

In module's controller I added

$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('MY_EVENT');
$this->model_setting_event->addEvent('MY_EVENT', "",'admin/view/common/column_left/before', 'extension/testimonials/module/testimonials/ADDTOADMINMENU');

ADDTOADMINMENU function in the module's controller looks like

public function ADDTOADMINMENU(&$route, &$data){

        /**
        * Check if current logged in user has permission to access that link
        * Replace "extension/module/MY_EXTENSION" with your target path
        * This check can very well be ignored/deleted...
        **/
    echo "Here";
    exit;
        if ($this->user->hasPermission('access', 'extension/testimonials/module/testimonials')) {
            $my_menu_entry = array(
                'id'       => 'menu-MY_EXTENSION',
                'icon'     => 'fa-check',
                'name'     => 'My menu entry',
                'href'     => $this->url->link('extension/testimonials/module/testimonials', 'user_token=' . $this->session->data['user_token'], true),
                'children' => array()
            );
    
            $target_menu_id      = 'menu-catalog';
            $target_submenu_href = $this->url->link('catalog/product', 'user_token=' . $this->session->data['user_token'], true);
    
            $new_menu = array();
    
            foreach( $data['menus'] as &$menu ) {
    
                if( $menu['id'] == $target_menu_id ) {
    
                    $new_submenu = array();
    
                    foreach( $menu['children'] as $submenu ) {
                        if( $submenu['href'] == $target_submenu_href ) {
                            $new_submenu[] = $my_menu_entry;
                            $new_submenu[] = $submenu;
                        } else {
                            $new_submenu[] = $submenu;
                        }
                    }
                    $menu['children'] = $new_submenu;
                    $new_menu[] = $menu;
                } else {
                    $new_menu[] = $menu;
                }
            }
    
            $data['menus'] = $new_menu;
        }
    }

But not even entering in this function. Is the correct way I am following ?

Please guide.


Solution

  • try this:

    $this->model_setting_event->addEvent('MY_EVENT', "",'admin/view/common/column_left/before', 'extension/testimonials/module/testimonials/ADDTOADMINMENU');
    

    replace with:

    $this->model_setting_event->addEvent('MY_EVENT', "",'admin/view/common/column_left/before', 'extension/testimonials/module/testimonials|ADDTOADMINMENU');
    

    I have not checked, but OC have changed they framework a bit.