J3.x

Creating a simple module/Adding Form Fields: Difference between revisions

From Joomla! Documentation

Wilsonge (talk | contribs)
Start
 
Cmb (talk | contribs)
m Some punctuation and grammar corrections.
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Chunk30:Creating a Simple Module for Joomla!3.x - Contents}}
<noinclude><languages /></noinclude>
{{J3.x:Creating_a_simple_module/<translate><!--T:1-->
en</translate>}}
<translate><!--T:2-->
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.</translate>


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.
<translate>
== Adding Form Fields to the Module == <!--T:3--></translate>
<translate><!--T:4-->
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. (The full details for this is on the [[S:MyLanguage/SQL_form_field_type|SQL Form Field]] docs page).</translate>


== Adding Form Fields to the module ==
<translate><!--T:5-->
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? Now inside these tags we are going to add the following code:</translate>
 
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:


<source lang="xml">
<source lang="xml">
<fields name="params">
<fields name="params">
     <fieldset name="basic">
     <fieldset name="basic">
         <field name="lang" type="sql" default="1" label="Select a language" query="SELECT id AS value, 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>
</source>
</source>


This selects the languages from the database table and allows the user to choose which one from a select element.
<translate><!--T:6-->
This selects the languages from the database table and allows the user to choose one from a select element.</translate>


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).
<translate><!--T:7-->
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 has appeared (although picking an option won't affect the module at the moment).</translate>


''Note:'' The form field parameters are stored for each module in the #__modules table under the params field as json encoded string.
<translate><!--T:8-->
''Note:'' The form field parameters are stored for each module in the ''#__modules'' table under the ''params'' field as JSON-encoded string.</translate>


Now lets make the form field do something!
<translate><!--T:9-->
Now let's make the form field do something!</translate>


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.
<translate><!--T:10-->
Go into the ''mod_helloworld.php'' file and retrieve the parameter. (Note that you '''cannot''' retrieve the parameter in the helper.) Then pass it into the helper function.</translate>


<source lang="php">
<source lang="php">
/**
/**
   * This retrieves the lang parameter we stored earlier. Note the second part
   * 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
   * which assigns the default value of 1 if the parameter cannot be
   * retrieved for some reason
   * retrieved for some reason.
**/
**/
$language = $params->get('lang', '1');
$language = $params->get('lang', '1');
$hello = modHelloWorldHelper::getHello( $language );
$hello   = modHelloWorldHelper::getHello( $language );
</source>
</source>


Now lets edit our database query in the helper file to reflect our parameter choice
<translate><!--T:11-->
Now let's edit our database query in the helper file to reflect our parameter choice.</translate>


<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 that 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 = '. $db->Quote($params));
//Prepare the query
// Prepare the query
$db->setQuery($query);
$db->setQuery($query);
// Load the row.
// Load the row.
$result = $db->loadResult();
$result = $db->loadResult();
//Return the Hello
// Return the Hello.
return $result;
return $result;
</source>
</source>


== Conclusion ==
<translate>
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.
== Conclusion == <!--T:12-->
Form fields give the user an easy way to customise the module to their site's settings. This allows the module's scope to be increased as well for many languages and applications.</translate>


<div class="row">
<div class="large-6 columns">{{Basic button|<translate><!--T:13-->
S:MyLanguage/J3.x:Creating_a_simple_module/Using_the_Database|Prev: Using the Database</translate>|class=expand success}}</div>
<div class="large-6 columns">{{Basic button|<translate><!--T:14-->
S:MyLanguage/J3.x:Creating_a_simple_module/Adding_Auto_Update|Next: Adding Auto Update</translate>|class=expand}}</div>
</div>
__NOTOC__
<noinclude>
<noinclude>
<translate>
<!--T:15-->
[[Category:Tutorials]]
[[Category:Tutorials in a Series]]
[[Category:Module Development]]
[[Category:Module Development]]
[[Category:Beginner Development]]
[[Category:Joomla! 3.x]]
[[Category:Joomla! 3.0]]
[[Category:Joomla! 3.1]]
[[Category:Joomla! 3.2]]
[[Category:Joomla! 3.3]]
[[Category:Joomla! 3.4]]
</translate>
</noinclude>
</noinclude>

Latest revision as of 16:30, 26 June 2019

Joomla! 
3.x
Tutorial
Creating a simple module

This is a multiple article series on how to create a module for Joomla! Version Joomla 3.x. You can navigate the articles in this series by using the navigation drop down menu.

Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (Articles in this series). There are 2 videos accompanying this tutorial which you can view at Basic Joomla Module Development video 1 and Basic Joomla Module Development video 2.




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.

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. (The full details for this is 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? 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 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 has 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.

Now let's make the form field do something!

Go into the mod_helloworld.php file and retrieve the parameter. (Note that you cannot retrieve the parameter in the helper.) Then pass it into the helper function.

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

Now let's edit our database query in the helper file to reflect our parameter choice.

// Obtain a database connection.
$db = JFactory::getDbo();
// Retrieve the shout. Note that we are now retrieving the id not the lang field.
$query = $db->getQuery(true)
            ->select($db->quoteName('hello'))
            ->from($db->quoteName('#__helloworld'))
            ->where('id = '. $db->Quote($params));
// Prepare the query
$db->setQuery($query);
// Load the row.
$result = $db->loadResult();
// Return the Hello.
return $result;

Conclusion

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