J3.x

Difference between revisions of "Developing an MVC Component/Adding configuration/fr"

From Joomla! Documentation

< J3.x:Developing an MVC Component
(Updating to match new version of source page)
 
(40 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
<noinclude><languages /></noinclude>
 
<noinclude><languages /></noinclude>
{{review}}
 
 
{{:J3.1:Developing an MVC Component/fr}}
 
{{:J3.1:Developing an MVC Component/fr}}
 
==Introduction==
 
==Introduction==
 
Ce didacticiel fait partie de la série de didacticiels sur le  [[S:MyLanguage/J3.2:Developing an MVC Component|Développement d'un Composant MVC pour Joomla! 3.x]]. Vous êtes invité à lire les articles précédents de cette série avant de lire celui-ci.
 
Ce didacticiel fait partie de la série de didacticiels sur le  [[S:MyLanguage/J3.2:Developing an MVC Component|Développement d'un Composant MVC pour Joomla! 3.x]]. Vous êtes invité à lire les articles précédents de cette série avant de lire celui-ci.
 +
 +
A cette étape, nous offrons à l'administrateur la possibilité de contrôler l'affichage de la catégorie sur le site public. Nous ferons cela en spécifiant un paramètre global de configuration qui peut être placé à "Afficher" ou "Masquer", mais aussi permet à l'administrateur de surcharger cette valeur sur la base d'un message individuel Helloworld. Pour permettre cela, nous allons introduire un champ paramètres (param) dans l'enregistrement dans la base de données, qui sera une chaîne de caractères au format JSON (ce qui est standard dans de nombreux composants Joomla du noyau), et nous devrons modifier le code qui l'accompagne pour réaliser cela.
 +
 +
Vous pouvez regarder une vidéo associée à cette étape ici [https://youtu.be/NCdjbzMrkgk Étape 13, Ajout de configuration] (en anglais).
 +
 +
{{#widget:YouTube|id=NCdjbzMrkgk}}
 +
 
== Ajouter des paramètres de configuration ==
 
== Ajouter des paramètres de configuration ==
 
Le framework Joomla! permet l'utilisation de paramètres stockés dans chaque composant. A l'aide de votre gestionnaire de fichiers et éditeur préférés, ajoutez un fichier <tt>admin/config.xml</tt> contenant ces lignes :
 
Le framework Joomla! permet l'utilisation de paramètres stockés dans chaque composant. A l'aide de votre gestionnaire de fichiers et éditeur préférés, ajoutez un fichier <tt>admin/config.xml</tt> contenant ces lignes :
Line 37: Line 43:
 
La meilleure façon de définir les paramètres est d'ajouter un bouton ''Préférences'' dans une barre d'outils.
 
La meilleure façon de définir les paramètres est d'ajouter un bouton ''Préférences'' dans une barre d'outils.
  
A l'aide de votre éditeur préféré, ajoutez ces lignes dans <tt>admin/views/helloworlds/view.html.php</tt>.
+
A l'aide de votre éditeur préféré, ajoutez ces lignes au fichier <tt>admin/views/helloworlds/view.html.php</tt> :
  
 
<span id="admin/views/helloworlds/view.html.php">
 
<span id="admin/views/helloworlds/view.html.php">
Line 47: Line 53:
 
  * @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 141: Line 147:
  
 
== Utiliser les paramètres de configuration comme valeur par défaut ==
 
== Utiliser les paramètres de configuration comme valeur par défaut ==
Nous voulons définir ce paramètre individuellement dans toutes les données HelloWorld. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes dans le fichier <tt>admin/models/forms/helloworld.xml</tt> :
+
Nous voulons définir ce paramètre individuellement dans toutes les données HelloWorld. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier <tt>admin/models/forms/helloworld.xml</tt> :
  
 
<span id="admin/models/forms/helloworld.xml">
 
<span id="admin/models/forms/helloworld.xml">
'''''admin/models/forms/helloworld.xml'''''
+
<tt>admin/models/forms/helloworld.xml</tt>
 
<source lang="xml" highlight="5-8,37-54">
 
<source lang="xml" highlight="5-8,37-54">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
Line 211: Line 217:
 
== Modifier le SQL ==
 
== Modifier le SQL ==
 
Les données contiennent désormais un nouveau paramètre : ''params''. La structure SQL doit être modifié.
 
Les données contiennent désormais un nouveau paramètre : ''params''. La structure SQL doit être modifié.
A l'aide de votre éditeur de texte préféré, ajoutez ces lignes dans <tt>admin/sql/installer.mysql.utf8.sql</tt> :
+
A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier <tt>admin/sql/installer.mysql.utf8.sql</tt> :
  
 
<span id="admin/sql/install.mysql.utf8.sql">
 
<span id="admin/sql/install.mysql.utf8.sql">
Line 237: Line 243:
 
</span>
 
</span>
  
A l'aide de votre éditeur de texte préféré, ajoutez ces lignes dans <tt>admin/sql/updates/mysql/0.0.13.sql</tt> :
+
A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier <tt>admin/sql/updates/mysql/0.0.13.sql</tt> :
  
 
<span id="admin/sql/updates/mysql/0.0.13.sql">
 
<span id="admin/sql/updates/mysql/0.0.13.sql">
Line 246: Line 252:
 
</span>
 
</span>
  
La table <tt>HelloWorld</tt> doit être modifiée afin de traiter ces paramètres : ils seront stockés dans un format JSON et auront une classe ''JParameter''. Nous devons surcharger les méthodes ''bind'' et ''load''. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes dans le fichier <tt>admin/tables/helloworld.php</tt>.
+
<tt>TableHelloWorld</tt> a été modifié afin de gérer ces paramètres&nbsp;; Ils seront stockés au format JSON. Nous devons surcharger la méthode ''bind'' pour convertir le tableau de ces paramètres en chaîne de caractères JSON pour la sauvegarder en base de données. Avec votre éditeur préféré, ajoutez ces lignes dans <tt>admin/tables/helloworld.php</tt>.
  
 
<span id="admin/tables/helloworld.php">
 
<span id="admin/tables/helloworld.php">
 
<tt>admin/tables/helloworld.php</tt>
 
<tt>admin/tables/helloworld.php</tt>
<source lang="php" highlight="28-71">
+
<source lang="php" highlight="28-46">
 
<?php
 
<?php
 
/**
 
/**
Line 256: Line 262:
 
  * @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 296: Line 302:
 
}
 
}
 
return parent::bind($array, $ignore);
 
return parent::bind($array, $ignore);
}
 
 
/**
 
* Overloaded load function
 
*
 
* @param      int $pk primary key
 
* @param      boolean $reset reset data
 
* @return      boolean
 
* @see JTable:load
 
*/
 
public function load($pk = null, $reset = true)
 
{
 
if (parent::load($pk, $reset))
 
{
 
// Convert the params field to a registry.
 
$params = new JRegistry;
 
$params->loadString($this->params, 'JSON');
 
 
$this->params = $params;
 
return true;
 
}
 
else
 
{
 
return false;
 
}
 
 
}
 
}
 
}
 
}
Line 328: Line 309:
  
 
== Modifier le backend ==
 
== Modifier le backend ==
The backend edit view has to display the options to the administrator. With your favorite editor, put these lines into the <tt>admin/views/helloworld/tmpl/edit.php</tt> file:
+
La vue ''edit'' du backend doit afficher les options à l'administrateur. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier <tt>admin/views/helloworld/tmpl/edit.php</tt> :
  
 
<span id="admin/views/helloworld/tmpl/edit.php">
 
<span id="admin/views/helloworld/tmpl/edit.php">
 
<tt>admin/views/helloworld/tmpl/edit.php</tt>
 
<tt>admin/views/helloworld/tmpl/edit.php</tt>
<source lang="php" highlight="17-31">
+
<source lang="php" highlight="12,17-31">
 
<?php
 
<?php
 
/**
 
/**
Line 338: Line 319:
 
  * @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 344: Line 325:
 
// No direct access
 
// No direct access
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.formvalidation');
+
JHtml::_('behavior.formvalidator');
 
?>
 
?>
 
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id=' . (int) $this->item->id); ?>"
 
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id=' . (int) $this->item->id); ?>"
Line 372: Line 353:
 
</span>
 
</span>
  
Note: The outer ''foreach'' retrieves all of the form fieldsets while the inner one gets the selected fielset fields.<br />
+
Remarque : Le premier ''foreach'' récupère tous les ''fieldsets'' du formulaire tandis que le second récupère les champs ''fieldset'' sélectionnés.<br />
This is the result of the <tt>$this->form->getFieldsets()</tt> command:
+
Voici le résultat de la commande <tt>$this->form->getFieldsets()</tt> :
  
 
<span>
 
<span>
Line 397: Line 378:
 
</span>
 
</span>
  
== Modifying the frontend ==
+
== Modifier le frontend ==
The frontend has to be modified according to the new ''show_category'' parameter.
+
Le frontend doit être modifié conformément au nouveau paramètre ''show_category''.
  
We have to modify the model:
+
Il nous faut modifier le modèle afin qu'il puisse :
* it has to merge global parameters and individual parameters
+
* fusionner les paramètres globaux et les paramètres individuels,
* it has to provide the category
+
* fournir la catégorie.
  
With your favorite editor, put these lines into the <tt>site/models/helloworld.php</tt> file:
+
A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier <tt>site/models/helloworld.php</tt> :
  
 
<span id="site/models/helloworld.php">
 
<span id="site/models/helloworld.php">
 
<tt>site/models/helloworld.php</tt>
 
<tt>site/models/helloworld.php</tt>
<source lang="php" highlight="20-47,65,96">
+
<source lang="php" highlight="20-47,65-96">
 
<?php
 
<?php
 
/**
 
/**
Line 414: Line 395:
 
  * @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 510: Line 491:
 
</span>
 
</span>
  
The view has to ask the model for the category. With your favorite editor, put these lines into the <tt>site/views/helloworld/view.html.php</tt>
+
La vue doit demander la catégorie au modèle. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier <tt>site/views/helloworld/view.html.php</tt> :
  
 
<span id="site/views/helloworld/view.html.php">
 
<span id="site/views/helloworld/view.html.php">
Line 520: Line 501:
 
  * @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 562: Line 543:
 
</span>
 
</span>
  
The layout can now display correctly the category or not. With your favorite editor, put these lines into <tt>site/views/helloworld/tmpl/default.php</tt>
+
Le rendu peut désormais afficher ou non la catégorie. A l'aide de votre votre éditeur de texte préféré, ajoutez ces lignes au fichier <tt>site/views/helloworld/tmpl/default.php</tt> :
  
 
<span id="site/views/helloworld/tmpl/default.php">
 
<span id="site/views/helloworld/tmpl/default.php">
Line 572: Line 553:
 
  * @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 586: Line 567:
 
</span>
 
</span>
  
== Adding some translation strings ==
+
== Ajouter des chaînes de traduction ==
Some strings have to be added in the <tt>admin/language/en-GB/en-GB.com_helloworld.ini</tt> file:
+
Certains chaînes doivent être ajoutées au fichier <tt>admin/language/en-GB/en-GB.com_helloworld.ini</tt> :
  
 
<span id="admin/language/en-GB/en-GB.com_helloworld.ini">
 
<span id="admin/language/en-GB/en-GB.com_helloworld.ini">
Line 593: Line 574:
 
<source lang="ini" highlight="22,23,38-40">
 
<source lang="ini" highlight="22,23,38-40">
 
; Joomla! Project
 
; Joomla! Project
; Copyright (C) 2005 - 2015 Open Source Matters. All rights reserved.
+
; Copyright (C) 2005 - 2018 Open Source Matters. All rights reserved.
 
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
 
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
 
; Note : All ini files need to be saved as UTF-8
 
; Note : All ini files need to be saved as UTF-8
Line 637: Line 618:
 
== Empaqueter le composant ==
 
== Empaqueter le composant ==
  
Contenu de votre répertoire de code
+
Contenu de votre répertoire de code. Chaque lien ci-dessous vous emmène vers l'étape du didacticiel qui contient la dernière version du code source de ce fichier.
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#helloworld.xml|helloworld.xml]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_configuration#helloworld.xml|helloworld.xml]]''
 
* ''[[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_an_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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]]''
Line 644: Line 625:
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_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_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_an_MVC_Component/Adding_configuration#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 
* ''[[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_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
 
* ''[[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_an_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/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_configuration#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_MVC_Component/Using_the_database#site/models/helloworld.php|site/models/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_configuration#site/models/helloworld.php|site/models/helloworld.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_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 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.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_an_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_an_MVC_Component/Basic_backend#admin/helloworld.php|admin/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_categories#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_configuration#admin/config.xml|admin/config.xml]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_configuration#admin/config.xml|admin/config.xml]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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]]''
Line 661: Line 642:
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/helpers/index.html]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/helpers/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_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
* ''[[J3.2:Developing_an_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
+
* ''[[J3.2:Developing_an_MVC_Component/Adding_configuration#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/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_an_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]]''
Line 671: Line 652:
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_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_an_MVC_Component/Using_the_database#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_categories#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Basic_backend#admin/models/helloworlds.php|admin/models/helloworlds.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_categories#admin/models/helloworlds.php|admin/models/helloworlds.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_an_MVC_Component/Adding_verifications#admin/models/helloworld.php|admin/models/helloworld.php]]''
 +
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding decorations to the backend#admin/models/forms/filter_helloworlds.xml|admin/models/forms/filter_helloworlds.xml]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_MVC_Component/Adding_verifications#admin/models/forms/helloworld.js|admin/models/forms/helloworld.js]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_verifications#admin/models/forms/helloworld.js|admin/models/forms/helloworld.js]]''
* ''[[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_an_MVC_Component/Adding_configuration#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_verifications#admin/models/rules/greeting.php|admin/models/rules/greeting.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_verifications#admin/models/rules/greeting.php|admin/models/rules/greeting.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/rules/index.html]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/rules/index.html]]''
Line 684: Line 666:
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_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_an_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_verifications#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/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]''
+
* ''[[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_an_MVC_Component/Adding_configuration#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_verifications#admin/views/helloworld/submitbutton.js|admin/views/helloworld/submitbutton.js]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_verifications#admin/views/helloworld/submitbutton.js|admin/views/helloworld/submitbutton.js]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_MVC_Component/Basic_backend#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_configuration#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
 
* ''[[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_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/tmpl/index.html]]''
* ''[[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_an_MVC_Component/Adding_categories#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_MVC_Component/Using_the_database#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_configuration#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_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_an_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 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 an MVC Component/Adding_configuration#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.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]''
+
* ''[[S:MyLanguage/J3.x:Developing an MVC Component/Adding_decorations_to_the_backend#admin.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|media/index.html]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|media/index.html]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|media/images/index.html]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|media/images/index.html]]''
Line 702: Line 685:
 
* ''[[S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_decorations_to_the_backend#Adding_some_icons|media/images/tux-48x48.png]]''
 
* ''[[S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_decorations_to_the_backend#Adding_some_icons|media/images/tux-48x48.png]]''
  
Créez un fichier compressé de ce répertoire ou téléchargez directement l'[https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-13-adding-configuration.zip archive] et installez-le en utilisant le gestionnaire des extensions Joomla. Vous pouvez ajouter un élément de menu pour ce composant à l'aide du gestionnaire de menus dans le backend.
+
Créez un fichier compressé de ce répertoire ou téléchargez directement l'[https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-13-adding-configuration.zip archive] et installez-le en utilisant le gestionnaire des extensions Joomla. Vous pouvez ajouter un lien de menu pour ce composant à l'aide du gestionnaire de menus dans le backend.
  
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">
Line 708: Line 691:
 
<source lang="xml" highlight="13,64">
 
<source lang="xml" highlight="13,64">
 
<?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>February 2015</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 801: Line 784:
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:Scionescire|Scionescire]]
 
*[[User:Scionescire|Scionescire]]
 +
*[[User:Robbiej|Robbie Jackson]]
  
 
<div class="row">  
 
<div class="row">  
Line 808: Line 792:
 
__NOTOC__
 
__NOTOC__
 
<noinclude>
 
<noinclude>
[[Category:Joomla! 3.x/fr|Joomla! 3.x]]
+
[[Category:Joomla! 3.x{{#translation:}}]]
[[Category:Joomla! 3.0/fr|Joomla! 3.0]]
+
[[Category:Joomla! 3.0{{#translation:}}]]
[[Category:Joomla! 3.1/fr|Joomla! 3.1]]
+
[[Category:Joomla! 3.1{{#translation:}}]]
[[Category:Joomla! 3.2/fr|Joomla! 3.2]]
+
[[Category:Joomla! 3.2{{#translation:}}]]
[[Category:Joomla! 3.3/fr|Joomla! 3.3]]
+
[[Category:Joomla! 3.3{{#translation:}}]]
[[Category:Joomla! 3.4/fr|Joomla! 3.4]]
+
[[Category:Joomla! 3.4{{#translation:}}]]
[[Category:Beginner Development/fr|Développement pour débutants]]
+
[[Category:Beginner Development{{#translation:}}]]
[[Category:Component Development/fr|Développement de composants]]
+
[[Category:Component Development{{#translation:}}]]
[[Category:Tutorials/fr|Didacticiels]]
+
[[Category:Tutorials{{#translation:}}]]
[[Category:Tutorials in a Series/fr|Didacticiels en série]]
+
[[Category:Tutorials in a Series{{#translation:}}]]
 
</noinclude>
 
</noinclude>

Latest revision as of 02:10, 24 February 2020

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎العربية • ‎中文(台灣)‎
Joomla! 
3.x
Didacticiel
Développement d'un composant MVC

Ajout d'une requête de variable dans le type de menu

Utilisation de la base de données

Backend de base

Ajout de la gestion des langues

Ajout d'actions en backend

Ajout de décorations pour le backend

Ajout de vérifications

Ajout de catégories

Ajout de configuration

  1. Ajout d'ACL

Ajout d'un fichier script installation/désinstallation/mise à jour

Ajout d'un formulaire de frontend

  1. Ajout d'une image
  2. Ajout d'un plan
  3. Ajout d'AJAX
  4. Ajout d'un alias

Utilisation du filtre de langues

  1. Ajouter une fenêtre modale
  2. Ajout d'associations
  3. Ajout de Checkout
  4. Ajout d'un filtre
  5. Ajout de niveaux
  6. Ajout de versions
  7. Ajout de tags
  8. Ajout d'accès
  9. Ajout d'un processus de traitement
  10. Ajout d'un cache
  11. Ajout d'un fil d'actualité

Ajout d'un serveur de mise à jour

  1. Adding Custom Fields
  2. Upgrading to Joomla4



Ceci est une série qui regroupe plusieurs articles pour devenir un didacticiel sur la façon de développer un Composant pour Joomla! Joomla 3.x suivant le principe Modèle-Vue-Contrôleur.

Commencez avec l'introduction, et naviguez dans les articles de cette série soit à l'aide des boutons de navigation en bas des articles, soit grâce au menu de droite : Les articles de cette série.



Introduction

Ce didacticiel fait partie de la série de didacticiels sur le Développement d'un Composant MVC pour Joomla! 3.x. Vous êtes invité à lire les articles précédents de cette série avant de lire celui-ci.

A cette étape, nous offrons à l'administrateur la possibilité de contrôler l'affichage de la catégorie sur le site public. Nous ferons cela en spécifiant un paramètre global de configuration qui peut être placé à "Afficher" ou "Masquer", mais aussi permet à l'administrateur de surcharger cette valeur sur la base d'un message individuel Helloworld. Pour permettre cela, nous allons introduire un champ paramètres (param) dans l'enregistrement dans la base de données, qui sera une chaîne de caractères au format JSON (ce qui est standard dans de nombreux composants Joomla du noyau), et nous devrons modifier le code qui l'accompagne pour réaliser cela.

Vous pouvez regarder une vidéo associée à cette étape ici Étape 13, Ajout de configuration (en anglais).

Ajouter des paramètres de configuration

Le framework Joomla! permet l'utilisation de paramètres stockés dans chaque composant. A l'aide de votre gestionnaire de fichiers et éditeur préférés, ajoutez un fichier admin/config.xml contenant ces lignes :

admin/config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
	<fieldset
		name="greetings"
		label="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL"
		description="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC"
	>
		<field
			name="show_category"
			type="radio"
			label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
			default="0"
		>
			<option value="0">JHIDE</option>
			<option value="1">JSHOW</option>
		</field>
	</fieldset>
</config>

Ce fichier sera lu par le composant natif de Joomla : com_config. Pour l'instant, nous avons défini un seul paramètre : le titre de la catégorie à afficher ou non en frontend.

La meilleure façon de définir les paramètres est d'ajouter un bouton Préférences dans une barre d'outils.

A l'aide de votre éditeur préféré, ajoutez ces lignes au fichier admin/views/helloworlds/view.html.php :

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 application
		$app = JFactory::getApplication();
		$context = "helloworld.list.admin.helloworld";
		// Get data from the model
		$this->items			= $this->get('Items');
		$this->pagination		= $this->get('Pagination');
		$this->state			= $this->get('State');
		$this->filter_order 	= $app->getUserStateFromRequest($context.'filter_order', 'filter_order', 'greeting', 'cmd');
		$this->filter_order_Dir = $app->getUserStateFromRequest($context.'filter_order_Dir', 'filter_order_Dir', 'asc', 'cmd');
		$this->filterForm    	= $this->get('FilterForm');
		$this->activeFilters 	= $this->get('ActiveFilters');

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

			return false;
		}

		// Set the submenu
		HelloWorldHelper::addSubmenu('helloworlds');

		// Set the toolbar and number of found items
		$this->addToolBar();

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

		// Set the document
		$this->setDocument();
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	protected function addToolBar()
	{
		$title = JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS');

		if ($this->pagination->total)
		{
			$title .= "<span style='font-size: 0.5em; vertical-align: middle;'>(" . $this->pagination->total . ")</span>";
		}

		JToolBarHelper::title($title, 'helloworld');
		JToolBarHelper::addNew('helloworld.add');
		JToolBarHelper::editList('helloworld.edit');
		JToolBarHelper::deleteList('', 'helloworlds.delete');
		JToolBarHelper::preferences('com_helloworld');
	}
	/**
	 * Method to set up the document properties
	 *
	 * @return void
	 */
	protected function setDocument() 
	{
		$document = JFactory::getDocument();
		$document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION'));
	}
}

Utiliser les paramètres de configuration comme valeur par défaut

Nous voulons définir ce paramètre individuellement dans toutes les données HelloWorld. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier admin/models/forms/helloworld.xml :

admin/models/forms/helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<form
				addrulepath="/administrator/components/com_helloworld/models/rules"
>
	<fieldset
				name="details"
				label="COM_HELLOWORLD_HELLOWORLD_DETAILS"
	>
		<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 validate-greeting"
				validate="greeting"
				required="true"
				default=""
				/>
		<field
				name="catid"
				type="category"
				extension="com_helloworld"
				class="inputbox"
				default=""
				label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL"
				description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC"
				required="true"
		>
			<option value="0">JOPTION_SELECT_CATEGORY</option>
		</field>
	</fieldset>
	<fields name="params">
		<fieldset
				name="params"
				label="JGLOBAL_FIELDSET_DISPLAY_OPTIONS"
		>
			<field
					name="show_category"
					type="list"
					label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
					description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
					default=""
			>
				<option value="">JGLOBAL_USE_GLOBAL</option>
				<option value="0">JHIDE</option>
				<option value="1">JSHOW</option>
			</field>
		</fieldset>
	</fields>
</form>

Nous définissons le même paramètre pour chaque message avec une valeur supplémentaire: Use global.

Remarque: le fieldset details possède désormais un label (identique à celui définit précédemment dans la vue edit).

Modifier le SQL

Les données contiennent désormais un nouveau paramètre : params. La structure SQL doit être modifié. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier admin/sql/installer.mysql.utf8.sql :

admin/sql/install.mysql.utf8.sql

DROP TABLE IF EXISTS `#__helloworld`;

CREATE TABLE `#__helloworld` (
	`id`       INT(11)     NOT NULL AUTO_INCREMENT,
	`greeting` VARCHAR(25) NOT NULL,
	`published` tinyint(4) NOT NULL,
	`catid`	    int(11)    NOT NULL DEFAULT '0',
	`params`   VARCHAR(1024) NOT NULL DEFAULT '',
	PRIMARY KEY (`id`)
)
	ENGINE =MyISAM
	AUTO_INCREMENT =0
	DEFAULT CHARSET =utf8;

INSERT INTO `#__helloworld` (`greeting`) VALUES
('Hello World!'),
('Good bye World!');

A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier admin/sql/updates/mysql/0.0.13.sql :

admin/sql/updates/mysql/0.0.13.sql

ALTER TABLE `#__helloworld` ADD `params` VARCHAR(1024) NOT NULL DEFAULT '';

TableHelloWorld a été modifié afin de gérer ces paramètres ; Ils seront stockés au format JSON. Nous devons surcharger la méthode bind pour convertir le tableau de ces paramètres en chaîne de caractères JSON pour la sauvegarder en base de données. Avec votre éditeur préféré, ajoutez ces lignes dans admin/tables/helloworld.php.

admin/tables/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
defined('_JEXEC') or die('Restricted access');

/**
 * Hello Table class
 *
 * @since  0.0.1
 */
class HelloWorldTableHelloWorld extends JTable
{
	/**
	 * Constructor
	 *
	 * @param   JDatabaseDriver  &$db  A database connector object
	 */
	function __construct(&$db)
	{
		parent::__construct('#__helloworld', 'id', $db);
	}
	/**
	 * Overloaded bind function
	 *
	 * @param       array           named array
	 * @return      null|string     null is operation was satisfactory, otherwise returns an error
	 * @see JTable:bind
	 * @since 1.5
	 */
	public function bind($array, $ignore = '')
	{
		if (isset($array['params']) && is_array($array['params']))
		{
			// Convert the params field to a string.
			$parameter = new JRegistry;
			$parameter->loadArray($array['params']);
			$array['params'] = (string)$parameter;
		}
		return parent::bind($array, $ignore);
	}
}

Modifier le backend

La vue edit du backend doit afficher les options à l'administrateur. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier admin/views/helloworld/tmpl/edit.php :

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');
JHtml::_('behavior.formvalidator');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id=' . (int) $this->item->id); ?>"
    method="post" name="adminForm" id="adminForm" class="form-validate">
	<div class="form-horizontal">
		<?php foreach ($this->form->getFieldsets() as $name => $fieldset): ?>
			<fieldset class="adminform">
				<legend><?php echo JText::_($fieldset->label); ?></legend>
				<div class="row-fluid">
					<div class="span6">
						<?php foreach ($this->form->getFieldset($name) as $field): ?>
							<div class="control-group">
								<div class="control-label"><?php echo $field->label; ?></div>
								<div class="controls"><?php echo $field->input; ?></div>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			</fieldset>
		<?php endforeach; ?>
	</div>
	<input type="hidden" name="task" value="helloworld.edit" />
	<?php echo JHtml::_('form.token'); ?>
</form>

Remarque : Le premier foreach récupère tous les fieldsets du formulaire tandis que le second récupère les champs fieldset sélectionnés.
Voici le résultat de la commande $this->form->getFieldsets() :

Array
(
    [details] => stdClass Object
        (
            [name] => details
            [label] => COM_HELLOWORLD_HELLOWORLD_DETAILS
            [description] => 
        )

    [params] => stdClass Object
        (
            [name] => params
            [label] => JGLOBAL_FIELDSET_DISPLAY_OPTIONS
            [description] => 
        )

)

Modifier le frontend

Le frontend doit être modifié conformément au nouveau paramètre show_category.

Il nous faut modifier le modèle afin qu'il puisse :

  • fusionner les paramètres globaux et les paramètres individuels,
  • fournir la catégorie.

A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier site/models/helloworld.php :

site/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 JModelItem
{
	/**
	 * @var object item
	 */
	protected $item;

	/**
	 * Method to auto-populate the model state.
	 *
	 * This method should only be called once per instantiation and is designed
	 * to be called on the first call to the getState() method unless the model
	 * configuration flag to ignore the request is set.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @return	void
	 * @since	2.5
	 */
	protected function populateState()
	{
		// Get the message id
		$jinput = JFactory::getApplication()->input;
		$id     = $jinput->get('id', 1, 'INT');
		$this->setState('message.id', $id);

		// Load the parameters.
		$this->setState('params', JFactory::getApplication()->getParams());
		parent::populateState();
	}

	/**
	 * 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);
	}

	/**
	 * Get the message
	 * @return object The message to be displayed to the user
	 */
	public function getItem()
	{
		if (!isset($this->item)) 
		{
			$id    = $this->getState('message.id');
			$db    = JFactory::getDbo();
			$query = $db->getQuery(true);
			$query->select('h.greeting, h.params, c.title as category')
				  ->from('#__helloworld as h')
				  ->leftJoin('#__categories as c ON h.catid=c.id')
				  ->where('h.id=' . (int)$id);
			$db->setQuery((string)$query);
		
			if ($this->item = $db->loadObject()) 
			{
				// Load the JSON string
				$params = new JRegistry;
				$params->loadString($this->item->params, 'JSON');
				$this->item->params = $params;

				// Merge global params with item params
				$params = clone $this->getState('params');
				$params->merge($this->item->params);
				$this->item->params = $params;
			}
		}
		return $this->item;
	}
}

La vue doit demander la catégorie au modèle. A l'aide de votre éditeur de texte préféré, ajoutez ces lignes au fichier site/views/helloworld/view.html.php :

site/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');

/**
 * HTML View class for the HelloWorld Component
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorld 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)
	{
		// Assign data to the view
		$this->item = $this->get('Item');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

			return false;
		}

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

Le rendu peut désormais afficher ou non la catégorie. A l'aide de votre votre éditeur de texte préféré, ajoutez ces lignes au fichier site/views/helloworld/tmpl/default.php :

site/views/helloworld/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');
?>
<h1><?php echo $this->item->greeting.(($this->item->category and $this->item->params->get('show_category'))
                                      ? (' ('.$this->item->category.')') : ''); ?>
</h1>

Ajouter des chaînes de traduction

Certains chaînes doivent être ajoutées au fichier admin/language/en-GB/en-GB.com_helloworld.ini :

admin/language/en-GB/en-GB.com_helloworld.ini

; Joomla! Project
; Copyright (C) 2005 - 2018 Open Source Matters. All rights reserved.
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
COM_HELLOWORLD_ADMINISTRATION_CATEGORIES="HelloWorld - Categories"
COM_HELLOWORLD_NUM="#"
COM_HELLOWORLD_HELLOWORLDS_FILTER="Filters"
COM_HELLOWORLD_PUBLISHED="Published"
COM_HELLOWORLD_HELLOWORLDS_NAME="Name"
COM_HELLOWORLD_ID="Id"

COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
COM_HELLOWORLD_HELLOWORLD_DETAILS="Details"
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"
COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE="Some values are unacceptable"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC="The category the messages belongs to"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL="Category"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC="This message will be displayed"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL="Message"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL="Show category"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC="If set to Show, the title of the message&rsquo;s category will show."
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING="Greeting"
COM_HELLOWORLD_HELLOWORLD_HEADING_ID="Id"
COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT="HelloWorld manager: Edit Message"
COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW="HelloWorld manager: New Message"
COM_HELLOWORLD_MANAGER_HELLOWORLDS="HelloWorld manager"
COM_HELLOWORLD_EDIT_HELLOWORLD="Edit message"
COM_HELLOWORLD_N_ITEMS_DELETED_1="One message deleted"
COM_HELLOWORLD_N_ITEMS_DELETED_MORE="%d messages deleted"
COM_HELLOWORLD_N_ITEMS_PUBLISHED="%d message(s) published"
COM_HELLOWORLD_N_ITEMS_UNPUBLISHED="%d message(s) unpublished"
COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL="Greeting"
COM_HELLOWORLD_HELLOWORLD_GREETING_DESC="Add Hello World Greeting"
COM_HELLOWORLD_SUBMENU_MESSAGES="Messages"
COM_HELLOWORLD_SUBMENU_CATEGORIES="Categories"
COM_HELLOWORLD_CONFIGURATION="HelloWorld Configuration"
COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL="Messages settings"
COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC="Settings that will be applied to all messages by default"

Empaqueter le composant

Contenu de votre répertoire de code. Chaque lien ci-dessous vous emmène vers l'étape du didacticiel qui contient la dernière version du code source de ce fichier.

Créez un fichier compressé de ce répertoire ou téléchargez directement l'archive et installez-le en utilisant le gestionnaire des extensions Joomla. Vous pouvez ajouter un lien de menu pour ce composant à l'aide du gestionnaire de menus dans le 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.13</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>

	<media destination="com_helloworld" folder="media">
		<filename>index.html</filename>
		<folder>images</folder>
	</media>

	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld' img="../media/com_helloworld/images/tux-16x16.png">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>config.xml</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
			<filename>access.xml</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>
			<!-- helpers files section -->
			<folder>helpers</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>

Contributeurs