e@sy Solutions IT

Aktionen mit verschiedenen Icons pro Datensatz

Wie man in die Listen von Contao eigene Aktionen einträgt, habe ich ja bereits im Artikel "Eine eigene Aktion in eine Contao-Übersicht einfügen" beschrieben. Bei diesem Vorgehen wird für alle Einträge der Liste das gleiche Icon angezeigt. Es handelt sich also um ein globales Icon. Es ist nicht möglich verschiedene Icons pro Listeneintrag anzuzeigen (um z.B. einen Status abzubilden). Wie man individuelle Icons für eine Aktion anzeigt, ist Thema dieses Artikels.

Schritt 1: Aktion mit Standard-Icon

Zu nächst legen wir eine Aktion mit Standard-Icon an. Dies geschieht wie in dem Beitrag „Eine eigene Aktion in eine Contao-Übersicht einfügen“.

Schritt 2: Den Button-Callback eintragen

Nun geben wir den button_callback bekannt. Dieser erstellt den Link für den Aufruf unserer Aktion mit dem individuellen Icon. Wir tragen dazu in die Datei mit dem Namen unserer Tabelle im dca-Verzeichnis unserer Erweiterung eine Klasse und einen Methodennamen ein (z.B. demoErweiterung/dca/tl_beispiel.php).

$GLOBALS['TL_DCA']['tl_beispiel']['list']['operations']['testaktion'] = array(
        'label'     => &$GLOBALS['TL_LANG']['tl_beispiel']['testaktion'],
        'href'      => 'key=reset',
        'icon'      => 'system/modules/demoErweiterung/html/standardIcon.png',
        'button_callback'   => array('demoErweiterungsKlasse', 'makeIconMethode')
);

Wichtig ist hier die Zeile 'button_callback' => array('demoErweiterungsKlasse', 'makeIconMethode'). Durch die Methode makeIconMethode der Klasse demoErweiterungsKlasse wird also das Icon erstellt.

$arrRowthe current row
$hrefthe url of the embedded link of the button
$labellabel text for the button
$titletitle value for the button
$iconurl of the image for the button
$attributesadditional attributes for the button (fetched from the array key "attributes" in the DCA)
$strTablethe name of the current table
$arrRootIdsarray of the ids of the selected "page mounts" (only in tree view)
$arrChildRecordIdsids of the childs of the current record (only in tree view)
$blnCircularReferencedetermines if this record has a circular reference (used to prevent pasting of an cutted item from an tree into any of it's childs).
$strPreviousid of the previous entry on the same parent/child level. Used for move up/down buttons. Not for root entries in tree view.
$strNextid of the next entry on the same parent/child level. Used for move up/down buttons. Not for root entries in tree view.

(Danke an Xtra aus dem Contao-Forum, der diese Liste hier gepostet hat.)

Contao erwartet von der Methode die Rückgabe eines Links, der wie folgt aufgebaut werden kann.

return '<a href="'.$this->addToUrl($href).'" title="'.specialchars($title).'"'.$attributes.'>'.$this->generateImage($icon, $label).'</a> ';

Vorher legt man für $icon den Pfad zum gewünschten Icon fest.

Beispiel:

Möchte man für jede gerade Id ein anderes Icon darstellen schreibt man in die Callback-Methode vor dem return:

if($arrRow %2 == 0){
	$icon = 'system/modules/demoErweiterung/html/anderesIcon.png';
}
return '<a href="'.$this->addToUrl($href).'" title="'.specialchars($title).'"'.$attributes.'>'.$this->generateImage($icon, $label).'</a> ';

Für die ungeraden Id's wird das Standardicon verwendet. Natürlich lassen sich beliebige Eigenschaften der Tabellenzeile analysieren und nicht nur das Icon, sondern auch das Linkziel lässt sich anpassen. Hier sind der Phantasie keine Grenzen gesetzt.
Das war's schon. Wie immer gilt: Es ist ganz einfach, wenn man weiß wie es geht! Wenn Sie Fragen oder Anregungen haben, hinterlassen Sie mir bitte einen Kommentar.

Kommentare