I have a custom View defined in a module which provides a table display of data which takes a Node ID as an argument.

| Col 1  | Col 2 |
| A      | B     |

I want to be able to check some data from the node that is the argument to see if a certain field is set to either 0 (no extra column) or 1 (an extra column).

Using hook_views_default_views_alter() I can add an extra column to the table using:

function MODULE_NAME_views_default_views_alter(&$views) {
  if (isset($views['VIEW_NAME'])) {
    // Add an extra field to the rider times view
    $handler = &$views['VIEW_NAME']->display['default']->handler;
    $handler->display->display_options['fields']['nothing']['id'] = 'nothing';
    $handler->display->display_options['fields']['nothing']['table'] = 'views';
    $handler->display->display_options['fields']['nothing']['field'] = 'nothing';
    $handler->display->display_options['fields']['nothing']['label'] = '';
    $handler->display->display_options['fields']['nothing']['alter']['text'] = 'Download';
    $handler->display->display_options['fields']['nothing']['alter']['make_link'] = TRUE;
    $handler->display->display_options['fields']['nothing']['alter']['path'] = 'download/[nid]/[number]';
    $handler->display->display_options['fields']['nothing']['element_label_colon'] = FALSE;

But this adds the column to all the tables regardless of the argument.

I guess there should be a way to add the column through hook_views_pre_render() but I can't figure out how.

The alternative would be to set up 2 views. One with the extra column and one without and conditionally show the correct view based on a Context/Panel visibility filter.

1 Answer 1


I don't know if this is the best way to do it, but when I've needed conditional columns I've removed them in hook_views_pre_build() (which is before the query gets built). The code would be something like this:

 * Implements hook_views_pre_build().
 * Removes column X on condition Y from all displays of view Z.
function MYMODULE_views_pre_build(&$view) {

  // Check this is the right view.
  if ($view->name != 'Z') {

  // Check if the column should be removed.
  if (!$Y) {


The code above will affect all displays of the view. If you want to target a particular display just check against $view->current_display as well.

  • Whether it's the best way or not, it works perfectly and I can't see any issues. Thank you.
    – jamestsymp
    Jul 14, 2014 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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