You are here

public static function BackgroundImageFormTrait::preRenderStates in Background Image 8

Same name and namespace in other branches
  1. 2.x src/BackgroundImageFormTrait.php \Drupal\background_image\BackgroundImageFormTrait::preRenderStates()
  2. 2.0.x src/BackgroundImageFormTrait.php \Drupal\background_image\BackgroundImageFormTrait::preRenderStates()

The #pre_render callback for ::addState.

Parameters

array $element: The render array element that is being pre-rendered.

Return value

array The modified render array.

See also

\Drupal\background_image\BackgroundImageFormTrait::addState

\Drupal\background_image\BackgroundImageFormTrait::mapStatesConditions

File

src/BackgroundImageFormTrait.php, line 563

Class

BackgroundImageFormTrait
Trait BackgroundImageFormTrait.

Namespace

Drupal\background_image

Code

public static function preRenderStates($element) {
  $pre_render_states = isset($element['#pre_render_states']) ? $element['#pre_render_states'] : [];
  foreach ($pre_render_states as $data) {

    // Retrieve the stored values.
    list($states, $passed_input, $conditions) = $data;

    // Find the real input element from the passed value.
    $input =& self::findFirstInput($passed_input);

    // Generate a proper selector based on the input element's #parents.
    $selector = 'edit-' . Html::cleanCssIdentifier(implode('-', $input['#parents']));

    // Typecast to an array.
    $states = (array) $states;
    foreach ($states as $state) {

      // Ensure the state exists.
      if (!isset($element['#states'][$state])) {
        $element['#states'][$state] = [];
      }

      // Merge in the mapped conditions for the state.
      $element['#states'][$state] = array_merge($element['#states'][$state], self::mapStatesConditions($selector, $conditions));
    }
  }

  // Remove the stored values.
  unset($element['#pre_render_states']);
  return $element;
}