You are here

function webform_views_pre_view in Webform 7.4

Implements hook_view_pre_view().

File

views/webform.views.inc, line 535
Views hooks implemented for the Webform module.

Code

function webform_views_pre_view($view, $display_id, $args) {
  $display = $view->display[$display_id];
  $all_fields_id = _webform_view_find_id($view, $display_id, 'field', array(
    'field' => 'webform_all_fields',
  ));
  if ($all_fields_id !== NULL && !empty($args[0]) && is_numeric($args[0]) && $args[0] > 0 && _webform_view_find_id($view, $display_id, 'argument', array(
    'field' => 'nid',
    'table' => 'webform_submissions',
  )) !== NULL && ($node = node_load($args[0])) && isset($node->webform['components'])) {

    // This is a view/display that needs its fields expanded. It contains the
    // webform_all_fields field, has a nid argument to the webform_submission
    // table that is a valid node. Retrieve the display's fields and remove any
    // fields after the 'webform_all_fields' field.
    $fields = $view
      ->get_items('field', $display_id);
    $prototype = $fields[$all_fields_id];
    $field_index = array_flip(array_keys($fields));
    $trailing_fields = array_slice($fields, $field_index[$all_fields_id] + 1, NULL, TRUE);
    $fields = array_slice($fields, 0, $field_index[$all_fields_id], TRUE);

    // Remove any fields after the webform_add_fields field.
    $new_columns = array();
    foreach ($node->webform['components'] as $component) {
      if (webform_component_invoke($component['type'], 'table', $component, array(
        '',
      )) !== NULL) {
        $new_id = 'webform_component_' . $component['cid'];
        $new_fields = array(
          array(
            'id' => $new_id,
            'field' => 'value',
            'table' => 'webform_submissions',
            'label' => $component['name'],
            'webform_nid' => $node->nid,
            'webform_cid' => $component['cid'],
            'exclude' => 0,
          ) + $prototype,
        );
        if (webform_component_implements($component['type'], 'view_field')) {
          $new_fields = webform_component_invoke($component['type'], 'view_field', $component, $new_fields);
        }
        foreach ($new_fields as $sub_id => $new_field) {
          $field_id = $new_id . ($sub_id ? '_' . $sub_id : '');
          $fields[$field_id] = $new_field;
          $new_columns[$field_id] = $field_id;
        }
      }
    }

    // Add any trailing fields back in.
    $fields += $trailing_fields;

    // Store. Alas, there is no view::set_items() method.
    $display->handler
      ->set_option('fields', $fields);

    // If this display's style is a table, add columns for click-sorting.
    // Note: Test for count($new_columns) is necessary because prior to PHP 5.6,
    // array_fill requires a positive number of elements to insert.
    if ($display->handler
      ->get_option('style_plugin') == 'table' && count($new_columns)) {
      $style_options = $display->handler
        ->get_option('style_options');
      $style_options['columns'] += $new_columns;
      $style_prototype = isset($style_options['info'][$all_fields_id]) ? $style_options['info'][$all_fields_id] : array();
      $style_prototype += array(
        'sortable' => 1,
        'default_sort_order' => 'asc',
        'align' => '',
        'separator' => '',
        'empty_column' => 0,
      );
      $style_options['info'] += array_combine($new_columns, array_fill(1, count($new_columns), $style_prototype));
      $display->handler
        ->set_option('style_options', $style_options);
    }

    // Reset field handlers cache and rebuild field handlers.
    unset($display->handler->handlers['field']);
    $display->handler
      ->get_handlers('field');

    // Allow other modules to alter these modifications to the view.
    drupal_alter('webform_view', $view, $display_id, $args);
  }
}