Add a column to a Views table display conditional on argument 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(&amp;$views) {
  if (isset($views['VIEW_NAME'])) {
    // Add an extra field to the rider times view
    $handler = &amp;$views['VIEW_NAME']-&gt;display['default']-&gt;handler;
    $handler-&gt;display-&gt;display_options['fields']['nothing']['id'] = 'nothing';
    $handler-&gt;display-&gt;display_options['fields']['nothing']['table'] = 'views';
    $handler-&gt;display-&gt;display_options['fields']['nothing']['field'] = 'nothing';
    $handler-&gt;display-&gt;display_options['fields']['nothing']['label'] = '';
    $handler-&gt;display-&gt;display_options['fields']['nothing']['alter']['text'] = 'Download';
    $handler-&gt;display-&gt;display_options['fields']['nothing']['alter']['make_link'] = TRUE;
    $handler-&gt;display-&gt;display_options['fields']['nothing']['alter']['path'] = 'download/[nid]/[number]';
    $handler-&gt;display-&gt;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.
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(&amp;$view) {

  // Check this is the right view.
  if ($view-&gt;name != 'Z') {
    return;
  }

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

  unset($view-&gt;field['X']);
}


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