Back to ListField class

Method addOption

public \Joomla\CMS\Form\Field\ListField
addOption
(mixed $text, mixed $attributes = array())
Method to add an option to the list field.
Parameters
  • string $text Text/Language variable of the option.
  • array $attributes Array of attributes ('name' => 'value' format)
Returns
  • \Joomla\CMS\Form\Field\ListField For chaining.
Since
  • 3.7.0
Class: ListField
Project: Joomla

Method addOption - Source code

/**
 * Method to add an option to the list field.
 *
 * @param   string  $text        Text/Language variable of the option.
 * @param   array   $attributes  Array of attributes ('name' => 'value' format)
 *
 * @return  ListField  For chaining.
 *
 * @since   3.7.0
 */
public function addOption($text, $attributes = array())
{
    if ($text && $this->element instanceof \SimpleXMLElement) {
        $child = $this->element->addChild('option', $text);
        foreach ($attributes as $name => $value) {
            $child->addAttribute($name, $value);
        }
    }
    return $this;
}