You are here

public static function BulkVariationsCreator::afterBuildPreRenderArrayAlter in Commerce Bulk 8

A callback which might be set to #pre_render or #after_build form element.

This helper function alters data on the form element passed along with the element in the following manner:

$i = 0;
$element['alter_data_' . $i] = [
  '#parents' => [
    'form',
    'deep',
    'nested',
    'array_element',
  ],
  '#default_value' => $my_value,
];
$i++;
$element['alter_data_' . $i] = [
  '#parents' => [
    'form',
    'another',
    'nested',
    'array_element',
  ],
  '#disabled' => TRUE,
];

// $element['#after_build'][] = [$creator, 'afterBuildPreRenderArrayAlter'];

It is primarily used for form structures and renderable arrays. Any number of data arrays with different paths (#parents) may be attached to an element. If #parents is omitted the altering will apply on the root of the element. The $creator may be passed to a callbacks array as an object or a fully qualified class name. After the target array elements being altered the 'alter_data_NNN' containers are unset.

Parameters

array $element: The render array element normally passed by the system call.

Return value

array The altered render array element.

Overrides BulkVariationsCreatorInterface::afterBuildPreRenderArrayAlter

See also

commerce_product_field_widget_form_alter()

File

src/BulkVariationsCreator.php, line 78

Class

BulkVariationsCreator
Default implementation of the BulkVariationsCreatorInterface.

Namespace

Drupal\commerce_bulk

Code

public static function afterBuildPreRenderArrayAlter(array $element) {
  $i = 0;
  while (isset($element['alter_data_' . $i]) && ($data = $element['alter_data_' . $i])) {
    $parents = [];
    if (isset($data['#parents'])) {
      $parents = $data['#parents'];
      unset($data['#parents']);
    }
    unset($element['alter_data_' . $i]);
    $key_exists = NULL;
    $old_data = NestedArray::getValue($element, $parents, $key_exists);
    if (is_array($old_data)) {
      if (isset($element['#value'])) {
        $old_data['#value'] = $old_data['#default_value'] = $data['#value'];
        $data = $old_data;
      }
      $data = array_replace($old_data, $data);
    }
    elseif ($key_exists && !in_array($old_data, $data)) {
      $data[] = $old_data;
    }
    NestedArray::setValue($element, $parents, $data);
    $i++;
  }
  return $element;
}