You are here

function nr_fields_views_pre_view in Node registration 7

Implemnts hook_views_pre_view().

Dynamically add node registration private fields to views listings.

File

submodules/nr_fields/nr_fields.module, line 71

Code

function nr_fields_views_pre_view($view, $display_id, $args) {
  if ($view->base_table == 'node_registration') {
    $fields = $view->display_handler
      ->get_option('fields');
    $arguments = $view->display_handler
      ->get_option('arguments');

    // Get the node this view is operating on.
    $node = NULL;
    $key = 0;
    foreach ($arguments as $argument) {
      if ($argument['table'] == $view->base_table && $argument['field'] == 'nid' && isset($view->args[$key])) {
        $nid = $view->args[$key];
        $node = node_load($nid);
        break;
      }
      $key++;
    }

    // We iterate over each field. Fields of type nr_fields_private_fields are
    // replaced by the actual private fields of the node.
    $new_fields = array();
    foreach ($fields as $field_name => $field) {
      if ($field['field'] == 'nr_fields_private_fields') {
        if ($node) {

          // Retrieve all the private fields for this node.
          $bundle = 'node_' . $node->nid;
          $instances = field_info_instances('node_registration', $bundle);

          // Each field is added as a Views field.
          foreach ($instances as $instance) {
            $new_field_name = $field['id'] . '_' . $instance['field_name'];
            $new_fields[$new_field_name]['id'] = $instance['field_name'];
            $new_fields[$new_field_name]['table'] = _field_sql_storage_tablename($instance);
            $new_fields[$new_field_name]['field'] = $instance['field_name'];
          }
        }
      }
      else {
        $new_fields[$field_name] = $field;
      }
    }

    // Replace old fields with new fields.
    $view->display_handler
      ->set_option('fields', $new_fields);
  }
}