J3.x

Difference between revisions of "Developing an MVC Component/Adding backend actions"

From Joomla! Documentation

< J3.x:Developing an MVC Component
(Marked for translation)
(Several markup changes.)
 
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
<noinclude><languages /></noinclude>
 
<noinclude><languages /></noinclude>
{{:J3.1:Developing a MVC Component/<translate>en</translate>}}
+
{{:J3.1:Developing an MVC Component/<translate><!--T:1-->
<translate>== Introduction ==</translate>
+
en</translate>}}
<translate>This tutorial is part of the [[S:MyLanguage/J3.2:Developing a MVC Component | Developing a MVC Component for Joomla! 3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate>
+
<translate>== Introduction == <!--T:2--></translate>
 +
<translate><!--T:3-->
 +
This tutorial is part of the [[S:MyLanguage/J3.2:Developing an MVC Component | Developing an MVC Component for Joomla! 3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate>
  
<translate>== Adding a toolbar ==</translate>
+
<translate><!--T:29--> There is a series of five videos associated with this step in the tutorial, covering (1) [https://youtu.be/ODEHeAtsGx8 Changes to the Helloworlds view and layout], (2) [https://youtu.be/6pidS152n58 Handling Deletes], and then three on the functionality required to support ''New'' and ''Edit'': (3) [https://youtu.be/bUUh8grjhkc Handling New/Edit Redirect], (4) [https://youtu.be/R1-XVPhI-Ms Display the Helloworld message editing form] and (5) [https://youtu.be/ZXKIhIwGszE Handling Save and Cancel].</translate>
<translate>In Joomla, the administrator interacts generally with components through the use of a toolbar. In the file <tt>admin/views/helloworlds/view.html.php</tt> put this content. It will create a basic toolbar and a title for the component.</translate>
+
 
 +
In conjunction with this step, you will probably find it useful to read [[Model-View-Controller|this description of Joomla's implementation of MVC]]
 +
 
 +
{{#widget:YouTube|id=ODEHeAtsGx8}}
 +
 
 +
{{#widget:YouTube|id=6pidS152n58}}
 +
 
 +
{{#widget:YouTube|id=bUUh8grjhkc}}
 +
 
 +
{{#widget:YouTube|id=R1-XVPhI-Ms}}
 +
 
 +
{{#widget:YouTube|id=ZXKIhIwGszE}}
 +
 
 +
<translate>== Adding a Toolbar == <!--T:4--></translate>
 +
<translate><!--T:5-->
 +
In Joomla, the Administrator interacts generally with components through the use of a toolbar. In the file ''admin/views/helloworlds/view.html.php'' put this content. It will create a basic toolbar and a title for the component.</translate>
 +
 
 +
<translate><!--T:6-->
 +
The arguments used in, for example, JToolbarHelper::addNew is used to set a controller instance which will be used after button is clicked. Further details below in the [[#Adding_specific_controllers | adding specific controllers]] section.</translate>
  
<translate>The arguments used in, for example, JToolBarHelper::addNew is used to set a controller instance which will be used after button is clicked. Further details below in the [[#Adding_specific_controllers | adding specific controllers]] section.</translate>
 
 
 
 
<span id ="admin/views/helloworlds/view.html.php">
 
<span id ="admin/views/helloworlds/view.html.php">
<tt>admin/views/helloworlds/view.html.php</tt>
+
''admin/views/helloworlds/view.html.php''
<source lang="php" highlight="41,42,55-61">
+
<syntaxhighlight lang="php" highlight="41,42,55-61">
 
<?php
 
<?php
 
/**
 
/**
Line 17: Line 35:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 68: Line 86:
 
protected function addToolBar()
 
protected function addToolBar()
 
{
 
{
JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'));
+
JToolbarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'));
JToolBarHelper::addNew('helloworld.add');
+
JToolbarHelper::addNew('helloworld.add');
JToolBarHelper::editList('helloworld.edit');
+
JToolbarHelper::editList('helloworld.edit');
JToolBarHelper::deleteList('', 'helloworlds.delete');
+
JToolbarHelper::deleteList('', 'helloworlds.delete');
 
}
 
}
 
}
 
}
</source>
+
</syntaxhighlight>
 
</span>
 
</span>
  
<translate>You can find other classic backend actions in the <tt>administrator/includes/toolbar.php</tt> file of your Joomla installation.</translate>
+
<translate><!--T:7-->
 +
You can find other classic backend actions in the ''administrator/includes/toolbar.php'' file of your Joomla installation.</translate>
  
<translate>Since the view can perform some actions, we have to add some input data. With your favorite file manager and editor, put in the file <tt>admin/views/helloworlds/tmpl/default.php</tt></translate>
+
<translate><!--T:8-->
 +
Since the view can perform some actions, we have to add some input data. With your favourite file manager and editor, put in the file ''admin/views/helloworlds/tmpl/default.php''</translate>
  
 
<span id="admin/views/helloworlds/tmpl/default.php">
 
<span id="admin/views/helloworlds/tmpl/default.php">
<tt>admin/views/helloworlds/tmpl/default.php</tt>
+
''admin/views/helloworlds/tmpl/default.php''
<source lang="php" highlight="42,50,65,66,67">
+
<syntaxhighlight lang="php" highlight="42,50,65,66,67">
 
<?php
 
<?php
 
/**
 
/**
Line 89: Line 109:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 153: Line 173:
 
</form>
 
</form>
  
</source>
+
</syntaxhighlight>
 
</span>
 
</span>
  
<translate>== Adding specific controllers ==</translate>
+
<translate>
<translate>Three actions have been added:</translate>
+
<!--T:30-->
* ''helloworlds.delete''  
+
The ''underscore'' function of JRoute is used so that the site displays SEF URLs. See [[Supporting_SEF_URLs_in_your_component|Supporting SEF URLs in your component]].
 +
 
 +
The ''task'' field is used to define the ''controller''.''task'' parameter which will be sent to the server in the POST request when the form is submitted.
 +
 
 +
The ''boxchecked'' field is used to keep track of the number of checkboxes ticked.
 +
 
 +
Using the form token helps prevent [[How_to_add_CSRF_anti-spoofing_to_forms|CSRF attacks]].
 +
</translate>
 +
 
 +
<translate>== Adding Specific Controllers == <!--T:9--></translate>
 +
<translate><!--T:10-->
 +
Three actions have been added:</translate>
 +
* ''helloworlds.delete''
 
* ''helloworld.edit''
 
* ''helloworld.edit''
 
* ''helloworld.add''
 
* ''helloworld.add''
  
<translate>These are compound tasks (''controller''.''task''). So two new controllers <tt>HelloWorldControllerHelloWorlds</tt> and <tt>HelloWorldControllerHelloWorld</tt> have to be coded. (To understand the difference between the two types of subcontrollers read more in this article: [[S:MyLanguage/JController_and_its_subclass_usage_overview|subcontrollers...]]).</translate>
+
<translate><!--T:11-->
 +
These are compound tasks (''controller''.''task''). So two new controllers ''HelloWorldControllerHelloWorlds'' and ''HelloWorldControllerHelloWorld'' have to be coded. (To understand the difference between the two types of subcontrollers read more in this article: [[S:MyLanguage/JController_and_its_subclass_usage_overview|subcontrollers...]]).</translate>
  
 
<span id="admin/controllers/helloworlds.php">
 
<span id="admin/controllers/helloworlds.php">
<tt>admin/controllers/helloworlds.php</tt>
+
''admin/controllers/helloworlds.php''
<source lang="php">
+
<syntaxhighlight lang="php">
 
<?php
 
<?php
 
/**
 
/**
Line 172: Line 205:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 204: Line 237:
 
}
 
}
  
</source>
+
</syntaxhighlight>
 
</span>
 
</span>
  
 
<span id="admin/controllers/helloworld.php">
 
<span id="admin/controllers/helloworld.php">
<tt>admin/controllers/helloworld.php</tt>
+
''admin/controllers/helloworld.php''
<source lang="php">
+
<syntaxhighlight lang="php">
 
<?php
 
<?php
 
/**
 
/**
Line 215: Line 248:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 231: Line 264:
 
{
 
{
 
}
 
}
 
+
</syntaxhighlight>
</source>
 
 
</span>
 
</span>
  
<translate>== Adding an editing view ==</translate>
+
<translate>== Adding an Editing View == <!--T:12--></translate>
<translate>With your favorite file manager and editor, put a file <tt>admin/views/helloworld/view.html.php</tt> containing:</translate>
+
<translate><!--T:13-->
 +
With your favourite file manager and editor, put a file ''admin/views/helloworld/view.html.php'' containing:</translate>
  
 
<span id="admin/views/helloworld/view.html.php">
 
<span id="admin/views/helloworld/view.html.php">
<tt>admin/views/helloworld/view.html.php</tt>
+
''admin/views/helloworld/view.html.php''
<source lang="php">
+
<syntaxhighlight lang="php">
 
<?php
 
<?php
 
/**
 
/**
Line 246: Line 279:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 321: Line 354:
 
}
 
}
  
JToolBarHelper::title($title, 'helloworld');
+
JToolbarHelper::title($title, 'helloworld');
JToolBarHelper::save('helloworld.save');
+
JToolbarHelper::save('helloworld.save');
JToolBarHelper::cancel(
+
JToolbarHelper::cancel(
 
'helloworld.cancel',
 
'helloworld.cancel',
 
$isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE'
 
$isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE'
Line 329: Line 362:
 
}
 
}
 
}
 
}
</source>
+
</syntaxhighlight>
 
</span>
 
</span>
  
<translate>This view will display data using a layout.</translate>
+
<translate><!--T:14-->
 +
This view will display data using a layout.</translate>
  
<translate>Put a file <tt>admin/views/helloworld/tmpl/edit.php</tt> containing:</translate>
+
<translate><!--T:15-->
 +
Put a file ''admin/views/helloworld/tmpl/edit.php'' containing:</translate>
  
 
<span id="admin/views/helloworld/tmpl/edit.php">
 
<span id="admin/views/helloworld/tmpl/edit.php">
<tt>admin/views/helloworld/tmpl/edit.php</tt>
+
''admin/views/helloworld/tmpl/edit.php''
<source lang="php">
+
<syntaxhighlight lang="php">
 
<?php
 
<?php
 
/**
 
/**
Line 344: Line 379:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 359: Line 394:
 
             <div class="row-fluid">
 
             <div class="row-fluid">
 
                 <div class="span6">
 
                 <div class="span6">
                     <?php foreach ($this->form->getFieldset() as $field): ?>
+
                     <?php
                        <div class="control-group">
+
                        foreach($this->form->getFieldset() as $field) {
                             <div class="control-label"><?php echo $field->label; ?></div>
+
                             echo $field->renderField();
                            <div class="controls"><?php echo $field->input; ?></div>
+
                         }
                         </div>
+
                     ?>
                     <?php endforeach; ?>
 
 
                 </div>
 
                 </div>
 
             </div>
 
             </div>
Line 373: Line 407:
 
</form>
 
</form>
  
</source>
+
</syntaxhighlight>
 
</span>
 
</span>
  
<translate>== Adding a model and modifying the existing one ==</translate>
+
<translate>== Adding a Model and Modifying the Existing One == <!--T:16--></translate>
  
<translate>The <tt>HelloWorldViewHelloWorld</tt> view asks form and data from a model. This model has to provide a ''getTable'', a ''getForm'' method and a ''loadData'' method (called from the ''JModelAdmin'' controller).</translate>
+
<translate><!--T:17-->
 +
The ''HelloWorldViewHelloWorld'' view asks form and data from a model. This model has to provide a ''getTable'', a ''getForm'' method and a ''loadData'' method (called from the ''JModelAdmin'' controller).</translate>
  
 
<span id="admin/models/helloworld.php">
 
<span id="admin/models/helloworld.php">
<tt>admin/models/helloworld.php</tt>
+
''admin/models/helloworld.php''
<source lang="php">
+
<syntaxhighlight lang="php">
 
<?php
 
<?php
 
/**
 
/**
Line 388: Line 423:
 
  * @subpackage  com_helloworld
 
  * @subpackage  com_helloworld
 
  *
 
  *
  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
+
  * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  * @license    GNU General Public License version 2 or later; see LICENSE.txt
 
  */
 
  */
Line 471: Line 506:
 
}
 
}
 
}
 
}
 
+
</syntaxhighlight>
</source>
 
 
</span>
 
</span>
  
<translate>This model inherits from the ''JModelAdmin'' class and uses its ''loadForm'' method. This method searches for forms in the ''forms'' folder. With your favorite file manager and editor, put a file <tt>admin/models/forms/helloworld.xml</tt> containing:</translate>
+
<translate><!--T:18-->
 +
This model inherits from the ''JModelAdmin'' class and uses its ''loadForm'' method. This method searches for forms in the ''forms'' folder. With your favourite file manager and editor, put a file ''admin/models/forms/helloworld.xml'' containing:</translate>
  
 
<span id="admin/models/forms/helloworld.xml">
 
<span id="admin/models/forms/helloworld.xml">
<tt>admin/models/forms/helloworld.xml</tt>
+
''admin/models/forms/helloworld.xml''
<source lang="xml">
+
<syntaxhighlight lang="xml">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
 
<form>
 
<form>
Line 498: Line 533:
 
</fieldset>
 
</fieldset>
 
</form>
 
</form>
</source>
+
</syntaxhighlight>
 
</span>
 
</span>
  
<translate>Note: ''COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL'' and ''COM_HELLOWORLD_HELLOWORLD_GREETING_DESC'' are an alias for ''COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL'' and ''COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC'' respectively.</translate>
+
<translate><!--T:19-->
 +
Note: ''COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL'' and ''COM_HELLOWORLD_HELLOWORLD_GREETING_DESC'' are aliases for ''COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL'' and ''COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC'' respectively.</translate>
  
<translate>Here we are using different label and description to convey two different meanings in two different sections, but they can be easily kept equal.</translate>
+
<translate><!--T:20-->
 +
Here we are using different label and description to convey two different meanings in two different sections, but they can be easily kept equal.</translate>
  
<translate>== Packaging the component ==</translate>
+
<translate>== Packaging the Component == <!--T:21--></translate>
  
<translate>Content of your code directory</translate>
+
<translate><!--T:22-->
 +
Content of your code directory</translate>
 
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[#helloworld.xml|helloworld.xml]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Using_the_database#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/models/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/models/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Using_the_database#site/models/helloworld.php|site/models/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#site/models/helloworld.php|site/models/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/language/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/language/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/language/en-GB/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/language/en-GB/index.html]]''
* [[S:MyLanguage/J3.x:Developing a MVC Component/Adding language management#site.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]
+
* [[S:MyLanguage/J3.x:Developing an MVC Component/Adding language management#site.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Basic_backend#admin/helloworld.php|admin/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Basic_backend#admin/helloworld.php|admin/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Basic_backend#admin/controller.php|admin/controller.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Basic_backend#admin/controller.php|admin/controller.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/updates/mysql/0.0.6.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/updates/mysql/0.0.6.sql]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/fields/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/fields/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Using_the_database#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Basic_backend#admin/models/helloworlds.php|admin/models/helloworlds.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Basic_backend#admin/models/helloworlds.php|admin/models/helloworlds.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/models/helloworlds.php|admin/models/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_backend_actions#admin/models/helloworlds.php|admin/models/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/forms/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/forms/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_backend_actions#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_backend_actions#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/controllers/helloworlds.php|admin/controllers/helloworlds.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_backend_actions#admin/controllers/helloworlds.php|admin/controllers/helloworlds.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/controllers/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/controllers/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworld/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworld/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_backend_actions#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_backend_actions#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworld/tmpl/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworld/tmpl/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Basic_backend#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Basic_backend#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/tmpl/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/tmpl/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Basic_backend#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Basic_backend#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/tables/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/tables/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Using_the_database#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/language/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/language/index.html]]''
* ''[[S:MyLanguage/J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/language/en-GB/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/language/en-GB/index.html]]''
* ''[[S:MyLanguage/J3.x:Developing a MVC Component/Adding language management#admin.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]''
+
* ''[[S:MyLanguage/J3.x:Developing an MVC Component/Adding language management#admin.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]''
* ''[[S:MyLanguage/J3.x:Developing a MVC Component/Adding language management#admin.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]''
+
* ''[[S:MyLanguage/J3.x:Developing an MVC Component/Adding language management#admin.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]''
  
<translate>Create a compressed file of this directory or directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-9-adding-backend-actions.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.</translate>
+
<translate><!--T:23-->
 +
Create a compressed file of this directory or directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-9-adding-backend-actions.zip archive] and install it using the Extension Manager of Joomla. You can add a menu item of this component using the Menu Manager in the Backend.</translate>
  
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">
<tt>helloworld.xml</tt>
+
''helloworld.xml''
<source lang="xml" highlight="13,69,70">
+
<syntaxhighlight lang="xml" highlight="13,69,70">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2.0" method="upgrade">
+
<extension type="component" version="3.0" method="upgrade">
  
 
<name>COM_HELLOWORLD</name>
 
<name>COM_HELLOWORLD</name>
 
<!-- The following elements are optional and free of formatting constraints -->
 
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2014</creationDate>
+
<creationDate>January 2018</creationDate>
 
<author>John Doe</author>
 
<author>John Doe</author>
 
<authorEmail>john.doe@example.org</authorEmail>
 
<authorEmail>john.doe@example.org</authorEmail>
Line 642: Line 681:
  
 
</extension>
 
</extension>
</source>
+
</syntaxhighlight>
 
</span>
 
</span>
  
{{notice|<translate>Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.</translate>}}
+
{{notice|<translate><!--T:24-->
 +
Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code discrepancies or if editing any of the source code on this page.</translate>}}
  
<translate>== Contributors ==</translate>
+
<translate>== Contributors == <!--T:25--></translate>
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:oaksu|Ozgur Aksu]]
Line 654: Line 694:
 
*[[User:Scionescire|Scionescire]]
 
*[[User:Scionescire|Scionescire]]
  
<div class="row">  
+
<div class="row">
<div class="large-6 columns">{{Basic button|<translate>S:MyLanguage/J3.x:Developing_a_MVC_Component/Adding language management|Prev: Adding language management</translate>|class=expand success}}</div>
+
<div class="large-6 columns">{{Basic button|<translate><!--T:26-->
<div class="large-6 columns">{{Basic button|<translate>S:MyLanguage/J3.x:Developing_a_MVC_Component/Adding decorations to the backend|Next: Adding decorations to the backend</translate>|class=expand}}</div>
+
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding language management|Prev: Adding Language Management</translate>|class=expand success}}</div>
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:27-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding decorations to the backend|Next: Adding Decorations to the Backend</translate>|class=expand}}</div>
 
</div>
 
</div>
 
+
__NOTOC__
 
<noinclude>
 
<noinclude>
 
<translate>
 
<translate>
 +
<!--T:28-->
 
[[Category:Joomla! 3.x]]
 
[[Category:Joomla! 3.x]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.0]]

Latest revision as of 18:40, 11 November 2022

Other languages:
English • ‎español • ‎français • ‎italiano • ‎العربية • ‎中文(台灣)‎
Joomla! 
3.x
Tutorial
Developing an MVC Component



This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.

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 (the Articles in this series).



Introduction[edit]

This tutorial is part of the Developing an MVC Component for Joomla! 3.2 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

There is a series of five videos associated with this step in the tutorial, covering (1) Changes to the Helloworlds view and layout, (2) Handling Deletes, and then three on the functionality required to support New and Edit: (3) Handling New/Edit Redirect, (4) Display the Helloworld message editing form and (5) Handling Save and Cancel.

In conjunction with this step, you will probably find it useful to read this description of Joomla's implementation of MVC

Adding a Toolbar[edit]

In Joomla, the Administrator interacts generally with components through the use of a toolbar. In the file admin/views/helloworlds/view.html.php put this content. It will create a basic toolbar and a title for the component.

The arguments used in, for example, JToolbarHelper::addNew is used to set a controller instance which will be used after button is clicked. Further details below in the adding specific controllers section.

admin/views/helloworlds/view.html.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorlds View
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorlds extends JViewLegacy
{
	/**
	 * Display the Hello World view
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  void
	 */
	function display($tpl = null)
	{
		// Get data from the model
		$this->items		= $this->get('Items');
		$this->pagination	= $this->get('Pagination');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode('<br />', $errors));

			return false;
		}

		// Set the toolbar
		$this->addToolBar();

		// Display the template
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	protected function addToolBar()
	{
		JToolbarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'));
		JToolbarHelper::addNew('helloworld.add');
		JToolbarHelper::editList('helloworld.edit');
		JToolbarHelper::deleteList('', 'helloworlds.delete');
	}
}

You can find other classic backend actions in the administrator/includes/toolbar.php file of your Joomla installation.

Since the view can perform some actions, we have to add some input data. With your favourite file manager and editor, put in the file admin/views/helloworlds/tmpl/default.php

admin/views/helloworlds/tmpl/default.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
	<table class="table table-striped table-hover">
		<thead>
		<tr>
			<th width="1%"><?php echo JText::_('COM_HELLOWORLD_NUM'); ?></th>
			<th width="2%">
				<?php echo JHtml::_('grid.checkall'); ?>
			</th>
			<th width="90%">
				<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_NAME') ;?>
			</th>
			<th width="5%">
				<?php echo JText::_('COM_HELLOWORLD_PUBLISHED'); ?>
			</th>
			<th width="2%">
				<?php echo JText::_('COM_HELLOWORLD_ID'); ?>
			</th>
		</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="5">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
			<?php if (!empty($this->items)) : ?>
				<?php foreach ($this->items as $i => $row) :
					$link = JRoute::_('index.php?option=com_helloworld&task=helloworld.edit&id=' . $row->id);
				?>
					<tr>
						<td><?php echo $this->pagination->getRowOffset($i); ?></td>
						<td>
							<?php echo JHtml::_('grid.id', $i, $row->id); ?>
						</td>
						<td>
							<a href="<?php echo $link; ?>" title="<?php echo JText::_('COM_HELLOWORLD_EDIT_HELLOWORLD'); ?>">
								<?php echo $row->greeting; ?>
							</a>
						</td>
						<td align="center">
							<?php echo JHtml::_('jgrid.published', $row->published, $i, 'helloworlds.', true, 'cb'); ?>
						</td>
						<td align="center">
							<?php echo $row->id; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
	<input type="hidden" name="task" value=""/>
	<input type="hidden" name="boxchecked" value="0"/>
	<?php echo JHtml::_('form.token'); ?>
</form>

The underscore function of JRoute is used so that the site displays SEF URLs. See Supporting SEF URLs in your component.

The task field is used to define the controller.task parameter which will be sent to the server in the POST request when the form is submitted.

The boxchecked field is used to keep track of the number of checkboxes ticked.

Using the form token helps prevent CSRF attacks.

Adding Specific Controllers[edit]

Three actions have been added:

  • helloworlds.delete
  • helloworld.edit
  • helloworld.add

These are compound tasks (controller.task). So two new controllers HelloWorldControllerHelloWorlds and HelloWorldControllerHelloWorld have to be coded. (To understand the difference between the two types of subcontrollers read more in this article: subcontrollers...).

admin/controllers/helloworlds.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorlds Controller
 *
 * @since  0.0.1
 */
class HelloWorldControllerHelloWorlds extends JControllerAdmin
{
	/**
	 * Proxy for getModel.
	 *
	 * @param   string  $name    The model name. Optional.
	 * @param   string  $prefix  The class prefix. Optional.
	 * @param   array   $config  Configuration array for model. Optional.
	 *
	 * @return  object  The model.
	 *
	 * @since   1.6
	 */
	public function getModel($name = 'HelloWorld', $prefix = 'HelloWorldModel', $config = array('ignore_request' => true))
	{
		$model = parent::getModel($name, $prefix, $config);

		return $model;
	}
}

admin/controllers/helloworld.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorld Controller
 *
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 * @since       0.0.9
 */
class HelloWorldControllerHelloWorld extends JControllerForm
{
}

Adding an Editing View[edit]

With your favourite file manager and editor, put a file admin/views/helloworld/view.html.php containing:

admin/views/helloworld/view.html.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorld View
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorld extends JViewLegacy
{
	/**
	 * View form
	 *
	 * @var         form
	 */
	protected $form = null;

	/**
	 * Display the Hello World view
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  void
	 */
	public function display($tpl = null)
	{
		// Get the Data
		$this->form = $this->get('Form');
		$this->item = $this->get('Item');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode('<br />', $errors));

			return false;
		}


		// Set the toolbar
		$this->addToolBar();

		// Display the template
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	protected function addToolBar()
	{
		$input = JFactory::getApplication()->input;

		// Hide Joomla Administrator Main menu
		$input->set('hidemainmenu', true);

		$isNew = ($this->item->id == 0);

		if ($isNew)
		{
			$title = JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW');
		}
		else
		{
			$title = JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT');
		}

		JToolbarHelper::title($title, 'helloworld');
		JToolbarHelper::save('helloworld.save');
		JToolbarHelper::cancel(
			'helloworld.cancel',
			$isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE'
		);
	}
}

This view will display data using a layout.

Put a file admin/views/helloworld/tmpl/edit.php containing:

admin/views/helloworld/tmpl/edit.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('_JEXEC') or die('Restricted access');

?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id=' . (int) $this->item->id); ?>"
    method="post" name="adminForm" id="adminForm">
    <div class="form-horizontal">
        <fieldset class="adminform">
            <legend><?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_DETAILS'); ?></legend>
            <div class="row-fluid">
                <div class="span6">
                    <?php
                        foreach($this->form->getFieldset() as $field) {
                            echo $field->renderField();
                        }
                    ?>
                </div>
            </div>
        </fieldset>
    </div>
    <input type="hidden" name="task" value="helloworld.edit" />
    <?php echo JHtml::_('form.token'); ?>
</form>

Adding a Model and Modifying the Existing One[edit]

The HelloWorldViewHelloWorld view asks form and data from a model. This model has to provide a getTable, a getForm method and a loadData method (called from the JModelAdmin controller).

admin/models/helloworld.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorld Model
 *
 * @since  0.0.1
 */
class HelloWorldModelHelloWorld extends JModelAdmin
{
	/**
	 * Method to get a table object, load it if necessary.
	 *
	 * @param   string  $type    The table name. Optional.
	 * @param   string  $prefix  The class prefix. Optional.
	 * @param   array   $config  Configuration array for model. Optional.
	 *
	 * @return  JTable  A JTable object
	 *
	 * @since   1.6
	 */
	public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to get the record form.
	 *
	 * @param   array    $data      Data for the form.
	 * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
	 *
	 * @return  mixed    A JForm object on success, false on failure
	 *
	 * @since   1.6
	 */
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm(
			'com_helloworld.helloworld',
			'helloworld',
			array(
				'control' => 'jform',
				'load_data' => $loadData
			)
		);

		if (empty($form))
		{
			return false;
		}

		return $form;
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return  mixed  The data for the form.
	 *
	 * @since   1.6
	 */
	protected function loadFormData()
	{
		// Check the session for previously entered form data.
		$data = JFactory::getApplication()->getUserState(
			'com_helloworld.edit.helloworld.data',
			array()
		);

		if (empty($data))
		{
			$data = $this->getItem();
		}

		return $data;
	}
}

This model inherits from the JModelAdmin class and uses its loadForm method. This method searches for forms in the forms folder. With your favourite file manager and editor, put a file admin/models/forms/helloworld.xml containing:

admin/models/forms/helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<form>
	<fieldset>
		<field
				name="id"
				type="hidden"
				/>
		<field
				name="greeting"
				type="text"
				label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
				description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
				size="40"
				class="inputbox"
				default=""
				/>
	</fieldset>
</form>

Note: COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL and COM_HELLOWORLD_HELLOWORLD_GREETING_DESC are aliases for COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL and COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC respectively.

Here we are using different label and description to convey two different meanings in two different sections, but they can be easily kept equal.

Packaging the Component[edit]

Content of your code directory

Create a compressed file of this directory or directly download the archive and install it using the Extension Manager of Joomla. You can add a menu item of this component using the Menu Manager in the Backend.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">

	<name>COM_HELLOWORLD</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>January 2018</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<version>0.0.9</version>
	<!-- The description is optional and defaults to the name -->
	<description>COM_HELLOWORLD_DESCRIPTION</description>

	<install> <!-- Runs on install -->
		<sql>
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
		</sql>
	</install>
	<uninstall> <!-- Runs on uninstall -->
		<sql>
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
		</sql>
	</uninstall>
	<update> <!-- Runs on update; New since J2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>

	<!-- Site Main File Copy Section -->
	<!-- Note the folder attribute: This attribute describes the folder
		to copy FROM in the package to install therefore files copied
		in this section are copied from /site/ in the package -->
	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
		<folder>models</folder>
	</files>

        <languages folder="site/language">
		<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
        </languages>

	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld'>COM_HELLOWORLD_MENU</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
			<!-- tables files section -->
			<folder>tables</folder>
			<!-- models files section -->
			<folder>models</folder>
			<!-- views files section -->
			<folder>views</folder>
			<!-- controllers files section -->
			<folder>controllers</folder>
		</files>
		<languages folder="admin/language">
        		<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
                        <language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
		</languages>
	</administration>

</extension>

Info non-talk.png
General Information

Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code discrepancies or if editing any of the source code on this page.

Contributors[edit]