Date de publication: le Dimanche 4 Octobre 2009 à 00h30
Dernière modification: par Pascal BOYER le Vendredi 19 Février 2010 à 16h37
« Article précédent: eZ Publish : récupérer X objects aléatoirement
» Article suivant: eZ Publish: les liens d'objets
Le datatype Selection
Le datatype Selection permet un choix unique ou multiple.
Pour afficher la ou les options sélectionnées il existe au moins deux solutions.
Première méthode
{attribute_view_gui attribute=$node.object.data_map.identifiant_du_datatype_selection}
Auquel cas, le template design/standard/templates/content/datatype/view/ezselection.tpl est utilisé pour afficher le résultat.
L'inconvénient de cette méthode, est que dans le cas d'une sélection multiple, vous ne gérez pas l'affichage produit. Toutes les options sélectionnées sont les une sous les autres. Pour contourner ce problème, on préfèrera la deuxième méthode.
Deuxième méthode
Le code est inspiré du template ezselection.tpl:
{* ON DÉFINIT UN TABLEAU selected_id_array CONTENANT LE/LES id DE/DES OPTION/S SÉLECTIONNÉE/S *}
{let selected_id_array=$node.data_map.identifiant_du_datatype_selection.content}
{* BOUCLE: POUR CHACUNE DES OPTIONS DU DATATYPE Sélection *}
{section var=Options loop=$node.data_map.identifiant_du_datatype_selection.class_content.options}
{* ON EXCLU TOUTES LES OPTIONS DONT LE id N'EST PAS EGAL À L'UN DES id CONTENUS PAR LE TABLEAU selected_id_array *}
{section-exclude match=$selected_id_array|contains( $Options.item.id )|not}
{* ON ÉCRIT LE NOM DE L'OPTION SÉLECTIONNÉE
{$Options.item.name|wash( xhtml )}
{* ON DÉFINIT UNE CHAÎNE DE CARACTÈRES QUI S'AFFICHERA APRÈS LE NOM DE CHAQUE OPTION SÉLECTIONNÉE (SAUF APRÈS LA DERNIÈRE) *}
{delimiter} -- {/delimiter}
{* ON FERME LA BOUCLE *}
{/section}
{/let}Exemple
Soit une classe d'objet contenant un attribut (identifiant mes_selections) basé sur le datatype Sélection. Peu importe que l'on décide d'autoriser ou non les choix multiples. Le cas échéant, le tableau $liste_des_id_selectionnes (voir code ci-dessous) contiendra alors simplement plusieurs id.
Le code présenté ci-après est une réécriture de celui proposé par défaut par le template où les boucles basées sur section (structure de contrôle obsolète) ont été remplacées par foreach:
{def $list_enfants=fetch( content, list, hash( parent_node_id, $node.node_id))}
{foreach $list_enfants as $enfant}
{* ON DÉFINIT UN TABLEAU $liste_des_id_selectionnes CONTENANT LE/LES id DE/DES OPTION/S SÉLECTIONNÉE/S *}
{def $liste_des_id_selectionnes=$enfant.data_map.mes_selections.content}
{* POUR CHACUNE DES OPTIONS DU DATATYPE mes_selections *}
{foreach $enfant.data_map.mes_selections.class_content.options as $option}
{* SI LE TABLEAU liste_des_id_selectionnes NE CONTIENT PAS D'OPTION DONT LE id EST ÉGAL AU id DE L'OPTION TRAITÉE *}
{if $liste_des_id_selectionnes|contains( $option.id )|not}
{* ALORS ON NE FAIT RIEN *}
{skip}
{* SINON *}
{elseif <mettre ici une condition quelconque>}
<span style="color: red;">{option.name|wash( xhtml )}</span>
{else}
{$option.name|wash( xhtml )}
{/if}
{delimiter} - {/delimiter}
{/foreach}
{/foreach}
{/def}
:
Le paramètre delimiter ne fonctionne pas aussi bien dans une boucle foreach que dans une boucle section en ce sens que la chaîne de caractères définie entre {delimiter} et {/delimiter} sera également présente après la dernière option écrite.
Exemple
Rechercher le nombre d'enfants en se basant sur la valeur sélectionnée dans un datatype Sélection:
{def $listes_enfants=fetch( content, list_count, hash( 'parent_node_id', $node.node_id,
'attribute_filter', array( array( 'ma_classe/mon_attribut_selection', 'in', array( 22) ))))}
Renvoie le nombre d'enfants dont l'option sélectionnée vaut 22
Exemple avec choix unique
Soit un datatype Sélection dont on a définit qu'il est à choix unique:
{* POUR AFFICHER LE TABLEAU DES INFOS RELATIVES AU DATATYPE selection *}
{$node.data_map.identifiant_datatype_selection.class_content|attribute(show,3)}
{* POUR AFFICHER TOUTES LES VALEURS DES OPTIONS DU DATATYPE selection *}
{def $touteslesoptions=$node.data_map.identifiant_datatype_selection.class_content.options}
{foreach $touteslesoptions as $option}
option={$option.name|wash( xhtml )}<br />
{/foreach}
<br /><br />
{* POUR AFFICHER UNIQUEMENT LE «id» DE L'OPTION SELECTIONNEE - FORCEMENT «...content.0» CAR CHOIX UNIQUE *}
{def $idOptionSelectionnee=$node.data_map.identifiant_datatype_selection.content.0}
idOptionSelectionnee={$idOptionSelectionnee}
<br /><br />
{* AFFICHER LA VALEUR DE L'OPTION SELECTIONNEE *}
valeur sélectionnée={$node.data_map.identifiant_datatype_selection.class_content.options.$idOptionSelectionnee.name}
Commentaires













