5 minutes reading time (1031 words)

Adding Fields to com_content The Smart Way

Adding Fields to com_content The Smart Way

I was recently working on a project where I needed to add customer testimonials. I wanted to output them using the schema.org/Reviews format. As I searched for solutions, everything I was finding was more than I needed. My goal was to keep it simple for my client to use, but still be able to output the right format. The easiest approach for my client was to add fields to the Articles (com_content) extension within Joomla. Of course I didn’t want to actually modify the com_content code or the #__content table in Joomla. Otherwise that would lead to maintenance and upgrade headaches down the road. So, plugins to the rescue!

The Joomla plugin architecture is extremely flexible and can allow you to do amazing things without changing the core code.

Plugin Goals

I decided on the following goals for this plugin:

  1. Add fields to the existing Articles manager form to allow the client to enter additional data for the testimonial.
  2. The testimonials would all reside in a specific category or one of its children categories.
  3. Output the data when that page is displayed on the front-end in the schema.org/Review format.

I could meet the first two goals with the plugin. The third goal would be met using a layout override.

The File Structure

The plugin will have the following file structure.

ksextras
|-> ksextras.php
|-> ksextras.xml
|-> extras
	|-> ksextras.css
	|-> testimonial.xml
|-> language
	|-> en-GB
		|-> en-GB.plg_content_ksextras.ini
		|-> en-GB.plg_content_ksextras.sys.ini
|-> sql
	|-> install.mysql.utf8.sql
	|-> uninstall.mysql.utf8.sql
	|-> update
		|-> install.mysql.utf8.sql

Feel free to download the files from my github account at:

Installation XML File

While we don’t have space to cover every bit of the file, we’ll touch on a few important ones.

The plugin group content tells Joomla what type of plugin this is and determines which triggers we can use.

Since we’re going to store the data in a separate table, we’ll include the option with the related SQL files. We’ll also include the and options for future use in case we decide to uninstall the plugin or update it. The option will run the SQL to delete the table. The file can contain SQL to update the table schema.

We’ll set some parameters in the block that we can use with the plugin, so that we don’t have to hard code this information in the code itself. These will include the Organization, this is the company that owns the website and is being reviewed. We’ll include some fields that are required by the schema.org/Review format. We’ll be able to set category on which the testimonial fields should be displayed and whether to include its child categories.

Plugin PHP File

The main file for the plugin is ksextras.php.

A few items to highlight.

__construct

In the __construct method, we only want to use this plugin for the com_content component. So, we’ll get the option parameter and if it is not com_content, we’ll return and not process the rest of the plugin.

onContentPrepareForm

This method will prepare the form fields to add to the Article Manager edit screen. It will add the extra fields in a separate tab. The label text for this tab is controlled through the language file using the “PLG_CONTENT_KSEXTRAS_SLIDER_LABEL” string in the main languge .ini file.

First, we’ll check to see if the category id is set. If not, we’ll go ahead and display the extra form fields since the user may want to set it for the Testimonials category. If the category id is set to something other than the category designated in the plugin parameters, it won’t display the extra fields.

From there, we’ll get the form fields from the extras/testimonial.xml file. Then, if there is an existing article ID, we are in edit mode and need to query the database to get the data.

onContentPrepareData

This method will retrieve and prepare the data.

setKSFields

This method sets an array of the fields to be used in the data. We use these fields to fill in the form data in the onContentPrepareForm. If you modify the fields in the Form XML File (see below), you should add that field to the ksfields array. This is a convenience to avoid writing extra code at the end of the onContentPrepareData method and allow us to loop through the fields and automatically set the values.

To learn more about the various events, you can visit:

Form XML File

In the extras folder, we have a JForm XML file that defines the fields used for this plugin. These are the fields that will show up in the tab in the article edit form. You can add or modify these to change what fields you use. Be sure and make sure the setKSFields method is updated to correspond to the fields in this XML file.

To learn more about the different field types, you can visit:

Language Files

Ideally, you will set up your plugin to allow for different languages. You can do that through the language ini files found in language/en-GB/en-GB.plg_content_ksextras.ini file.

Usage: Layout Overrides

In order to use the fields in the output on your site, you’ll need to create a layout override.

Accessing Articles Attributes / KSExtras data

To access the data from your article override, you can use something similar to the following code:

$ksattribs = json_decode($this->item->attribs);
$testimonial_by = $ksattribs->testimonial_by;
$author_job_title = $ksattribs->author_job_title;

If you are using this in a module override, your code will be slightly different. Instead of referencing “$this->item->”, you will reference “$item->”.

$ksattribs = json_decode($item->attribs);
$testimonial_by = $ksattribs->testimonial_by;
$author_job_title = $ksattribs->author_job_title;

Accessing Plugin Parameters:

To get the plugin parameters, you can use something like:

$plgParams = new JRegistry();
$plugin = JPluginHelper::getPlugin('content', 'ksextras');
if ($plugin && isset($plugin->params))
{
    $plgParams->loadString($plugin->params);
}
$this->organization = $plgParams->get('organization', 'org was not found');
$this->organization_type = $plgParams->get('organization_type', 'Organization');

To learn more about layout overrides, you can visit:

Once you install the plugin, you’ll need to first set the parameters for the plugin through the Extension Manager >> Plugins.

Conclusion

We’ve covered a lot of ground. I’m sure there are many ways to improve and extend this plugin. So, experiment and have fun!

0
What do Italy and Mexico Have in Common?
Pourquoi migrer ? Et... comment ?
 

Comments

Already Registered? Login Here
No comments made yet. Be the first to submit a comment

By accepting you will be accessing a service provided by a third-party external to https://magazine.joomla.org/