Vom: 20.08.2012

Pimcore: Dropdown-Länderliste leicht befüllt

Wieder mal ein Griff in die Trickkiste, aus der Kategorie "nützliche Dreizeiler". Problemstellung war ein Kontaktformular mit einem Dropdown als Liste aller Länder. Man kann diese Liste nun natürlich "zu Fuss" definieren, oder sich gleich komfortabel von Pimcore bzw dem Zend Framework befüllen lassen - dort liegen die Daten ohnehin schon vor:

    $countries = new Object_Class_Data_Country();
    $options = array();
    foreach ($countries->getOptions() as $country) $options[$country["value"]] = $country["key"];

Im Array $options liegen die Länder dann in der Form vor, wie man sie direkt in ein Zend Form Element vom Typ Select stecken kann:

    $land = $this->createElement("select", "land");
    $land->setLabel("Land")->setMultioptions($options);

Wer ein spezielleres Ergebnis braucht, kann die Erstellung auch selbst vornehmen und das abändern bzw kopieren, was in Object_Class_Data_Country gemacht wird:

        $countries = Zend_Locale::getTranslationList('territory');
        asort($countries);
        $options = array();

        foreach ($countries as $short => $translation) {
            if (strlen($short) == 2) {
                $options[] = array(
                    "key" => $translation,
                    "value" => $short
                );
            }
        }

        return $options;