I wrote a Joomla plugin which will eventually load a library.
The path to library is a plugin parameter, as such when the path is incorrect, a message pops up in the backend, together with a link to edit the plugin parameters:
/administrator/index.php?option=com_plugins&view=plugin&client=site&task=edit&cid[]=36
See the 36 at the end? That's my plugin's id in the database (table jos_plugins).
My issue is that this id changes on installation, ie, on different installs, it would be something else. So I need to find this id programmatically.
The problem is that I couldn't find this id from the plugin object itself (as to why not, that would be joomla's arguably short-sighted design decision).
So unless you know about some neat trick, (I've checked and double checked JPlugin and JPluginHelper classes), I'll be using the DB.
Edit; Some useful links:
Guess I'll be using the wisdom from that last link...
For joomla 2.5.x and 3.x Christian's function improvement would be;
function getId($folder,$name){
$db= JFactory::getDBO();
$sql='SELECT extension_id FROM #__extensions WHERE folder ="'.$db->getEscaped($folder).'" AND element ="'.$db->getEscaped($name).'"';
$db->setQuery($sql);
if(!($plg=$db->loadObject())){
JError::raiseError(100,'Fatal: Plugin is not installed or your SQL server is NUTS.');
}else return (int)$plg->extension_id;
}
function getId($folder,$name){
$db=&JFactory::getDBO();
$sql='SELECT `id` FROM `#__plugins` WHERE `folder`="'.$db->getEscaped($folder).'" AND `element`="'.$db->getEscaped($name).'"';
$db->setQuery($sql);
if(!($plg=$db->loadObject())){
JError::raiseError(100,'Fatal: Plugin is not installed or your SQL server is NUTS.');
}else return (int)$plg->id;
}
That did the trick.
In Joomla 3.x this is the way!!!
function pluginId($name,$type,$element,$folder)
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query
->select($db->quoteName('a.extension_id'))
->from($db->quoteName('#__extensions', 'a'))
->where($db->quoteName('a.name').' = '.$db->quote($name))
->where($db->quoteName('a.type').' = '.$db->quote($type))
->where($db->quoteName('a.element').' = '.$db->quote($element))
->where($db->quoteName('a.folder').' = '.$db->quote($folder));
$db->setQuery($query);
$db->execute();
if($db->getNumRows()){
return $db->loadResult();
}
return false;
}
Then to use the function:
$pluginId = pluginId('plg_system_getbibleactivitycron','plugin','getbibleactivitycron','system');
if($pluginId){
echo 'Plugin id is: '. $pluginId;
} else {
echo 'Plugin not installed';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With