You are here

protected static function ConfigActionsTransform::tree_change_data in Config Actions 8

Recursive helper function to walk a tree and add/change a value

Parameters

array $tree : An array passed by reference to which we will add:

array $path : An array of keys to walk to find the point to insert:

$value : the value to insert:

1 call to ConfigActionsTransform::tree_change_data()
ConfigActionsTransform::add in src/ConfigActionsTransform.php
Walk a tree and add a value at the end of the path

File

src/ConfigActionsTransform.php, line 20

Class

ConfigActionsTransform
Perform transformations on data needed for config_actions plugins

Namespace

Drupal\config_actions

Code

protected static function tree_change_data(array &$tree, array $path, $value) {

  // If no path is given, loop through the value array and set top-level items
  if (empty($path) && is_array($value)) {
    foreach ($value as $key => $item) {
      static::tree_change_data($tree, [
        $key,
      ], $item);
    }
    return;
  }
  $key = array_shift($path);
  if (!empty($path)) {

    //if we do not have a value set it
    if (!array_key_exists($key, $tree)) {
      $tree[$key] = [];
    }
    static::tree_change_data($tree[$key], $path, $value);
  }
  else {
    $tree[$key] = $value;
  }
}