Creating a custom JHtml helper

206 views
Skip to first unread message

Marcos Lois Bermúdez

unread,
May 24, 2014, 10:49:18 AM5/24/14
to joomla-de...@googlegroups.com
Hi,

I'm creating a custom component for Joomla 3.x, but I'm stuck with a problem for more than one day. My goal seems to be simple, but I can't make it work.

My component have backend and frontend parts, I only want create a JHtml helper so I can write JHtml::_('mycomponent.searchtool.form') but always get a 500 Error about it's not found.

I created a very simple helper, and try to call it from my templates with no luck:

<pre>
component/
com_mycomp/
helpers/
html/
prueba.php
</pre>

And the file contains:

<pre>
<?php
defined('JPATH_PLATFORM') or die;

abstract class JHtmlPrueba {


public static function main() {
return "Hello cruel World!!";
}
}
</pre>

And try to call it from my JLayout that reside on '/component/com_mycomp/layouts/...', my layout are loaded correctly, but the JHtml helper not load, I try to put in helpers root folder, in libraries folder of joomla, and some other places without luck:

<pre>
...
JHtml::_('prueba.main');
...
</pre>

Where is the place to put JHtml helpers to be availabe for the component, can I make it available to all components / modules?.

Regards.

Viper

unread,
May 24, 2014, 12:24:18 PM5/24/14
to joomla-de...@googlegroups.com
Read the description for the addIncludePath() method in the libraries\cms\html\html.php

Marcos Lois Bermúdez

unread,
May 24, 2014, 3:21:38 PM5/24/14
to joomla-de...@googlegroups.com
Hi Viper,

Thanks for your fast reply.

I have some doubt about this:

- I also noticed this method, but this means that every component/module that want use this helper need to register it?
- Can it be auto-discovered? 
- My helper is a new one, but if I want to override a joomla provided one, i need to make it manually?
- Can be registered on a library install?

Now I'm creating a lib_module that will install my particular helper on ${joomla.dir}/library/cms/html that seems the only place that my helper is loaded.

Regards.

Viper

unread,
May 25, 2014, 1:42:35 AM5/25/14
to joomla-de...@googlegroups.com
I think that you're on the wrong way. Instead of creating custom global library you can create a library just for you component and use that library in any module/plugin/component. Why? This is easy for managing during install/uninstall and you automatically avoid a crap code.

something like:
class PUHtml extends JHtml...

When put this file in you component library folder and use JLoader for loading this class.

PS! If you want to autoload you library you need to add it into cms.php or import.php or import.legacy.php.


My helper is a new one, but if I want to override a joomla provided one, i need to make it manually?
As someone mention in one of topics it's bad idea to override core joomla classes.
Reply all
Reply to author
Forward
0 new messages