You are here

function _entityform_view_add_all_fields in Entityform 7.2

Same name and namespace in other branches
  1. 7 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 1598
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, $view_mode) {
  $instances = field_info_instances('entityform', $bundle_name);
  $view_mode_settings = field_view_mode_settings('entityform', $bundle_name);

  // when adding autofields to view let view modes determine which fields should be include and in what order
  $autofields = array();
  foreach ($instances as $instance) {
    if (!empty($view_mode_settings[$view_mode]['custom_settings']) && isset($instance['display'][$view_mode])) {
      $field_display = $instance['display'][$view_mode];
    }
    else {
      $field_display = $instance['display']['default'];
    }

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

      // Use the field label for this instance of the field.
      if (!empty($instance['label'])) {
        $autofield['options']['label'] = t($instance['label']);
      }
      $autofields[] = $autofield;
    }
  }
  if ($autofields) {

    // Add settings to indicate the View has autofields added from Entitform
    $view->display[$display_id]->entityform_settings = array(
      'autofields' => TRUE,
      'view_mode' => $view_mode,
      'entityform_type' => $bundle_name,
    );

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