Adding custom fields to core components using a plugin

From Joomla! Documentation

Revision as of 06:40, 25 February 2014 by Waveywhite (talk | contribs) (Created page with "Have you ever wished that there was an extra phone number field in com_contact or needed an extra field for articles in com_content? Joomla provides a straight-foward way of i...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Have you ever wished that there was an extra phone number field in com_contact or needed an extra field for articles in com_content? Joomla provides a straight-foward way of integrating new fields with core components such as these. All it takes is a simple content plugin and a template override to achieve.

This article shows how to add an extra field to a core component using the example of adding an additional email address to contacts in com_contact.

Adding a custom field[edit]

It is recommended that you first read Creating a Plugin for Joomla for details on how to create and install a plugin, which is not covered here.

To add a field to a system component you need to create a content plugin which picks up the onContentPrepareForm event and inserts it's own fields into the given JForm. The following code is for Joomla 3.1 and later, for Joomla 2.5 the language files need to be loaded in the constructor (see J2.5:Creating a Plugin for Joomla).

<?php
// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );

class plgContentExample extends JPlugin {
	
	/**
	 * Load the language file on instantiation.
	 * Note this is only available in Joomla 3.1 and higher.
	 * If you want to support 3.0 series you must override the constructor
	 *
	 * @var boolean
	 * @since 3.1
	 */
	
	protected $autoloadLanguage = true;

	function onContentPrepareForm($form, $data) {
		$app = JFactory::getApplication();
		$option = $app->input->get('option');
		
		switch($option) {
			
			case 'com_contact':
				if ($app->isAdmin()) {
					JForm::addFormPath(__DIR__ . '/forms');
					$form->loadFile('contact', false);
				}
				return true;
				
		}
		return true;
	}
	
}
?>

The additional fields are loaded from the file forms/contact.xml in the plugin directory. It's important that these fields are in a fields element with the name property set to "params". If you don't set this property name the fields will appear in the admin site but the values will not be saved.

In this case we are adding a field for a label to describe the email field in the public website and a second field for the value of the email address.

<?xml version="1.0" encoding="UTF-8"?>
<form>
	<fields name="params" >
		<fieldset name="params" >
			<field
				name="contact_emaillabel2"
				type="text"
				label="PLG_CONTENT_EXAMPLE_CONTACT_EMAILLABEL2"
				/>
			<field
				name="contact_email2"
				type="text"
				label="PLG_CONTENT_EXAMPLE_CONTACT_EMAIL2"
				filter="email"
			/>
		</fieldset>
	</fields>
</form>

Finally we need a language file so that the parameters are presnted nicely in the admin site and are translatable into different languages. This file needs to be called something like en-GB.plg_content_example.ini.

COM_CONTACT_PARAMS_FIELDSET_LABEL="Additional Information"
PLG_CONTENT_EXAMPLE_CONTACT_EMAIL2="Additional email address"
PLG_CONTENT_PHOTOCMS_CONTACT_EMAILLABEL2="Additional email label"

Displaying the custom field[edit]