FOF30\Form\Field\GenericList::getOptionName PHP Method

getOptionName() public static method

Gets the active option's label given an array of JHtml options
public static getOptionName ( array $data, mixed $selected = null, string $optKey = 'value', string $optText = 'text', boolean $selectFirst = true ) : mixed
$data array The JHtml options to parse
$selected mixed The currently selected value
$optKey string Key name
$optText string Value name
$selectFirst boolean Should I automatically select the first option?
return mixed The label of the currently selected option
    public static function getOptionName($data, $selected = null, $optKey = 'value', $optText = 'text', $selectFirst = true)
    {
        $ret = null;
        foreach ($data as $elementKey => &$element) {
            if (is_array($element)) {
                $key = $optKey === null ? $elementKey : $element[$optKey];
                $text = $element[$optText];
            } elseif (is_object($element)) {
                $key = $optKey === null ? $elementKey : $element->{$optKey};
                $text = $element->{$optText};
            } else {
                // This is a simple associative array
                $key = $elementKey;
                $text = $element;
            }
            if (is_null($ret) && $selectFirst) {
                $ret = $text;
            } elseif ($selected == $key) {
                $ret = $text;
            }
        }
        return $ret;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Gets the active option's label given an array of JHtml options
  *
  * @param   array   $data      The JHtml options to parse
  * @param   mixed   $selected  The currently selected value
  * @param   string  $groupKey  Group name
  * @param   string  $optKey    Key name
  * @param   string  $optText   Value name
  *
  * @return  mixed   The label of the currently selected option
  */
 public static function getOptionName($data, $selected = null, $groupKey = 'items', $optKey = 'value', $optText = 'text')
 {
     if ($groupKey) {
     }
     // Keeps phpStorm from freaking out
     $ret = null;
     foreach ($data as $dataKey => $group) {
         if (is_array($group)) {
             $label = $group[$optText];
             $noGroup = false;
         } elseif (is_object($group)) {
             // Sub-list is in a property of an object
             $label = $group->{$optText};
             $noGroup = false;
         } else {
             throw new InvalidGroupContents(get_called_class());
         }
         if ($noGroup) {
             $label = '';
         }
         $match = GenericList::getOptionName($data, $selected, $optKey, $optText);
         if (!is_null($match)) {
             $ret = array('group' => $label, 'item' => $match);
             break;
         }
     }
     return $ret;
 }
All Usage Examples Of FOF30\Form\Field\GenericList::getOptionName