3

I need to retrieve a system plugin id, and I am using the following code with success.

function getId($folder, $name)
    {
        // Get a database object
        $db = JFactory::getDbo();

        // Create a new query object
        $query = $db->getQuery(true);

        // Select field to get
        $fields = array(
            $db->quoteName('extension_id')
        );

        // Conditions for which record should be get
        $conditions = array(
            $db->quoteName('element') . ' = ' . $db->quote($name), 
            $db->quoteName('folder') . ' = ' . $db->quote($folder)
        );

        // Set the query and load the result
        $query->select($fields)->from($db->quoteName('#__extensions'))->where($conditions);
        $db->setQuery($query);   
        $id = $db->loadResult();

        return $id;
    }

// Set query values
$folder = 'system';
$name   = 'pugin_name';

// Get plugin id
$id = getId($folder, $name);

What I would like to know is if there is a better or more correct and appropriate way of doing it.

  • Actually a good question. I looked at com_plugins to see how it's done there but MVC is not my strong point. If you're familiar with it, you may find it helpful :) – Lodder Mar 14 '15 at 11:29
  • there is no function available in plugin-helper or any other class which will result you this desired output. Query execution is the only way as far as I know. Couple of weeks back i done the same think in a code. – Nick Mar 14 '15 at 18:12
2

This can be done using the plugin helper

JPluginHelper::getPlugin('user','myplugin')->id;

However you'd probably want to check that getPlugin returned something before trying to access its id property.

This is working in 3.8.7, but I don't know when it was introduced.

  • I think that the ability to retrieve the plug id through the getPlugin() method was introduced to Joomla! 3.8.0 – n.h. Feb 22 at 17:34
1

This is how I get info on extensions

public function getItemByElement($element, $type, $folder = null)
{
    $dbo = JFactory::getDbo();
    $query = $dbo->getQuery(true);
    $query->select('extension_id, manifest_cache');
    $query->from('#__extensions');

    $query->where('element = '.$dbo->quote($element));
    $query->where('type = '.$dbo->quote($type));

    if($type == 'plugin' && !empty($folder))
    {
        $query->where('folder = '.$dbo->quote($folder));
    }

    $dbo->setQuery($query);
    $extensionRecord = $dbo->loadAssoc();

    if(!is_null($extensionRecord))
    {
            $manifestData = json_decode($extensionRecord['manifest_cache']);
            $extensionRecord['manifest'] = $manifestData;
    }

    return $extensionRecord;
}

This will let you get anything in the extensions table

//return a component 
$componentData = getItemByElement('com_content', 'component');

//return a plugin
$pluginData = getItemByElement('codemirror','plugin','editors');

//return a module
$moduleData = getItemByElement('mod_article_archives', 'module');

//return library
$libData = getItemByElement('joomla','library');

I haven't tested it on languages (haven't had the need), but I'm sure it will work for that too.

Happy Joomla!ng =^D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.