You are here

public static function BackgroundImageFormTrait::addState in Background Image 2.0.x

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

Provides a way to add #states to an element, but in a deferred way.

This ensures that if the form is altered somewhere in the #process or #after_build phase, it generates a selector for the States API using the proper #parents array values.

Parameters

array $element: The render array element in which to attach #states, passed by reference.

string|string[] $state: The state(s) to set for passed $element.

array $input: The render array input element that is used when determining the conditions.

array $conditions: A standard States API conditions array. In place of a condition selector, you can use "%selector" anywhere and it will be replaced with the real selector value. An example of what this real selector value would be is if the $element #parents value contained an array with the values ['parent', 'child', 'input_element'], then the corresponding selector would be "edit-parent-child-input-selector". In most cases, you can pass "*" as the whole selector which will turn into the following selector: "[data-drupal-selector="%selector"]".

See also

\Drupal\background_image\BackgroundImageFormTrait::mapStatesConditions

\Drupal\background_image\BackgroundImageFormTrait::preRenderStates

5 calls to BackgroundImageFormTrait::addState()
BackgroundImageForm::buildImage in src/Form/BackgroundImageForm.php
Builds the "Image" group.
BackgroundImageForm::buildSetting in src/Form/BackgroundImageForm.php
Builds a setting element.
BackgroundImageForm::buildSettings in src/Form/BackgroundImageForm.php
Builds the "Settings" group.
BackgroundImageForm::buildTypeSelection in src/Form/BackgroundImageForm.php
Builds the new type selection step.
BackgroundImageSettingsForm::buildForm in src/Form/BackgroundImageSettingsForm.php
Form constructor.

File

src/BackgroundImageFormTrait.php, line 95

Class

BackgroundImageFormTrait
Trait BackgroundImageFormTrait.

Namespace

Drupal\background_image

Code

public static function addState(array &$element, $state, array &$input, array $conditions = []) {
  $element['#pre_render_states'][] = [
    $state,
    &$input,
    $conditions,
  ];
  self::prependCallback($element, '#pre_render', [
    self::class,
    'preRenderStates',
  ]);
}