Back to FormHelper class

Method parseShowOnConditions

public static array
parseShowOnConditions
(mixed $showOn, mixed $formControl = null, mixed $group = null)
Parse the show on conditions
Parameters
  • string $showOn Show on conditions.
  • string $formControl Form name.
  • string $group The dot-separated form group path.
Returns
  • array Array with show on conditions.
Since
  • 3.7.0
Class: FormHelper
Project: Joomla

Method parseShowOnConditions - Source code

/**
 * Parse the show on conditions
 *
 * @param   string  $showOn       Show on conditions.
 * @param   string  $formControl  Form name.
 * @param   string  $group        The dot-separated form group path.
 *
 * @return  array   Array with show on conditions.
 *
 * @since   3.7.0
 */
public static function parseShowOnConditions($showOn, $formControl = null, $group = null)
{
    // Process the showon data.
    if (!$showOn) {
        return array();
    }
    $formPath = $formControl ?: '';
    if ($group) {
        $groups = explode('.', $group);
        // An empty formControl leads to invalid shown property
        // Use the 1st part of the group instead to avoid.
        if (empty($formPath) && isset($groups[0])) {
            $formPath = $groups[0];
            array_shift($groups);
        }
        foreach ($groups as $group) {
            $formPath .= '[' . $group . ']';
        }
    }
    $showOnData = array();
    $showOnParts = preg_split('#(\\[AND\\]|\\[OR\\])#', $showOn, -1, PREG_SPLIT_DELIM_CAPTURE);
    $op = '';
    foreach ($showOnParts as $showOnPart) {
        if ($showOnPart === '[AND]' || $showOnPart === '[OR]') {
            $op = trim($showOnPart, '[]');
            continue;
        }
        $compareEqual = strpos($showOnPart, '!:') === false;
        $showOnPartBlocks = explode($compareEqual ? ':' : '!:', $showOnPart, 2);
        $dotPos = strpos($showOnPartBlocks[0], '.');
        if ($dotPos === false) {
            $field = $formPath ? $formPath . '[' . $showOnPartBlocks[0] . ']' : $showOnPartBlocks[0];
        } else {
            if ($dotPos === 0) {
                $fieldName = substr($showOnPartBlocks[0], 1);
                $field = $formControl ? $formControl . '[' . $fieldName . ']' : $fieldName;
            } else {
                if ($formControl) {
                    $field = $formControl . ('[' . str_replace('.', '][', $showOnPartBlocks[0]) . ']');
                } else {
                    $groupParts = explode('.', $showOnPartBlocks[0]);
                    $field = array_shift($groupParts) . '[' . join('][', $groupParts) . ']';
                }
            }
        }
        $showOnData[] = array('field' => $field, 'values' => explode(',', $showOnPartBlocks[1]), 'sign' => $compareEqual === true ? '=' : '!=', 'op' => $op);
        if ($op !== '') {
            $op = '';
        }
    }
    return $showOnData;
}