J3.x

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

From Joomla! Documentation

< J3.x:Developing an MVC Component
 
(20 intermediate revisions by 4 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.
Le framework de Joomla! a implémenté l'utilisation des catégories pour tous les composants. L'ajout de la capacité de catégorisation à un composant est assez simple.
+
Vous pouvez regarder une vidéo associée à cette étape ici [https://youtu.be/dd-2AcpqRg4 Étape 12, ajout de catégories].
 +
 
 +
{{#widget:YouTube|id=dd-2AcpqRg4}}
 +
 
 +
Dans cette étape, nous utiliserons la fonctionnalité Catégories de Joomla pour nous permettre de définir un ensemble de catégories à utiliser avec notre composant Helloworld et pour associer chaque message d'accueil Helloworld à l'une de ces catégories. Une grande partie de cela sera mis en œuvre de manière similaire à d'autres composants, tels que Articles (com_content) et Bannières.
 +
 
 +
Il y a plusieurs aspects à cela:
 +
 
 +
# Nous devons lier notre table de base de données Helloworld avec la table de Catégories Joomlaǃ&nbsp;; ceci va impacter notre installation SQL et nos scripts de mise à jour.
 +
# Nous allons mettre à jour notre formulaire d'édition de Helloworld pour autoriser un administrateur à associer une catégorie avec n'importe lequel des messages d'accueil de Helloworld. Ceci va impacter la définition XML de notre formulaire d'édition Helloworld.
 +
# Jusqu'à maintenant, quand les administrateurs définissaient un élément de menu de type Helloworld, il leur était demandé de choisir le message d'accueil à afficher à partir de la liste des messages d'accueil disponibles. Nous allons améliorer cette liste pour montrer la catégorie associée entre parenthèses après le message d'accueil, à condition que ce message d'accueil ait une catégorie définie. Ceci va impacter le code du modèle FieldList dans admin/models/fields/helloworld.php.
 +
# Nous allons autoriser l'administrateur à définir les catégories associées à notre composant Helloworld. Cette fonctionnalité nous est fournie directement par le composant Joomla com_categories. Il génère le formulaire qui permet aux administrateurs de faire cela – nous devons juste fournir les liens de menus pointant vers ce formulaire au travers du sous-menu décrit plus bas et à travers le menu Composants, et de plus, nous devons définir les droits d'accès à cette fonctionnalité.
 +
# Nous allons modifier le formulaire Helloworlds qui montre tous les messages Helloworld afin que la catégorie associée à un message soit affichée. Ceci impactera notre modèle (car nous devrons faire une jointure SQL avec la table des catégories afin d'obtenir le titre de la catégorie) et notre fichier de rendu (où nous allons afficher les catégories associées).
 +
# Nous créerons un sous-menu qui peut se fermer vers la gauche comme une barre latérale, et qui montrera deux éléments&nbsp;:
 +
#* Messages – qui permettra l'accès à tous nos messages Helloworld
 +
#* Catégories – qui permettra l’accès au formulaire permettant à l'administrateur de définir la liste de catégories du composant Helloworld.
 +
#: Ceci va affecter notre vue Helloworlds (où nous mettrons notre barre latérale) et le rendu (où nous afficherons notre barre latérale), ainsi que définir le sous-menu de la barre latérale lui-même dans un nouveau fichier Helper. Nous allons aussi ajouter une ligne dans notre fichier principal d'administration helloworld.php pour dire à Joomla de charger et exécuter ce fichier helper.
 +
 
 +
'''Remarque importante (avec le recul).''' Cette étape permet de créer des catégories dans le composant helloworld en leur permettant d'être facultatives. Cela diffère des composants Joomla principaux dans lesquels une catégorie doit toujours être définie et où, par défaut, les éléments ont une catégorie "non catégorisée". Si vous autorisez les catégories à être facultatives, vous rencontrerez probablement des difficultés ultérieurement, car certaines fonctions essentielles de la bibliothèque Joomla supposent qu'elles sont obligatoires. Si cette série de didacticiels est mise à jour pour les versions ultérieures de Joomla, il serait préférable d’inclure des catégories d’une manière qui suive le modèle standard Joomla.  
  
 
== Modification du SQL ==
 
== Modification du SQL ==
Line 98: Line 115:
 
  * @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 171: Line 188:
 
  * @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 187: Line 204:
 
  * @since  1.6
 
  * @since  1.6
 
  */
 
  */
abstract class HelloWorldHelper
+
abstract class HelloWorldHelper extends JHelperContent
 
{
 
{
 
/**
 
/**
Line 197: Line 214:
 
public static function addSubmenu($submenu)  
 
public static function addSubmenu($submenu)  
 
{
 
{
JSubMenuHelper::addEntry(
+
JHtmlSidebar::addEntry(
 
JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
 
JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
 
'index.php?option=com_helloworld',
 
'index.php?option=com_helloworld',
$submenu == 'messages'
+
$submenu == 'helloworlds'
 
);
 
);
  
JSubMenuHelper::addEntry(
+
JHtmlSidebar::addEntry(
 
JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
 
JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
 
'index.php?option=com_categories&view=categories&extension=com_helloworld',
 
'index.php?option=com_categories&view=categories&extension=com_helloworld',
Line 235: Line 252:
 
  * @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 270: Line 287:
  
 
La classe css ''.icon-48-helloworld'' est désormais définie dans la fonction ''addSubmenu''.
 
La classe css ''.icon-48-helloworld'' est désormais définie dans la fonction ''addSubmenu''.
Il nous faut maintenant appeler cette fonction dans la vue ''hellowords'' :
+
Il nous faut maintenant appeler cette fonction dans la vue ''helloworlds'' :
  
 
<span id="admin/views/helloworlds/view.html.php">
 
<span id="admin/views/helloworlds/view.html.php">
Line 280: Line 297:
 
  * @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 370: Line 387:
 
}
 
}
 
}
 
}
 +
</source>
 +
</span>
 +
 +
== Obtenir les titres de catégories associés ==
 +
Mettez à jour le modèle helloworlds de sorte qu'il fasse une jointure SQL avec la table Catégories de Joomla pour rechercher le titre de la catégorie associée.
 +
 +
<span id="admin/models/helloworlds.php">
 +
<tt>admin/models/helloworlds.php</tt>
 +
<source lang="php" highlight="53-58,74,78">
 +
<?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');
 +
 +
/**
 +
* HelloWorldList Model
 +
*
 +
* @since  0.0.1
 +
*/
 +
class HelloWorldModelHelloWorlds extends JModelList
 +
{
 +
/**
 +
* Constructor.
 +
*
 +
* @param  array  $config  An optional associative array of configuration settings.
 +
*
 +
* @see    JController
 +
* @since  1.6
 +
*/
 +
public function __construct($config = array())
 +
{
 +
if (empty($config['filter_fields']))
 +
{
 +
$config['filter_fields'] = array(
 +
'id',
 +
'greeting',
 +
'published'
 +
);
 +
}
 +
 +
parent::__construct($config);
 +
}
 +
 +
/**
 +
* Method to build an SQL query to load the list data.
 +
*
 +
* @return      string  An SQL query
 +
*/
 +
protected function getListQuery()
 +
{
 +
// Initialize variables.
 +
$db    = JFactory::getDbo();
 +
$query = $db->getQuery(true);
 +
 +
// Create the base select statement.
 +
$query->select('a.id as id, a.greeting as greeting, a.published as published')
 +
  ->from($db->quoteName('#__helloworld', 'a'));
 +
 +
// Join over the categories.
 +
$query->select($db->quoteName('c.title', 'category_title'))
 +
->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON c.id = a.catid');
 +
 +
// Filter: like / search
 +
$search = $this->getState('filter.search');
 +
 +
if (!empty($search))
 +
{
 +
$like = $db->quote('%' . $search . '%');
 +
$query->where('greeting LIKE ' . $like);
 +
}
 +
 +
// Filter by published state
 +
$published = $this->getState('filter.published');
 +
 +
if (is_numeric($published))
 +
{
 +
$query->where('a.published = ' . (int) $published);
 +
}
 +
elseif ($published === '')
 +
{
 +
$query->where('(a.published IN (0, 1))');
 +
}
 +
 +
// Add the list ordering clause.
 +
$orderCol = $this->state->get('list.ordering', 'greeting');
 +
$orderDirn = $this->state->get('list.direction', 'asc');
 +
 +
$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));
 +
 +
return $query;
 +
}
 +
}
 +
 +
</source>
 +
</span>
 +
 +
== Mise à jour de la mise en page ==
 +
Mettez à jour le fichier de mise en page helloworlds pour afficher la barre latérale et la catégorie associée au message d'accueil.
 +
 +
<span id="admin/views/helloworlds/tmpl/default.php">
 +
<tt>admin/views/helloworlds/tmpl/default.php</tt>
 +
<source lang="php" highlight="19-22,73-75,93">
 +
<?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');
 +
 +
JHtml::_('formbehavior.chosen', 'select');
 +
 +
$listOrder    = $this->escape($this->filter_order);
 +
$listDirn      = $this->escape($this->filter_order_Dir);
 +
?>
 +
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
 +
<div id="j-sidebar-container" class="span2">
 +
<?php echo JHtmlSidebar::render(); ?>
 +
</div>
 +
<div id="j-main-container" class="span10">
 +
<div class="row-fluid">
 +
<div class="span6">
 +
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_FILTER'); ?>
 +
<?php
 +
echo JLayoutHelper::render(
 +
'joomla.searchtools.default',
 +
array('view' => $this)
 +
);
 +
?>
 +
</div>
 +
</div>
 +
<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 JHtml::_('grid.sort', 'COM_HELLOWORLD_HELLOWORLDS_NAME', 'greeting', $listDirn, $listOrder); ?>
 +
</th>
 +
<th width="5%">
 +
<?php echo JHtml::_('grid.sort', 'COM_HELLOWORLD_PUBLISHED', 'published', $listDirn, $listOrder); ?>
 +
</th>
 +
<th width="2%">
 +
<?php echo JHtml::_('grid.sort', 'COM_HELLOWORLD_ID', 'id', $listDirn, $listOrder); ?>
 +
</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>
 +
<div class="small">
 +
<?php echo JText::_('JCATEGORY') . ': ' . $this->escape($row->category_title); ?>
 +
</div>
 +
</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"/>
 +
<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>"/>
 +
<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>"/>
 +
<?php echo JHtml::_('form.token'); ?>
 +
</div>
 +
</form>
 
</source>
 
</source>
 
</span>
 
</span>
Line 403: Line 622:
 
<source lang="ini" highlight="7,17-19,34,35">
 
<source lang="ini" highlight="7,17-19,34,35">
 
; 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 443: Line 662:
 
== 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.
 
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[#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]]''
Line 460: Line 679:
 
* [[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/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_an_MVC_Component/Adding_categories#admin/access.xml|admin/access.xml]]''
 
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_categories#admin/access.xml|admin/access.xml]]''
Line 466: Line 685:
 
* ''[[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]]''
* ''[[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_an_MVC_Component/Adding_categories#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 475: Line 694:
 
* ''[[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_categories#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 488: Line 708:
 
* ''[[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_verifications#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_categories#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/Using_the_database#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
 
* ''[[#admin/language/index.html|admin/language/index.html]]''
 
* ''[[#admin/language/index.html|admin/language/index.html]]''
 
* ''[[#admin/language/en-GB/index.html|admin/language/en-GB/index.html]]''
 
* ''[[#admin/language/en-GB/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_categories#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 512: Line 733:
 
<source lang="xml" highlight="13,66,77,78">
 
<source lang="xml" highlight="13,66,77,78">
 
<?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 607: Line 828:
 
*[[User:Scionescire|Scionescire]]
 
*[[User:Scionescire|Scionescire]]
 
*[[User:Veronika|Veronika Patel]]
 
*[[User:Veronika|Veronika Patel]]
 +
*[[User:Robbiej|Robbie Jackson]]
  
 
<div class="row">  
 
<div class="row">  
Line 614: Line 836:
 
__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:Joomla! 3.5{{#translation:}}]]
[[Category:Component Development/fr|Développement de composants]]
+
[[Category:Joomla! 3.6{{#translation:}}]]
[[Category:Tutorials/fr|Didacticiels]]
+
[[Category:Beginner Development{{#translation:}}]]
[[Category:Tutorials in a Series/fr|Didacticiels en série]]
+
[[Category:Component Development{{#translation:}}]]
 +
[[Category:Tutorials{{#translation:}}]]
 +
[[Category:Tutorials in a Series{{#translation:}}]]
 
</noinclude>
 
</noinclude>

Latest revision as of 08:48, 2 April 2019

Other languages:
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. Vous pouvez regarder une vidéo associée à cette étape ici Étape 12, ajout de catégories.

Dans cette étape, nous utiliserons la fonctionnalité Catégories de Joomla pour nous permettre de définir un ensemble de catégories à utiliser avec notre composant Helloworld et pour associer chaque message d'accueil Helloworld à l'une de ces catégories. Une grande partie de cela sera mis en œuvre de manière similaire à d'autres composants, tels que Articles (com_content) et Bannières.

Il y a plusieurs aspects à cela:

  1. Nous devons lier notre table de base de données Helloworld avec la table de Catégories Joomlaǃ ; ceci va impacter notre installation SQL et nos scripts de mise à jour.
  2. Nous allons mettre à jour notre formulaire d'édition de Helloworld pour autoriser un administrateur à associer une catégorie avec n'importe lequel des messages d'accueil de Helloworld. Ceci va impacter la définition XML de notre formulaire d'édition Helloworld.
  3. Jusqu'à maintenant, quand les administrateurs définissaient un élément de menu de type Helloworld, il leur était demandé de choisir le message d'accueil à afficher à partir de la liste des messages d'accueil disponibles. Nous allons améliorer cette liste pour montrer la catégorie associée entre parenthèses après le message d'accueil, à condition que ce message d'accueil ait une catégorie définie. Ceci va impacter le code du modèle FieldList dans admin/models/fields/helloworld.php.
  4. Nous allons autoriser l'administrateur à définir les catégories associées à notre composant Helloworld. Cette fonctionnalité nous est fournie directement par le composant Joomla com_categories. Il génère le formulaire qui permet aux administrateurs de faire cela – nous devons juste fournir les liens de menus pointant vers ce formulaire au travers du sous-menu décrit plus bas et à travers le menu Composants, et de plus, nous devons définir les droits d'accès à cette fonctionnalité.
  5. Nous allons modifier le formulaire Helloworlds qui montre tous les messages Helloworld afin que la catégorie associée à un message soit affichée. Ceci impactera notre modèle (car nous devrons faire une jointure SQL avec la table des catégories afin d'obtenir le titre de la catégorie) et notre fichier de rendu (où nous allons afficher les catégories associées).
  6. Nous créerons un sous-menu qui peut se fermer vers la gauche comme une barre latérale, et qui montrera deux éléments :
    • Messages – qui permettra l'accès à tous nos messages Helloworld
    • Catégories – qui permettra l’accès au formulaire permettant à l'administrateur de définir la liste de catégories du composant Helloworld.
    Ceci va affecter notre vue Helloworlds (où nous mettrons notre barre latérale) et le rendu (où nous afficherons notre barre latérale), ainsi que définir le sous-menu de la barre latérale lui-même dans un nouveau fichier Helper. Nous allons aussi ajouter une ligne dans notre fichier principal d'administration helloworld.php pour dire à Joomla de charger et exécuter ce fichier helper.

Remarque importante (avec le recul). Cette étape permet de créer des catégories dans le composant helloworld en leur permettant d'être facultatives. Cela diffère des composants Joomla principaux dans lesquels une catégorie doit toujours être définie et où, par défaut, les éléments ont une catégorie "non catégorisée". Si vous autorisez les catégories à être facultatives, vous rencontrerez probablement des difficultés ultérieurement, car certaines fonctions essentielles de la bibliothèque Joomla supposent qu'elles sont obligatoires. Si cette série de didacticiels est mise à jour pour les versions ultérieures de Joomla, il serait préférable d’inclure des catégories d’une manière qui suive le modèle standard Joomla.

Modification du SQL

Afin de gérer les catégories, nous devons changer les tables SQL. A l'aide de votre éditeur de texte préféré, modifiez le fichier admin/sql/installer.mysql.utf8.sql pour y ajouter ces lignes :

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',
	PRIMARY KEY (`id`)
)
	ENGINE =MyISAM
	AUTO_INCREMENT =0
	DEFAULT CHARSET =utf8;

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

admin/sql/updates/mysql/0.0.12.sql

ALTER TABLE `#__helloworld` ADD `catid` int(11) NOT NULL DEFAULT '0';

Modification du formulaire

Un message HelloWorld peut désormais appartenir à une catégorie. Il nous faut maintenant modifier le formulaire d'édition. Dans le fichier admin/models/forms/helloworld.xml, ajoutez ces lignes :

admin/models/forms/helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<form
				addrulepath="/administrator/components/com_helloworld/models/rules"
>
	<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 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>
</form>

Notez que la catégorie peut être 0 (c'est à dire aucune catégorie).

Modification du type de menu

Le type de menu HelloWorld affiche une liste déroulante de tous les messages. Si le message est catégorisé, il nous faut ajouter la catégorie à cet affichage.

Dans le fichier admin/models/fields/helloworld.php, ajoutez ces lignes :

admin/models/fields/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');

JFormHelper::loadFieldClass('list');

/**
 * HelloWorld Form Field class for the HelloWorld component
 *
 * @since  0.0.1
 */
class JFormFieldHelloWorld extends JFormFieldList
{
	/**
	 * The field type.
	 *
	 * @var         string
	 */
	protected $type = 'HelloWorld';

	/**
	 * Method to get a list of options for a list input.
	 *
	 * @return  array  An array of JHtml options.
	 */
	protected function getOptions()
	{
		$db    = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('#__helloworld.id as id,greeting,#__categories.title as category,catid');
		$query->from('#__helloworld');
		$query->leftJoin('#__categories on catid=#__categories.id');
		// Retrieve only published items
		$query->where('#__helloworld.published = 1');
		$db->setQuery((string) $query);
		$messages = $db->loadObjectList();
		$options  = array();

		if ($messages)
		{
			foreach ($messages as $message)
			{
				$options[] = JHtml::_('select.option', $message->id, $message->greeting .
				                      ($message->catid ? ' (' . $message->category . ')' : ''));
			}
		}

		$options = array_merge(parent::getOptions(), $options);

		return $options;
	}
}

Cela permet désormais d'afficher la catégorie entre parenthèses. Remarque : nous avons également ajouté une clause where pour pouvoir filtrer les éléments non publiés.

Gestion du sous-menu

Le composant com_categories permet de définir le sous-menu à l'aide d'un fichier helper. A l'aide de votre gestionnaire de fichiers et éditeur préférés, ajoutez un fichier admin/helpers/helloworld.php contenant ces lignes :

admin/helpers/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 component helper.
 *
 * @param   string  $submenu  The name of the active view.
 *
 * @return  void
 *
 * @since   1.6
 */
abstract class HelloWorldHelper extends JHelperContent
{
	/**
	 * Configure the Linkbar.
	 *
	 * @return Bool
	 */

	public static function addSubmenu($submenu) 
	{
		JHtmlSidebar::addEntry(
			JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
			'index.php?option=com_helloworld',
			$submenu == 'helloworlds'
		);

		JHtmlSidebar::addEntry(
			JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
			'index.php?option=com_categories&view=categories&extension=com_helloworld',
			$submenu == 'categories'
		);

		// Set some global property
		$document = JFactory::getDocument();
		$document->addStyleDeclaration('.icon-48-helloworld ' .
										'{background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
		if ($submenu == 'categories') 
		{
			$document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES'));
		}
	}
}

REMARQUE : vous DEVEZ utiliser le nom de votre composant (sans le com_) pour le nom du fichier helper, sinon vos sous-menus ne s'afficheront pas dans la vue de catégories.

Pour importe la classe helper, ajoutez ces lignes au fichier admin/helloworld.php :

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

// Set some global property
$document = JFactory::getDocument();
$document->addStyleDeclaration('.icon-helloworld {background-image: url(../media/com_helloworld/images/tux-16x16.png);}');

// Require helper file
JLoader::register('HelloWorldHelper', JPATH_COMPONENT . '/helpers/helloworld.php');

// Get an instance of the controller prefixed by HelloWorld
$controller = JControllerLegacy::getInstance('HelloWorld');

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

Cette fonction sera appelée automatiquement par le composant com_categories. A noter que cela va :

  • modifier le sous-menu,
  • modifier certaines propriétés css (pour l'affichage des icônes),
  • changer le titre du navigateur, si le sous-menu est catégories,
  • modifier le titre et ajouter un bouton préférences.

La classe css .icon-48-helloworld est désormais définie dans la fonction addSubmenu. Il nous faut maintenant appeler cette fonction dans la vue helloworlds :

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');
	}
	/**
	 * Method to set up the document properties
	 *
	 * @return void
	 */
	protected function setDocument() 
	{
		$document = JFactory::getDocument();
		$document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION'));
	}
}

Obtenir les titres de catégories associés

Mettez à jour le modèle helloworlds de sorte qu'il fasse une jointure SQL avec la table Catégories de Joomla pour rechercher le titre de la catégorie associée.

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

/**
 * HelloWorldList Model
 *
 * @since  0.0.1
 */
class HelloWorldModelHelloWorlds extends JModelList
{
	/**
	 * Constructor.
	 *
	 * @param   array  $config  An optional associative array of configuration settings.
	 *
	 * @see     JController
	 * @since   1.6
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields']))
		{
			$config['filter_fields'] = array(
				'id',
				'greeting',
				'published'
			);
		}

		parent::__construct($config);
	}

	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return      string  An SQL query
	 */
	protected function getListQuery()
	{
		// Initialize variables.
		$db    = JFactory::getDbo();
		$query = $db->getQuery(true);

		// Create the base select statement.
		$query->select('a.id as id, a.greeting as greeting, a.published as published')
			  ->from($db->quoteName('#__helloworld', 'a'));

		// Join over the categories.
		$query->select($db->quoteName('c.title', 'category_title'))
			->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON c.id = a.catid');

		// Filter: like / search
		$search = $this->getState('filter.search');

		if (!empty($search))
		{
			$like = $db->quote('%' . $search . '%');
			$query->where('greeting LIKE ' . $like);
		}

		// Filter by published state
		$published = $this->getState('filter.published');

		if (is_numeric($published))
		{
			$query->where('a.published = ' . (int) $published);
		}
		elseif ($published === '')
		{
			$query->where('(a.published IN (0, 1))');
		}

		// Add the list ordering clause.
		$orderCol	= $this->state->get('list.ordering', 'greeting');
		$orderDirn 	= $this->state->get('list.direction', 'asc');

		$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));

		return $query;
	}
}

Mise à jour de la mise en page

Mettez à jour le fichier de mise en page helloworlds pour afficher la barre latérale et la catégorie associée au message d'accueil.

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

JHtml::_('formbehavior.chosen', 'select');

$listOrder     = $this->escape($this->filter_order);
$listDirn      = $this->escape($this->filter_order_Dir);
?>
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
	<div id="j-sidebar-container" class="span2">
		<?php echo JHtmlSidebar::render(); ?>
	</div>
	<div id="j-main-container" class="span10">
		<div class="row-fluid">
			<div class="span6">
				<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_FILTER'); ?>
				<?php
					echo JLayoutHelper::render(
						'joomla.searchtools.default',
						array('view' => $this)
					);
				?>
			</div>
		</div>
		<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 JHtml::_('grid.sort', 'COM_HELLOWORLD_HELLOWORLDS_NAME', 'greeting', $listDirn, $listOrder); ?>
				</th>
				<th width="5%">
					<?php echo JHtml::_('grid.sort', 'COM_HELLOWORLD_PUBLISHED', 'published', $listDirn, $listOrder); ?>
				</th>
				<th width="2%">
					<?php echo JHtml::_('grid.sort', 'COM_HELLOWORLD_ID', 'id', $listDirn, $listOrder); ?>
				</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>
								<div class="small">
									<?php echo JText::_('JCATEGORY') . ': ' . $this->escape($row->category_title); ?>
								</div>
							</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"/>
		<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>"/>
		<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>"/>
		<?php echo JHtml::_('form.token'); ?>
	</div>
</form>

Ajout d'ACL

admin/access.xml

<?xml version="1.0" encoding="utf-8" ?>
<access component="com_helloworld">
	<section name="component">
		<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" />
		<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" />
		<action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" />
		<action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" />
		<action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" />
	</section>
	<section name="message">
		<action name="core.delete" title="JACTION_DELETE" description="COM_HELLOWORLD_ACCESS_DELETE_DESC" />
		<action name="core.edit" title="JACTION_EDIT" description="COM_HELLOWORLD_ACCESS_EDIT_DESC" />
	</section>
</access>

REMARQUE : Si vous n'ajoutez pas ce fichier, les boutons Nouveau, Modifier et ... ne s'afficheront pas dans la vue de catégorie. Pour plus d'informations, veuillez consulter la section Ajout d'ACL, accessible depuis le haut de cette page.

Ajout de chaînes de traduction

Certains chaînes sont à traduire. Dans le fichier admin/language/en-GB/en-GB.com_helloworld.ini, ajoutez ces lignes :

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_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"

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.12</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>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