這是我最終用於刪除管理菜單中的條目的代碼。
首先,我創建了一個實現在一個文件中的飛行後的方法稱爲script.php的安裝腳本:
<?php
//No direct access
defined('_JEXEC) or die;');
class com_mycomponentInstallerScript{
function postflight($type, $parent){
// $parent is the class calling this method
// $type is the type of change (install, update or discover_install)
$componentName = 'myComponent'; //The name you're using in the manifest
$extIds = $this->getExtensionIds($componentName);
if(count($extIds)) {
foreach($extIds as $id) {
if(!$this->removeAdminMenus($id)) {
echo JText::_(COM_MYCOMPONENT_POSTFLIGHT_FAILED);
}
}
}
}
/**
* Retrieves the #__extensions IDs of a component given the component name (eg "com_somecomponent")
*
* @param string $component The component's name
* @return array An array of component IDs
*/
protected function getExtensionIds($component) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('extension_id');
$query->from('#__extensions');
$cleanComponent = filter_var($component, FILTER_SANITIZE_MAGIC_QUOTES);
$query->where($query->qn('name') . ' = ' . $query->quote($cleanComponent));
$db->setQuery($query);
$ids = $db->loadResultArray();
return $ids;
}
/**
* Removes the admin menu item for a given component
*
* This method was pilfered from JInstallerComponent::_removeAdminMenus()
*
* @param int $id The component's #__extensions id
* @return bool true on success, false on failure
*/
protected function removeAdminMenus(&$id)
{
// Initialise Variables
$db = JFactory::getDbo();
$table = JTable::getInstance('menu');
// Get the ids of the menu items
$query = $db->getQuery(true);
$query->select('id');
$query->from('#__menu');
$query->where($query->qn('client_id') . ' = 1');
$query->where($query->qn('component_id') . ' = ' . (int) $id);
$db->setQuery($query);
$ids = $db->loadColumn();
// Check for error
if ($error = $db->getErrorMsg())
{
return false;
}
elseif (!empty($ids))
{
// Iterate the items to delete each one.
foreach ($ids as $menuid)
{
if (!$table->delete((int) $menuid))
{
return false;
}
}
// Rebuild the whole tree
$table->rebuild();
}
return true;
}
}
接下來,我加入與組件清單中的條目,運行腳本安裝的組件後, :
<scriptfile>script.php</scriptfile>
如果我沒有弄錯,Joomla核心代碼會自動發生這種情況。我不認爲你想要什麼是可能的。你爲什麼要從管理組件列表中刪除它? – Lodder
該組件僅用作需要代理類的模塊的組件助手。使用組件是我知道實現這一目標的唯一途徑。 – Stilero