0

I'm developing a custom library which will be used for custom JHtml methods:

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Uri\Uri;

abstract class JHtmlKipengee
{
    protected static $loaded = [];

    public static function modal()
    {
        // Only load once
        if (!empty(static::$loaded[__METHOD__]))
        {
            return;
        }

        HTMLHelper::_('script', Uri::root() . 'media/kipengee/js/modal.js', ['version' => 'auto']);

        static::$loaded[__METHOD__] = true;

        return;
    }
}

\JLoader::register('Kipengee', JPATH_LIBRARIES . '/kipengee/kipengee.php');

This also registers the library and everything is working so far.

Now, to call the method, I'd like to be able to use:

HTMLHelper::_('kipengee.modal');

on its own, however this only works if I use addIncludePath() too, like so:

HTMLHelper::addIncludePath(JPATH_LIBRARIES . '/kipengee');
HTMLHelper::_('kipengee.modal');

I would ideally like to be able to register 'kipengee.modal' within the library, and without the use of a system plugin, so that I'm not having to do it every time I call HTMLHelper::_('kipengee.modal');

Is this possible?

  • Does that first snippet contains contents of JPATH_LIBRARIES . '/kipengee/kipengee.php'? – Sharky May 5 at 11:14
  • @Sharky yes, it does – Lodder May 5 at 11:27
  • It's not clear to me why it's trying to register itself and with a different class name. How do you know the class is working, how are you accessing it? – Sharky May 5 at 11:51
  • I know the class is working because if I incldue HTMLHelper::addIncludePath(JPATH_LIBRARIES . '/kipengee'); and HTMLHelper::_('kipengee.modal');, then the modal script is loaded on the page. The only thing I don't want to do is use HTMLHelper::addIncludePath() – Lodder May 5 at 12:48
1

Assuming you don't even want to load the class file, this is simply not possible. You must register the JHtml service or the include paths or load the class. Either manually wherever you want to use it or with a system plugin to do this system-wide.

| improve this answer | |
0

There's a way to do that, though I'm not sure it's any better/easier than the addIncludePath you don't want to use, depending on why you don't want to use it, for reasons I'll get into later. This way works for individual methods (you've only talked about one method, 'modal', so that's why I'm putting this out there).

That being said, get out your gear and let's go codewalking. Line numbers here refer to latest Joomla stable.

Line 159 of HTMLHelper.php shows the register method. This is the key to your door. That method registers any method with HTMLHelper and connects it with a key:

HTMLHelper::register( *key*, *method* )

In this specific example the key will be 'kipengee.modal' (your modal script) and the method will be your modal method.

After that call, any HTMLHelper::_ call using the 'kipengee.modal' key will grab your method and run. That method (Line 99 of the same file) reads the key being sent it, looks up that key in a table to find what method it's supposed to call to get it done, and calls it.

The smoothest way to implement this involves writing a plugin, and issuing that register call inside the plugin's onAfterInitialise method, which will get called early enough in the process to make it available thoughout the templating system. If you do this, then make your modal method a static member of that plugin and you're home and dry.

If you're wanting to make this part of a component, so every output of the component has access to it, you make your method a static member of your component and register it when the component is initialized.

If you're wanting it to be a part of your template overrides, I think you can wait to register it until the template's index.php file, though there I'm on less secure ground. You might need to go the plugin route for this, but it's worth testing to find out.

And that brings me to why you may not find this better than addIncludePath. Writing a plugin is more work than simply using addIncludePath. And it moves away from having your own class to simply attaching the methods you want to use as a static member of whatever you're using to register the method. You decide which is better for your case; I'm not qualified to make that call for you.

This is all based on registering a single method, it'll get tedious as the number of methods grows. If your goal is to create an entire library of methods, such as the Bootstrap ecology, you might want to take a peek at how the Regularlabs library is installed in Joomla and emulate that.

| improve this answer | |

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.