You are here

public function Data::flatten in Translation Management Tool 8

Converts a nested data array into a flattened structure with a combined key.

This function can be used by translators to help with the data conversion.

Nested keys will be joined together using a colon, so for example $data['key1']['key2']['key3'] will be converted into $flattened_data['key1][key2][key3'].

Parameters

array $data: The nested array structure that should be flattened.

string $prefix: Internal use only, indicates the current key prefix when recursing into the data array.

array $label: Label for the data.

Return value

array The flattened data array.

1 call to Data::flatten()
Data::filterTranslatable in src/Data.php
Flattens and filters data for being translatable.

File

src/Data.php, line 56

Class

Data
All data-related functions.

Namespace

Drupal\tmgmt

Code

public function flatten(array $data, $prefix = NULL, $label = array()) {
  $flattened_data = array();
  if (isset($data['#label'])) {
    $label[] = $data['#label'];
  }

  // Each element is either a text (has #text property defined) or has children,
  // not both.
  if (!empty($data['#text'])) {
    $flattened_data[$prefix] = $data;
    $flattened_data[$prefix]['#parent_label'] = $label;
  }
  else {
    $prefix = isset($prefix) ? $prefix . static::TMGMT_ARRAY_DELIMITER : '';
    foreach (Element::children($data) as $key) {
      $flattened_data += $this
        ->flatten($data[$key], $prefix . $key, $label);
    }
  }
  return $flattened_data;
}