You are here

public function ConfigPagesLoaderService::getValue in Config Pages 8.2

Same name and namespace in other branches
  1. 8.3 src/ConfigPagesLoaderService.php \Drupal\config_pages\ConfigPagesLoaderService::getValue()

Get value from CP.

Parameters

string|ConfigPages $type: Config page object or type name.

string $field_name: Field name.

array|int $deltas: Field value deltas that you like to get.

string $key: Field "value" key.

Return value

array|mixed|null Value (or array of values) from specified field in CP.

Overrides ConfigPagesLoaderServiceInterface::getValue

File

src/ConfigPagesLoaderService.php, line 51

Class

ConfigPagesLoaderService
Class used as loader for ConfigPages.

Namespace

Drupal\config_pages

Code

public function getValue($type, $field_name, $deltas = [], $key = NULL) {
  $default = !empty($key) ? NULL : [];
  if (!is_array($deltas)) {
    $return_delta = $deltas;
    $deltas = [
      $deltas,
    ];
  }
  else {
    $return_delta = NULL;
  }

  // Exit if empty config page.
  $config_page = is_object($type) ? $type : $this
    ->load($type);
  if (empty($config_page)) {
    return $return_delta === NULL ? [] : $default;
  }

  // Load field.
  if ($config_page
    ->hasField($field_name)) {
    $field = $config_page
      ->get($field_name);
  }
  else {
    return $return_delta === NULL ? [] : $default;
  }

  // Trim values by deltas.
  $_values = $field
    ->getValue();
  $values = [];
  if (empty($deltas)) {
    $values = $_values;
  }
  else {
    foreach ($deltas as $delta) {
      $values[$delta] = isset($_values[$delta]) ? $_values[$delta] : [];
    }
  }

  // Extract keys from values.
  if (!empty($key)) {
    foreach ($values as &$value) {
      $value = isset($value[$key]) ? $value[$key] : NULL;
    }
  }
  return $return_delta === NULL ? $values : $values[$return_delta];
}