You are here

protected static function ConfigActionsTransform::tree_delete_data in Config Actions 8

Recursive helper function to walk a tree and prune it.

Parameters

array $tree : A Array passed by reference that we will prune:

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

bool $prune_empty : If true then unset the data key, otherwise just: clear the data based on its existing value.

1 call to ConfigActionsTransform::tree_delete_data()
ConfigActionsTransform::delete in src/ConfigActionsTransform.php
Walk a tree and remove a path

File

src/ConfigActionsTransform.php, line 50

Class

ConfigActionsTransform
Perform transformations on data needed for config_actions plugins

Namespace

Drupal\config_actions

Code

protected static function tree_delete_data(array &$tree, array $path, $prune_empty = FALSE) {
  $key = array_shift($path);

  //If we have farther to walk keep walking
  if (!empty($path)) {
    static::tree_delete_data($tree[$key], $path, $prune_empty);

    //if we have an empty branch we prune
    if ($prune_empty && empty($tree[$key])) {
      unset($tree[$key]);
    }
  }
  elseif (empty($key)) {
    $tree = NULL;
  }
  elseif (isset($tree[$key])) {
    $old = $tree[$key];
    if ($prune_empty) {
      unset($tree[$key]);
    }
    elseif (is_string($old)) {
      $tree[$key] = '';
    }
    elseif (is_array($old)) {
      $tree[$key] = [];
    }
    elseif (is_object($old)) {
      $tree[$key] = NULL;
    }
    elseif (is_numeric($old)) {
      $tree[$key] = 0;
    }
    elseif (is_bool($old)) {
      $tree[$key] = FALSE;
    }
    else {
      $tree[$key] = NULL;
    }
  }
}