Create a plugin to override Hotspots marker fields

Joomla! has a lot of "hidden" features that few people know about. Starting with Joomla 1.6 ( I think) we were able to define custom fields using plugins. This feature is relatively easy to use, if you know your way with PHP. Most probably this is one of the reasons why very few people actually know that they can define custom fields, override any field or delete any field in a component that follows the Joomla conventions. For those of you that want to find how to add custom fields to the default com_content (Article Manager) then look at this tutorial over here.

In this blog post we'll work with Hotspots. You can download the Core version over here. Starting with Hotspots 3, we use the JForm class and that in terms allows you to override or add new fields to the Hotspot submission form.

What are we going to do?

In Hotspots we use the default editor that you use on your site. However if for some reason you wan to use a different editor with Hotspots, you'll have to hack the administrator/components/com_hotspots/models/forms/marker.xml file and add few parameters to the hotspotsText field. If you hack this file, the next time you update hotspots you'll have to re-apply this hack. Which is a waste of time. If we were to use a plugin and to dynamically override the Hotspot form we could spare our self some work. So let us get started.

First of all create a directory - let's call it - hotspotsoverride. The structure of the directory should look like this:

- marker
-- marker.xml
- hotspotsoverride.php
- hotspotsoverride.xml

Below you have the code necessary for the hotspotsoverride.xml file:

<extension version="2.5" type="plugin" group="content" method="upgrade">
	<name>Hotspots overide marker</name>
	<author>compojoom</author>
	<creationdate>March 2014</creationdate>
	<copyright>(C) 2008 - 2014 compojoom.com All rights reserved.</copyright>
	<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
	<authoremail>contact-us@compojoom.com</authoremail>
	<authorurl>https://compojoom.com</authorurl>
	<version>2.5.0</version>
	<description>Plugin to override the Hotspots Marker fields.</description>
	<files>
		<folder>marker</folder>
		<filename plugin="hotspotsoverride">hotspotsoverride.php</filename>
	</files>
	<config>
	</config>
</extension> 

This is a basic plugin definition necessary for the installation of the plugin. Now let us create the marker.xml that will override the hotspots editor:

<form><fieldset>
		<field name="hotspotText" type="editor" class="inputbox" label="COM_HOTSPOTS_FIELD_DESCRIPTION" description="COM_HOTSPOTS_FIELD_DESCRIPTION_DESC" filter="JComponentHelper::filterText" editor="none" buttons="true" hide="article,image,pagebreak" required="true"></field>
	</fieldset>
</form> 
The interesting part comes with the hotspotsoverride.php file.
<?php
/**
 * @package    Hotspots
 * @author     DanielDimitrov <daniel@compojoom.com>
 * @date       11.03.13
 *
 * @copyright  Copyright (C) 2008 - 2013 compojoom.com . All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE
 */

defined('_JEXEC') or die('Restricted access');

/**
 * Class PlgContentHotspotsoverride
 *
 * @since  1.0
 */
class PlgContentHotspotsoverride extends JPlugin
{
	/**
	 * In this function we will modify the hotspots form
	 *
	 * @param   JForm   $form  - the form
	 * @param   object  $data  - the hotspots object
	 *
	 * @return bool
	 */
	public function onContentPrepareForm($form, $data)
	{
		if (!($form instanceof JForm) || ($form->getName() == 'com_hotspots.marker'))
		{
			// Add the path to our form for overriding
			JForm::addFormPath(dirname(__FILE__) . '/marker');

			// The second parameter makes sure that we override the previous value of the fields already present
			$form->loadFile('marker', true);
		}

		return true;
	}
} 

The magic actually happens on line 28 in the onContentPrepareForm. This function is automatically called by Joomla when it displays a JForm. On line 30, we just check that we are in the correct form (as we said earlier this function will be called for all extensions that use JForm) - in our case the form name is com_hotspots.marker. On line 33 we tell the JForm class that in the /marker folder there are .xml definitions for forms. On line 36 we tell Joomla to load the marker.xml file. The second parameter to load file is very important - it forces Joomla to actually override values that were previously defined and are again defined in the new .xml file. Now if you install and enable this plugin you'll see that the hotspot editor is actually set to none.

That's it! With just a few lines of code we overwrote the default field definitions. And not only that, but now when you update hotspot you can be sure that your changes will be still there. For those of you who want to have the code I've set up a github repository with the code for this plugin.

Now you can go crazy with your overrides :) Not only that, but you can define new fields! However, just keep in mind - the next version of hotspots will come with a custom fields option. Basically you'll get a screen where you would be able to create new text and select fields. You'll do this through a GUI, but in the background our code works more or less the same way as described here. We just use what Joomla has to offer!

Rate this blog entry:
3
Hotspots 6.0 brings Joomla4 compatibility
SEO Contest Siebtlingsgeburt with Joomla 2017