Adding Parameters to Our Plugin
Next, let’s add a parameter to our plugin. Suppose we want the user to be able to configure whether or not to show the second check box. To do this, we need to do three things:
- Add a parameter field to our plugin XML file.
- Add logic to the plugin to check the parameter and remove the field if desired.
- Add the new language keys to the main language file.
The parameter field is added using a JForm field, similar to what we added in our form.xml file. The revised code for the config element is shown here:
<config> <fields name="params"> <fieldset name="basic" > <field name="show_age_checkbox" type="radio" label="PLG_USER_MYREGISTRATION2_SHOW_AGE" description="PLG_USER_MYREGISTRATION2_SHOW_AGE_DESC" default="0"> <option value="0">JHIDE</option> <option value="1">JSHOW</option> </field> </fieldset> </fields> </config>
This code is similar to the code for template parameters we saw in Chapter 3. It is used to add the options when we edit this plugin. We create a fields element with the name attribute equal to “params” to hold our fieldset and field elements and put the fields element inside the config element.
We need this fields element with the name attribute in our XML file. Otherwise the options will not show in the plugin edit screen. This is because the layout file used to create the form for editing plugins (administrator/components/com_plugins/views/plugin/tmpl/edit_options.php) looks for a fields element with a name of “params” and includes the contained fieldset elements as options in the form. If we don’t have this fields element, no options will show on the plugin edit screen. This is also true for components, modules, and templates.
Note that we didn’t need this fields element when we created our form.xml file. It is only required when we are adding parameters to the installation XML file for an extension.
Inside the fieldset element we define a new field with a type of “radio” with two option elements, “0” for Hide and “1” for Show. Notice that we use the language keys JHIDE and JSHOW. These are standard language keys used throughout Joomla, so we don’t have to add them to our plugin language file. Notice also that each option is a separate element. Field types that provide a list of options, like radio or list, use a series of option elements, one for each item in the list.
The next step is to add the two new keys to the language file. We add them to the en-GB.plug_user_myregistration2.ini file (not the .sys.ini file) because these are used when editing the individual plugin in Plugin Manager. The label attribute should be short, since it will be used to label the field. The description attribute can be longer. It will show up in the tool tip that pops up when you hover the mouse on the field.
The new language file lines are as follows:
PLG_USER_MYREGISTRATION2_SHOW_AGE="Show Age Checkbox" PLG_USER_MYREGISTRATION2_SHOW_AGE_DESC="Whether to Hide or Show the Show Age check box. If it is shown, it will be required."
Figure 5.8 shows the two new language strings on the Plugin Manager form. Note that we have Debug Language enabled in the Global Configuration → System, so we see the double “**” around the text fields to indicate that they are properly translated.
Figure 5.8. New parameter in Plugin Manager
The last step is to change the plugin code to check the new parameter and act on it. The revised code for the onContentPrepareForm() method is as follows:
public function onContentPrepareForm($form, $data) { // If we aren't in the registration form ignore the form. if ($form->getName() != 'com_users.registration') { return; } // Load the plugin language file $this->loadLanguage(); // Load our custom registration xml into the user registration form. $form->loadFile(dirname(__FILE__).'/forms/form.xml');if (!$this->params->get('show_age_checkbox', '1')) {
$form->removeField('old_enough');
}
}
The only new code is the if statement at the end of the method. This gets the new parameter and uses it as a boolean value inside the if statement condition. If the Show option was selected, the parameter will be true. Since we use a not (“!”) in the if statement condition, the statement inside the if code block will not be executed when the parameter is true. So, if we have selected Show, we will skip the if block and the entire form will be shown.
If we have selected Hide, the statement
$form->removeField('old_enough');
will execute. This uses the removeField() method of the JForm class to remove the field named “old_enough” from the form. That’s all we need to do. After you have made these changes, test that the parameter works as expected.
There are some other useful methods in JForm we can use to modify forms in a plugin. One is to change a field attribute. For example, suppose we wanted to have an option to choose whether or not to make a field required. We could use the command
$form->setFieldAttribute('old_enough', 'required', 'false');
to change the “old_enough” field to no longer be required.
The important thing to remember is this: We can use the onContentPrepareForm event to intercept the JForm object before the form is shown. Then we can use JForm methods to add, remove, or alter fields in the form. This gives us a chance to tailor the form to fit our exact needs.