Fake an instance of a field.

Parameters

$field_name: The unique name for this field no matter what entity/bundle it may be used on.

$view_mode: We're building a new view mode for this function. Defaults to ctools, but we expect developers to actually name this something meaningful.

$formatter: The formatter key selected from the options provided by field_ui_formatter_options().

$formatter_settings: An array of key value pairs. These will be used as #default_value for the form elements generated by a call to hook_field_formatter_settings_form() for this field type. Typically we'll pass an empty array to begin with and then pass this information back to ourselves on form submit so that we can set the values for later edit sessions.

1 call to ctools_fields_fake_field_instance()
ctools_fields_get_field_formatter_settings_form in includes/fields.inc
Helper function for calling hook_field_formatter_settings_form() without needing to load an instance of the field.

File

includes/fields.inc, line 21
Extend core fields with some helper functions to reduce code complexity within views and ctools plugins.

Code

function ctools_fields_fake_field_instance($field_name, $view_mode, $formatter, $formatter_settings) {
  $view_mode = isset($view_mode) ? $view_mode : 'ctools';
  $field = field_read_field($field_name);
  $field_type = field_info_field_types($field['type']);
  return array(
    // Build a fake entity type and bundle.
    'field_name' => $field_name,
    'entity_type' => 'ctools',
    'bundle' => 'ctools',
    // Use the default field settings for settings and widget.
    'settings' => field_info_instance_settings($field['type']),
    'widget' => array(
      'type' => $field_type['default_widget'],
      'settings' => array(),
    ),
    // Build a dummy display mode.
    'display' => array(
      $view_mode => array(
        'type' => $formatter,
        'settings' => $formatter_settings,
      ),
    ),
    // Set the other fields to their default values.
    // @see _field_write_instance().
    'required' => FALSE,
    'label' => $field_name,
    'description' => '',
    'deleted' => 0,
  );
}