function FormHelper::processStatesArray

Same name in other branches
  1. 9 core/lib/Drupal/Core/Form/FormHelper.php \Drupal\Core\Form\FormHelper::processStatesArray()
  2. 8.9.x core/lib/Drupal/Core/Form/FormHelper.php \Drupal\Core\Form\FormHelper::processStatesArray()
  3. 11.x core/lib/Drupal/Core/Form/FormHelper.php \Drupal\Core\Form\FormHelper::processStatesArray()

Helps recursively rewrite #states selectors.

Not to be confused with self::processStates(), which just prepares states for rendering.

Parameters

array $conditions: States conditions array.

string $search: A partial or entire jQuery selector string to replace in #states.

string $replace: The string to replace all instances of $search with.

See also

self::rewriteStatesSelector()

1 call to FormHelper::processStatesArray()
FormHelper::rewriteStatesSelector in core/lib/Drupal/Core/Form/FormHelper.php
Rewrites #states selectors in a render element.

File

core/lib/Drupal/Core/Form/FormHelper.php, line 57

Class

FormHelper
Provides helpers to operate on forms.

Namespace

Drupal\Core\Form

Code

protected static function processStatesArray(array &$conditions, $search, $replace) {
    // Retrieve the keys to make it easy to rename a key without changing the
    // order of an array.
    $keys = array_keys($conditions);
    $update_keys = FALSE;
    foreach ($conditions as $id => $values) {
        if (str_contains($id, $search)) {
            $update_keys = TRUE;
            $new_id = str_replace($search, $replace, $id);
            // Replace the key and keep the array in the same order.
            $index = array_search($id, $keys, TRUE);
            $keys[$index] = $new_id;
        }
        elseif (is_array($values)) {
            static::processStatesArray($conditions[$id], $search, $replace);
        }
    }
    // Updates the states conditions keys if necessary.
    if ($update_keys) {
        $conditions = array_combine($keys, array_values($conditions));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.