Back to ListField class

Method getOptions

protected array
getOptions
()
Method to get the field options.
Returns
  • array The field option objects.
Since
  • 3.7.0
Class: ListField
Project: Joomla

Method getOptions - Source code

/**
 * Method to get the field options.
 *
 * @return  array  The field option objects.
 *
 * @since   3.7.0
 */
protected function getOptions()
{
    $fieldname = preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname);
    $options = array();
    foreach ($this->element->xpath('option') as $option) {
        // Filter requirements
        if ($requires = explode(',', (string) $option['requires'])) {
            // Requires multilanguage
            if (\in_array('multilanguage', $requires) && !Multilanguage::isEnabled()) {
                continue;
            }
            // Requires associations
            if (\in_array('associations', $requires) && !Associations::isEnabled()) {
                continue;
            }
            // Requires adminlanguage
            if (\in_array('adminlanguage', $requires) && !ModuleHelper::isAdminMultilang()) {
                continue;
            }
            // Requires vote plugin
            if (\in_array('vote', $requires) && !PluginHelper::isEnabled('content', 'vote')) {
                continue;
            }
            // Requires record hits
            if (\in_array('hits', $requires) && !ComponentHelper::getParams('com_content')->get('record_hits', 1)) {
                continue;
            }
        }
        $value = (string) $option['value'];
        $text = trim((string) $option) != '' ? trim((string) $option) : $value;
        $disabled = (string) $option['disabled'];
        $disabled = $disabled === 'true' || $disabled === 'disabled' || $disabled === '1';
        $disabled = $disabled || $this->readonly && $value != $this->value;
        $checked = (string) $option['checked'];
        $checked = $checked === 'true' || $checked === 'checked' || $checked === '1';
        $selected = (string) $option['selected'];
        $selected = $selected === 'true' || $selected === 'selected' || $selected === '1';
        $tmp = array('value' => $value, 'text' => Text::alt($text, $fieldname), 'disable' => $disabled, 'class' => (string) $option['class'], 'selected' => $checked || $selected, 'checked' => $checked || $selected);
        // Set some event handler attributes. But really, should be using unobtrusive js.
        $tmp['onclick'] = (string) $option['onclick'];
        $tmp['onchange'] = (string) $option['onchange'];
        if ((string) $option['showon']) {
            $encodedConditions = json_encode(FormHelper::parseShowOnConditions((string) $option['showon'], $this->formControl, $this->group));
            $tmp['optionattr'] = " data-showon='" . $encodedConditions . "'";
        }
        // Add the option object to the result set.
        $options[] = (object) $tmp;
    }
    if ($this->element['useglobal']) {
        $tmp = new \stdClass();
        $tmp->value = '';
        $tmp->text = Text::_('JGLOBAL_USE_GLOBAL');
        $component = Factory::getApplication()->input->getCmd('option');
        // Get correct component for menu items
        if ($component === 'com_menus') {
            $link = $this->form->getData()->get('link');
            $uri = new Uri($link);
            $component = $uri->getVar('option', 'com_menus');
        }
        $params = ComponentHelper::getParams($component);
        $value = $params->get($this->fieldname);
        // Try with global configuration
        if (\is_null($value)) {
            $value = Factory::getApplication()->get($this->fieldname);
        }
        // Try with menu configuration
        if (\is_null($value) && Factory::getApplication()->input->getCmd('option') === 'com_menus') {
            $value = ComponentHelper::getParams('com_menus')->get($this->fieldname);
        }
        if (!\is_null($value)) {
            $value = (string) $value;
            foreach ($options as $option) {
                if ($option->value === $value) {
                    $value = $option->text;
                    break;
                }
            }
            $tmp->text = Text::sprintf('JGLOBAL_USE_GLOBAL_VALUE', $value);
        }
        array_unshift($options, $tmp);
    }
    reset($options);
    return $options;
}