1

I have a content type, referenced by multiple subsequent content types and at some point I made a spreadsheet with all that data. My new assignment is to display a view like that and then export it to Excel, but before that let the user choose which columns appear in the table. I'm also going to need to be able to display exposed filters for all the chosen columns and save the columns and the filters as a report template.

Is it possible to do all that using Views or perhaps create it from scratch as a new entity?

I've been trying to do that in Views and I've hit a few walls. I thought about creating a bunch of PHP fields for all possible columns and check if they should be displayed based on the list of columns to display. Displaying data that way worked, but I can't put filters on PHP fields so I pretty much can't use them here.

After that I tried simply displaying all the columns initially and then in the "modify output" part of the fields display an empty string if that column is not on the list of columns to display (this way I could simply hide the empty columns). Only to realize you can't put PHP code anywhere in the view field settings.

Haven't managed to get that far but I think you can't simply display a node and x referencing nodes in one table row. How it works in my db is that there's a "Examination" content type, which has different types. Multiple examinations might reference the same node so I'd have to find a way to display that node and all referencing examination types in the same row. Assuming each type can occur once.

^ This part might deserve it's own question though.

To put it shortly: I need to populate table view columns based on a list of columns while still being able to expose full filters to each column

1

What you'll want to do is create a new display (using the table style) on the fly and then use that display to render the output.

If you take a look at views exports, you can get an idea of how columns are added to new displays:

From commerce_cart.views_default.inc:

$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['title'] = 'Shopping cart';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'quantity' => 'quantity',
  'commerce_display_path' => 'commerce_display_path',
  'line_item_title' => 'line_item_title',
  'commerce_total' => 'commerce_total',
);

// Ignoring relationships...

$handler->display->display_options['style_options']['info'] = array(
  'quantity' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
      'align' => '',
    'separator' => '',
  ),
  'commerce_display_path' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
  ),
  'line_item_title' => array(
    'align' => '',
    'separator' => '',
  ),
  'commerce_total' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => 'views-align-right',
    'separator' => '',
  ),
);

  /* Field: Commerce Line Item: Quantity */
  $handler->display->display_options['fields']['quantity']['id'] = 'quantity';
  $handler->display->display_options['fields']['quantity']['table'] = 'commerce_line_item';
  $handler->display->display_options['fields']['quantity']['field'] = 'quantity';
  $handler->display->display_options['fields']['quantity']['relationship'] = 'commerce_line_items_line_item_id';
  $handler->display->display_options['fields']['quantity']['label'] = '';
  $handler->display->display_options['fields']['quantity']['precision'] = '0';
  $handler->display->display_options['fields']['quantity']['suffix'] = ' × ';
  /* Field: Commerce Line item: Display path */
  $handler->display->display_options['fields']['commerce_display_path']['id'] = 'commerce_display_path';
  $handler->display->display_options['fields']['commerce_display_path']['table'] = 'field_data_commerce_display_path';
  $handler->display->display_options['fields']['commerce_display_path']['field'] = 'commerce_display_path';
  $handler->display->display_options['fields']['commerce_display_path']['relationship'] = 'commerce_line_items_line_item_id';
  $handler->display->display_options['fields']['commerce_display_path']['label'] = '';
  $handler->display->display_options['fields']['commerce_display_path']['exclude'] = TRUE;

And so on. Then, just save the view (views_save_view()) and render the output. Commerce has a function called commerce_embed_view() that shows how to make this happen. This is slightly modified, though:

// You can get the ID after you save it.
$view->set_display($display_id);

// Set the specific arguments passed in. Optional.
$view->set_arguments($arguments);

// Prepare and execute the View query.
$view->pre_execute();
$view->execute();

// Return the rendered View.
return $view->render();
1

I found a module that pretty much covers what I needed to do:

Views Dynamic Fields

Insead of providing a list of columns you can simply let the user choose which columns to display.

As for the exposed filters, my idea, albeit not a perfect one, would be to create exposed filters for all possible fields and upon applying the Dynamic Fields filter using some jQuery to hide/display respective filters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.