0

To be honest, I don't know what jimport and jimport('joomla.plugin.plugin') do. So I think an explanation of what they do might as well answer my question.

That being said, I've made a simple plugin without jimport('joomla.plugin.plugin'), but in other plugins I see this a lot, so when do I need it?

1

Pretty sure that specific instance is a relic of the past. It was used to import the base class that your plugin class extended. (eg the JPlugin in class PlgUserJoomla extends JPlugin )

The joomla.plugin.plugin was a representation for the file in the libraries folder JPATH_ROOT . "/libraries/joomla/plugin/plugin.php" if I remember right.

jimport('xxx') is currently an alias for JLoader::import('xxx') and still used for some imports in the Joomla source.

function jimport($path, $base = null)
{
    return JLoader::import($path, $base);
}

However, the necessity of that I am unsure of. Much of the source auto-loads now either via a namespace or or an alias.

For example, JPlugin is an alias of \Joomla\CMS\Plugin\CMSPlugin.

JLoader::registerAlias('JPlugin',                           '\\Joomla\\CMS\\Plugin\\CMSPlugin', '5.0');

Note: As of right now, current version of Joomla is 3.9.13.

  • 1
    An additional note just to make it sure: there is no need of using jimport(); function in plugins anymore, since the JPlugin class is autoloaded. – Zollie Dec 14 '19 at 6:28
  • 1
    However I would always use the namespaced version now as it is in your answer. So, on the top of a plugin class file: use \Joomla\CMS\Plugin\CMSPlugin; and extend CMSPlugin with the plugin. – Zollie Dec 14 '19 at 6:54
  • @Zollie Is use \Joomla\CMS\Plugin\CMSPlugin; necessary? My plugin runs well without it – shenkwen Dec 14 '19 at 13:13
  • @shenkwen - no, you do not have to use that if you extend the JPlugin aliased class without referencing it using CMSPlugin namespace (only few years later will have real relevance of using the CMSPlugin namespaced name instead of JPlugin when you create a plugin, As far as I remember only in Joomla 5. – Zollie Dec 14 '19 at 14:51

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.