rmattr('type'); $this->optGroups[''] = new \dokuwiki\plugin\data\Form\OptGroup(null, $options); $this->val(''); } /** * Adds multivalue capabilities * * @param array $value * @return DropdownElement|string|array */ public function val($value = null) { // getter if ($value === null) { if (isset($this->attributes['multiple'])) { return $this->values; } else { return $this->values[0]; } } // setter $this->values = $this->setValuesInOptGroups((array)$value); if (!$this->values) { // unknown value set, select first option instead $this->values = $this->setValuesInOptGroups((array)$this->getFirstOptionKey()); } return $this; } /** * Skips over parent's \InvalidArgumentException thrown for 'multiple' * * @param $name * @param $value * @return DropdownElement|string */ public function attr($name, $value = null) { return InputElement::attr($name, $value); } /** * Returns the first option's key * * @return string */ protected function getFirstOptionKey() { $options = $this->options(); if (!empty($options)) { $keys = array_keys($options); return (string)array_shift($keys); } foreach ($this->optGroups as $optGroup) { $options = $optGroup->options(); if (!empty($options)) { $keys = array_keys($options); return (string)array_shift($keys); } } return ''; // should not happen } /** * Set the value in the OptGroups, including the optgroup for the options without optgroup. * * @param string[] $values The values to be set * @return string[] The values actually set */ protected function setValuesInOptGroups($values) { $valueset = []; /** @var \dokuwiki\plugin\data\Form\OptGroup $optGroup */ foreach ($this->optGroups as $optGroup) { $found = $optGroup->storeValues($values); $values = array_diff($values, $found); $valueset = array_merge($valueset, $found); } return $valueset; } /** * @inheritDoc */ protected function mainElementHTML() { $attr = $this->attrs(); if (isset($attr['multiple'])) { // use array notation when multiple values are allowed $attr['name'] .= '[]'; } elseif ($this->useInput) { // prefilling is only supported for non-multi fields $this->prefillInput(); } unset($attr['selected']); $html = ''; return $html; } }