diff --git a/_test/tests/inc/form/dropdownelement.test.php b/_test/tests/inc/form/dropdownelement.test.php index 989469182c7a52a70f030bf3870d42f966173a2c..78ec55713de88bd0057bb47a60a2ce70e020ccb3 100644 --- a/_test/tests/inc/form/dropdownelement.test.php +++ b/_test/tests/inc/form/dropdownelement.test.php @@ -46,6 +46,41 @@ class form_dropdownelement_test extends DokuWikiTest { $this->assertEquals('label text', $label->find('span')->text()); } + function test_extended_options() { + $form = new Form\Form(); + + $options = array( + 'first' => array ( + 'label' => 'the label', + 'attrs' => array( + 'id' => 'theID', + 'class' => 'two classes', + 'data-foo' => 'bar' + ) + ), + 'second' + ); + + $form->addDropdown('foo', $options, 'label text'); + // HTML + $html = $form->toHTML(); + $pq = phpQuery::newDocumentXHTML($html); + + $select = $pq->find('select[name=foo]'); + $this->assertTrue($select->length == 1); + + $options = $pq->find('option'); + $this->assertTrue($options->length == 2); + + $option = $pq->find('option#theID'); + $this->assertEquals(1, $option->length); + $this->assertEquals('first', $option->val()); + $this->assertEquals('the label', $option->text()); + $this->assertEquals('bar', $option->attr('data-foo')); + $this->assertTrue($option->hasClass('two')); + $this->assertTrue($option->hasClass('classes')); + } + /** * check that posted values overwrite preset default */ diff --git a/inc/Form/DropdownElement.php b/inc/Form/DropdownElement.php index fc9ce54ec9e767a80bbbd004d1b8f10e92a35051..6a2147d0aaea9bcf25b1dee3a9e3e5db04877a61 100644 --- a/inc/Form/DropdownElement.php +++ b/inc/Form/DropdownElement.php @@ -28,7 +28,14 @@ class DropdownElement extends InputElement { * Get or set the options of the Dropdown * * Options can be given as associative array (value => label) or as an - * indexd array (label = value). + * indexd array (label = value) or as an array of arrays. In the latter + * case an element has to look as follows: + * option-value => array ( + * 'label' => option-label, + * 'attrs' => array ( + * attr-key => attr-value, ... + * ) + * ) * * @param null|array $options * @return $this|array @@ -40,9 +47,12 @@ class DropdownElement extends InputElement { foreach($options as $key => $val) { if(is_int($key)) { - $this->options[$val] = (string) $val; + $this->options[$val] = array('label' => (string) $val); + } elseif (!is_array($val)) { + $this->options[$key] = array('label' => (string) $val); } else { - $this->options[$key] = (string) $val; + if (!key_exists('label', $val)) throw new \InvalidArgumentException('If option is given as array, it has to have a "label"-key!'); + $this->options[$key] = $val; } } $this->val(''); // set default value (empty or first) @@ -103,7 +113,12 @@ class DropdownElement extends InputElement { $html = '<select ' . buildAttributes($this->attrs()) . '>'; foreach($this->options as $key => $val) { $selected = ($key == $this->value) ? ' selected="selected"' : ''; - $html .= '<option' . $selected . ' value="' . hsc($key) . '">' . hsc($val) . '</option>'; + $attrs = ''; + if (is_array($val['attrs'])) { + array_walk($val['attrs'],function (&$aval, $akey){$aval = hsc($akey).'="'.hsc($aval).'"';}); + $attrs = join(' ', $val['attrs']); + } + $html .= '<option' . $selected . ' value="' . hsc($key) . '" '.$attrs.'>' . hsc($val['label']) . '</option>'; } $html .= '</select>';