Archived

Difference between revisions of "Creating a simple module/Adding Form Fields"

From Joomla! Documentation

< Archived:Creating a simple module
(→‎Adding Form Fields to the module: Add explanatory paragraph)
 
(9 intermediate revisions by 3 users not shown)
Line 7: Line 7:
  
 
== Adding Form Fields to the module ==
 
== Adding Form Fields to the module ==
In this case we are going to extend our previous example using the database to provide a list of languages for the user to select from. To allow this to happen we will use the SQL form field type (full details for this on the [[SQL_form_field_type|SQL Form Field]] docs page.
+
In this case we are going to extend our previous example using the database to provide a list of languages for the user to select from. To allow this to happen we will use the SQL form field type (full details for this on the [[SQL_form_field_type|SQL Form Field]] docs page).
  
 
First of all we are going to add a form field into our xml file. Remember those config tags that we added in earlier? Well now inside these tags we are going to add the following code:
 
First of all we are going to add a form field into our xml file. Remember those config tags that we added in earlier? Well now inside these tags we are going to add the following code:
Line 14: Line 14:
 
<fields name="params">
 
<fields name="params">
 
     <fieldset name="basic">
 
     <fieldset name="basic">
         <field name="lang" type="sql" default="10" label="Select a language" query="SELECT id AS value, title AS lang FROM #__helloworld" />
+
         <field
 +
              name="lang"
 +
              type="sql"
 +
              default="1"
 +
              label="Select a language"
 +
              query="SELECT id AS value, lang FROM #__helloworld" />
 
     </fieldset>
 
     </fieldset>
 
</fields>
 
</fields>
Line 24: Line 29:
  
 
''Note:'' The form field parameters are stored for each module in the #__modules table under the params field as json encoded string.
 
''Note:'' The form field parameters are stored for each module in the #__modules table under the params field as json encoded string.
 +
 +
Because of the Joomla! module rendering method, when we are inside the mod_helloworld.php program, we have the same variables in scope that we have in the renderModule() method of the JModuleHelper class (libraries/joomla/application/module/helper.php). Among these we have the $params variable defined as follows:
 +
 +
<source lang="php">
 +
$params= new JRegistry;
 +
$params->loadJSON($module->params);
 +
</source>
 +
 +
Hence, we can always access the $params variable although it's never been defined in our code.
  
 
Now lets make the form field do something!
 
Now lets make the form field do something!
Line 36: Line 50:
 
**/
 
**/
 
$language = $params->get('lang', '1');
 
$language = $params->get('lang', '1');
$hello = modHelloWorldHelper::getHello( $language );
+
$langid = $params->get('id', '1');
 +
$hello   = modHelloWorldHelper::getHello($langid);
 
</source>
 
</source>
 +
  
 
Now lets edit our database query in the helper file to reflect our parameter choice
 
Now lets edit our database query in the helper file to reflect our parameter choice
  
 
<source lang="php">
 
<source lang="php">
//Obtain a database connection
+
// Obtain a database connection
 
$db = JFactory::getDbo();
 
$db = JFactory::getDbo();
//Retrieve the shout - note we are now retrieving the id not the lang field.
+
// Retrieve the shout - note we are now retrieving the id not the lang field.
 
$query = $db->getQuery(true)
 
$query = $db->getQuery(true)
 
             ->select($db->quoteName('hello'))
 
             ->select($db->quoteName('hello'))
 
             ->from($db->quoteName('#__helloworld'))
 
             ->from($db->quoteName('#__helloworld'))
             ->where('id = '. $db->Quote($params));
+
             ->where('id = '. (int)$params); //here $params is the parameter passed to the helper getHello function
//Prepare the query
+
// Prepare the query
 
$db->setQuery($query);
 
$db->setQuery($query);
 
// Load the row.
 
// Load the row.
$results = $db->loadRow();
+
$result = $db->loadResult();
 
//Return the Hello
 
//Return the Hello
return $results[1];
+
return $result;
 
</source>
 
</source>
  

Latest revision as of 17:18, 26 April 2022

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.


Form fields give a great deal of customisation in Joomla and for modules are the sole way of allowing the user to tweak the module to the needs of their site.

What are Form Fields?[edit]

In Joomla 1.5 form fields were called parameters - and a comparison between form fields and parameters can be found on this page. A complete list of form fields can be found on this page.

Adding Form Fields to the module[edit]

In this case we are going to extend our previous example using the database to provide a list of languages for the user to select from. To allow this to happen we will use the SQL form field type (full details for this on the SQL Form Field docs page).

First of all we are going to add a form field into our xml file. Remember those config tags that we added in earlier? Well now inside these tags we are going to add the following code:

<fields name="params">
    <fieldset name="basic">
        <field
               name="lang"
               type="sql"
               default="1"
               label="Select a language"
               query="SELECT id AS value, lang FROM #__helloworld" />
    </fieldset>
</fields>

This selects the languages from the database table and allows the user to choose which one from a select element.

If you were to install the module as it is now you would see if you were to access the module in the backend of Joomla that a dropdown had appeared (although picking an option won't affect the module at the moment).

Note: The form field parameters are stored for each module in the #__modules table under the params field as json encoded string.

Because of the Joomla! module rendering method, when we are inside the mod_helloworld.php program, we have the same variables in scope that we have in the renderModule() method of the JModuleHelper class (libraries/joomla/application/module/helper.php). Among these we have the $params variable defined as follows:

$params= new JRegistry;
$params->loadJSON($module->params);

Hence, we can always access the $params variable although it's never been defined in our code.

Now lets make the form field do something!

Lets go into the mod_helloworld.php and retrieve the parameter (note you cannot retrieve the parameter in the helper), and then pass it into the helper function.

/**
  * This retrieves the lang parameter we stored earlier. Note the second part
  * which states to use the default value of 1 if the parameter cannot be
  * retrieved for some reason
**/
$language = $params->get('lang', '1');
$langid = $params->get('id', '1');
$hello    = modHelloWorldHelper::getHello($langid);


Now lets edit our database query in the helper file to reflect our parameter choice

// Obtain a database connection
$db = JFactory::getDbo();
// Retrieve the shout - note we are now retrieving the id not the lang field.
$query = $db->getQuery(true)
            ->select($db->quoteName('hello'))
            ->from($db->quoteName('#__helloworld'))
            ->where('id = '. (int)$params); //here $params is the parameter passed to the helper getHello function
// Prepare the query
$db->setQuery($query);
// Load the row.
$result = $db->loadResult();
//Return the Hello
return $result;

Conclusion[edit]

Form fields give the user an easy way to customise the module to their sites settings. This allows the modules scope to be increased as well for many languages and applications.