You are here

function _entityform_view_add_all_fields in Entityform 7

Same name and namespace in other branches
  1. 7.2 entityform.module \_entityform_view_add_all_fields()

Add automatically add all the fields for a Bundle to a View

1 call to _entityform_view_add_all_fields()
entityform_views_pre_view in ./entityform.module
Implements hook_views_pre_view().

File

./entityform.module, line 1403
Module for the Entityform Entity - a starting point to create your own Entity and associated administration interface

Code

function _entityform_view_add_all_fields(&$view, $display_id, $bundle_name) {
  $instances = field_info_instances('entityform', $bundle_name);

  // when adding autofields to view let view modes determine which fields should be include and in what order
  switch ($display_id) {
    case 'autofields_csv':
    case 'autofields_xml':
      $view_mode = 'download';
      break;
    case 'autofields_table':
      $view_mode = 'table';
      break;
    default:
      $view_mode = 'default';
  }
  $autofields = array();
  foreach ($instances as $instance) {
    $field_display = isset($instance['display'][$view_mode]) ? $instance['display'][$view_mode] : $instance['display']['default'];

    // don't add to autofields if this field was hidden in the view mode
    if ($field_display['type'] != 'hidden') {
      $autofields[] = array(
        'field_name' => $instance['field_name'],
        'options' => array(
          'type' => $field_display['type'],
          'settings' => $field_display['settings'],
        ),
        'weight' => $field_display['weight'],
      );
    }
  }

  // reorder fields to match view mode
  uasort($autofields, 'drupal_sort_weight');
  foreach ($autofields as $field) {
    $view
      ->add_item($display_id, 'field', "field_data_{$field['field_name']}", $field['field_name'], $field['options']);
  }
}