1 field.crud.inc field_read_fields($params = array(), $include_additional = array())

Reads in fields that match an array of conditions.

Parameters

array $params: An array of conditions to match against.

array $include_additional: The default behavior of this function is to not return fields that are inactive or have been deleted. Setting $include_additional['include_inactive'] or $include_additional['include_deleted'] to TRUE will override this behavior.

Return value

An array of fields matching $params. If $include_additional['include_deleted'] is TRUE, the array is keyed by field id, otherwise it is keyed by field name.

Related topics

File

core/modules/field/field.crud.inc, line 328
Field CRUD API, handling field and field instance creation and deletion.

Code

function field_read_fields($params = array(), $include_additional = array()) {
  $include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive'];
  $include_deleted = isset($include_additional['include_deleted']) && $include_additional['include_deleted'];

  $prefix = 'field.field.';
  if (isset($params['field_name'])) {
    $config_names = array($prefix . $params['field_name']);
  }
  else {
    $config_names = config_get_names_with_prefix($prefix);
  }

  $fields = array();
  foreach ($config_names as $config_name) {
    $config = config($config_name);
    $field = $config->get();

    // Continue if matching against a specific field that doesn't exist.
    if (isset($params['field_name']) && empty($field)) {
      continue;
    }
    else {
      unset($params['field_name']);
    }

    // Continue if the config does not match passed parameters.
    if ($params) {
      foreach ($params as $param_key => $param_value) {
        if ($field[$param_key] != $param_value) {
          continue 2;
        }
      }
    }

    // Continue if this is an inactive field.
    if ((!$field['active'] || !$field['storage']['active']) && !$include_inactive) {
      continue;
    }
    // Or a deleted field.
    if ($field['deleted'] && !$include_deleted) {
      continue;
    }

    module_invoke_all('field_read_field', $field);

    // Populate storage information.
    module_load_install($field['module']);
    $schema = (array) module_invoke($field['module'], 'field_schema', $field);
    $schema += array(
      'columns' => array(),
      'indexes' => array(),
      'foreign keys' => array(),
    );
    $field['columns'] = $schema['columns'];
    $field['indexes'] = $schema['indexes'];
    $field['foreign keys'] = $schema['foreign keys'];

    // Populate defaults.
    $field += field_defaults_field($field['type']);
    $field['settings'] += field_info_field_settings($field['type']);

    $field_name = $field['field_name'];
    $fields[$field_name] = $field;
  }

  return $fields;
}