1.20.x field.info.inc _field_info_prepare_instance($instance, $field)

Prepares an instance definition for the current run-time context.

Since the instance was last saved or updated, a number of things might have changed: widgets or formatters disabled, new settings expected, new view modes added...

Parameters

$instance: The raw instance structure as read from the database.

$field: The field structure for the instance.

Return value

Field instance array.:

Related topics

File

modules/field/field.info.inc, line 323
Field Info API, providing information about available fields and field types.

Code

function _field_info_prepare_instance($instance, $field) {
  // Make sure all expected instance settings are present.
  $instance['settings'] += field_info_instance_settings($field['type']);

  // Set a default value for the instance.
  if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) {
    $instance['default_value'] = NULL;
  }

  $instance['widget'] = _field_info_prepare_instance_widget($field, $instance['widget']);

  foreach ($instance['display'] as $view_mode => $display) {
    $instance['display'][$view_mode] = _field_info_prepare_instance_display($field, $display);
  }

  // Fallback to 'hidden' for display modes configured to use custom display
  // settings, and for which the instance has no explicit settings.
  $entity_info = entity_get_info($instance['entity_type']);
  $view_modes = array_merge(array('default'), array_keys($entity_info['view modes']));
  $view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']);
  foreach ($view_modes as $view_mode) {
    if ($view_mode == 'default' || !empty($view_mode_settings[$view_mode]['custom_settings'])) {
      if (!isset($instance['display'][$view_mode])) {
        $instance['display'][$view_mode] = array(
          'type' => 'hidden',
          'label' => 'hidden',
          'settings' => array(),
          'weight' => 0,
        );
      }
    }
  }

  return $instance;
}