![](/img/trans.png)
[英]Is there a way to add menu items programmatically from my component in Joomla?
[英]How to add new admin menu items without reinstalling component in joomla?
我一直在為Joomla 1.7.x開發一個組件,在開發過程中我需要在管理菜單中添加新的組件菜單項,通過在Joomla中向DB中的組件表添加新行很容易1.5次,但現在添加起來很復雜由於Joomla 1.7中的數據庫結構更改,通過向菜單表添加新行來顯示菜單項
有沒有重新安裝組件的簡單方法?
謝謝
我發現最簡單的方法:
$table = JTable::getInstance('menu');
$data = array();
$data['menutype'] = 'main';
$data['client_id'] = 1;
$data['title'] = 'ITEM TITLE';
$data['alias'] = 'com-component-name';
$data['link'] = 'index.php?option=com_component_name&view=default';
$data['type'] = 'component';
$data['published'] = '0';
$data['parent_id'] = '117'; // ID, under which you want to add an item
$data['component_id'] = '10026'; // ID of the component
$data['img'] = 'components/com_component_name/assets/images/icon.png';
$data['home'] = 0;
if (
!$table->setLocation(117, 'last-child') // Parent ID, Position of an item
|| !$table->bind($data)
|| !$table->check()
|| !$table->store()
){
// Install failed, warn user and rollback changes
JError::raiseWarning(1, $table->getError());
return false;
}
刪除:
$table->delete(120); // item ID
$table->rebuild();
基於http://docs.joomla.org/Using_nested_sets#Adding_a_new_node
以下是我提出的一些SQL查詢,它們完成了訣竅(僅顯示了相關部分):
SET @lastRgt := (SELECT rgt + 1 FROM #__menu WHERE alias="alias-of-preceding-menu-item");
UPDATE #__menu SET rgt=rgt+2 WHERE rgt > @lastRgt;
UPDATE #__menu SET lft=lft+2 WHERE lft > @lastRgt;
INSERT INTO #__menu (menutype, title, alias, path, link, type, published, parent_id, level, component_id, img, client_id, params, access, lft, rgt)
VALUES(..., @lastRgt+1, @lastRgt+2);
在Joomla 2.5上為我工作。
Admit的答案需要Joomla 3.x的更新
我確定它對於較舊的Joomla版本是正確的,這就是為什么我不編輯它。
經過進一步的研究和編輯,這對我有用。
$table = JTableNested::getInstance('Menu');
$data = array();
$data['menutype'] = 'main';
$data['client_id'] = 1;
$data['title'] = 'ITEM TITLE';
$data['alias'] = 'com-component-name';
$data['link'] = 'index.php?option=com_component_name&view=default';
$data['type'] = 'component';
$data['published'] = '0';
$data['parent_id'] = '117'; // ID, under which you want to add an item
$data['component_id'] = '10026'; // ID of the component
$data['img'] = 'components/com_component_name/assets/images/icon.png';
$data['home'] = 0;
$table->setLocation(117, 'last-child') // Parent ID, Position of an item
if (!$table->bind($data) || !$table->check() || !$table->store()) {
// Install failed, warn user and rollback changes
JError::raiseWarning(1, $table->getError());
return false;
}
Joomla 1.6+菜單項存儲在#__menu表下,管理菜單中有一個名為“main”的特殊菜單類型。
找到主要組件管理員菜單項的ID。 您可以通過將parent_id列聲明為主菜單項的ID並將level列設置為2來添加其子項。
您將遇到的唯一其他問題是采用嵌套集(lft和rgt列)。 這是處理父子關系和菜單項排序的更好方法。 我不確定在這個階段是否使用了parent_id或lft / rgt,但是它們都被填寫了。
要添加新項目,您必須將所有lft / rgt值“分流”為菜單項,其值大於或等於您希望添加菜單項的位置。 這應該包括您父項的rgt。 如果您的父項目沒有子項,則新項目的lft將是父項左側的值+ 1.新項目的rgt的值將是父項的lft + 2。
有一點需要注意的是lft和rgt是編號適用於每個菜單項(前端和后端),所以不能正確執行它可能會破壞整個菜單層次結構。 我認為這就是仍然使用parent_id列的原因,並且管理區域中有一個選項可以“重建”菜單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.