Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla get plugin id

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:

  • http://docs.joomla.org/Plugin_Developer_Overview
  • http://api.joomla.org/Joomla-Framework/Plugin/JPlugin.html
  • http://api.joomla.org/Joomla-Framework/Plugin/JPluginHelper.html
  • http://forum.joomla.org/viewtopic.php?p=2227737

Guess I'll be using the wisdom from that last link...

like image 978
Christian Avatar asked Dec 05 '10 10:12

Christian


3 Answers

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;
}
like image 125
Benn Avatar answered Oct 12 '22 03:10

Benn


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.

like image 29
Christian Avatar answered Oct 12 '22 04:10

Christian


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';
}
like image 27
Llewellyn Avatar answered Oct 12 '22 05:10

Llewellyn