在 Opencart4 中我正在创建一个新模块。安装模块并通过访问正常工作
Admin-> extensions -> modules -> module name -> (then edit)
。
现在我想在管理部分的“目录”菜单下添加一个新的菜单项。
我已经做了以下事情
在模块的控制器中我添加了
$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
模块控制器中的功能如下
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;
}
}
但连这个功能都没有进入。我遵循的方法正确吗?
请指导。
试试这个:
$this->model_setting_event->addEvent('MY_EVENT', "",'admin/view/common/column_left/before', 'extension/testimonials/module/testimonials/ADDTOADMINMENU');
替换为:
$this->model_setting_event->addEvent('MY_EVENT', "",'admin/view/common/column_left/before', 'extension/testimonials/module/testimonials|ADDTOADMINMENU');
我还没有检查过,但是 OC 稍微改变了他们的框架。
解决方案:
更换
$this->model_setting_event->addEvent('MY_EVENT', "",'admin/view/common/column_left/before', 'extension/testimonials/module/testimonials/ADDTOADMINMENU');
与
$dataEvent = [
'code' => 'MY_EVENT',
'description' => '',
'trigger' => 'admin/view/common/column_left/before',
'action' => 'extension/testimonials/module/testimonials|ADDTOADMINMENU',
'status' => true,
'sort_order' => 0,
];
if (version_compare(VERSION, '4.0.1.0', '>=')) {
$this->model_setting_event->addEvent($dataEvent);
} elseif (version_compare(VERSION, '4.0.0.0', '>=')) {
$this->model_setting_event->addEvent($dataEvent['code'], $dataEvent['description'], $dataEvent['trigger'], $dataEvent['action'], $dataEvent['status'], $dataEvent['sort_order']);
}else {
/* compatibility Opencart 3 */
$dataEvent['trigger']=str_replace("|","/",$dataEvent['trigger']);
$this->model_setting_event->addEvent($dataEvent['code'], $dataEvent['trigger'], $dataEvent['action'], $dataEvent['status'], $dataEvent['sort_order']);
}
现在您可以使用您的代码来更新 Opencart 3 上的管理菜单
并非所有菜单/子菜单项都会有 ID。最好使用菜单文本来标识菜单的放置位置。有时您可能想将菜单放置在子菜单的深处。这是使用 array_splice 的更高效的代码。
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...
**/
if ($this->user->hasPermission('access', 'extension/MY_EXTENSION/module/MY_EXTENSION')) {
$newMenuItem = array(
'name' => 'My menu entry',
'href' => $this->url->link('extension/MY_EXTENSION/module/MY_EXTENSION', 'user_token=' . $this->session->data['user_token'], true),
'children' => array()
);
// Iterate through the top-level menus
foreach ($data['menus'] as &$menu) {
if (isset($menu['name']) && $menu['name'] === 'Top Level Menu') {
// Iterate through 'Top Level Menu' menu's children to find 'Sub Menu'
foreach ($menu['children'] as &$subMenu) {
if (isset($subMenu['name']) && $subMenu['name'] === 'Sub Menu') {
// Iterate through 'Sub Menu' menu's children to find 'Child Menu'
foreach ($subMenu['children'] as $key => $child) {
if (isset($child['name']) && $child['name'] === 'Child Menu') {
// Insert new item before 'Child Menu'
array_splice($subMenu['children'], $key, 0, [$newMenuItem]);
return; // Exit function after inserting
}
}
}
}
}
}
}
}