You are here

function config_pages_get in Config Pages 7

Fetch config value.

Parameters

$field_name: (str) field name.

$delta: (int/array) specify which deltas to return.

$default: (any) will be returned if config missing

$key: (string) inner field key, like 'value' or 'target_id', if not provided - result is based on field common usage.

$context: (sting) specify context string, if not provided will use default context.

1 call to config_pages_get()
config_pages_render_field in ./config_pages.inc
Theme specified config.

File

./config_pages.inc, line 49
Logic functions.

Code

function config_pages_get($type, $field_name, $default = NULL, $delta = 0, $key = NULL, $context = NULL) {
  $instance = field_info_instance('config_pages', $field_name, $type);

  // Load config.
  if ($context === NULL) {
    $context = config_pages_context_get($type);
  }
  $config = config_pages_load_entity($type, $context);

  // Get field info.
  if (empty($key) && !is_array($key)) {
    $keys_known = array(
      'number' => 'value',
      'text' => 'value',
      'list' => 'value',
      'image' => array(),
    );
    $field = field_info_field($field_name);
    if (!empty($keys_known[$field['module']])) {
      $key = $keys_known[$field['module']];
    }
    else {
      $key = array();
    }
  }

  // Load items.
  if (!empty($config)) {
    $items = field_get_items('config_pages', $config, $field_name);
  }
  else {

    // Default field values used.
    $items = !empty($instance['default_value']) ? $instance['default_value'] : array();
  }
  $deltas = is_array($delta) ? $delta : array(
    $delta,
  );
  if (empty($deltas)) {
    $deltas = is_array($items) ? array_keys($items) : array();
  }

  // Fetch info for given deltas.
  $result = array();
  foreach ($deltas as $_delta) {
    $result[$_delta] = isset($items[$_delta]) ? $items[$_delta] : $default;
    if (is_array($result[$_delta]) && !empty($key) && !is_array($key)) {
      $result[$_delta] = isset($result[$_delta][$key]) ? $result[$_delta][$key] : $default;
    }
  }
  return is_array($delta) ? $result : $result[$delta];
}