In this tutorial I'll show how you can programmatically and dynamically remove a column from your view. In my example, I chose to hide an email column from unauthenticated users, but you could apply this code to do pretty much anything.
To get things started, I defined a content type of "Profile" to contain some user details (using the Content Profile module) and used the Devel module's generate users and nodes functionality to create some sample data. I then created a view to show the data:

Here is my page view display:

Now you can add the hook_views_pre_build() function in your module. Since the $view object is large, I recommend using krumo() (which comes with the Devel module) to browse through the $view object's properties.
<?php
function MYMODULE_views_pre_build(&$view) {
// check for your view name (in my example: people)
if ($view->name=='people') {
krumo($view);
}
}
?>The above code will neatly format the $view object in a hierarchical display. You can click to expand each class property.

As you can see, the $view object has a lot of data in it. At this point, you'll need to get acquainted with the structure of the handler object of the view ($view->display['default']->handler) and determine what to modify. To remove the email column for unauthenticated users, you can add the following code:
<?php
function MYMODULE_views_pre_build(&$view) {
// check for your view name (in my example: people)
if ($view->name=='people') {
// check if the user is anon
if (in_array('anonymous user',$GLOBALS['user']->roles)) {
// remove view hander properties for email column
unset(
$view->display['default']->handler->options['fields']['mail'],
$view->display['default']->handler->options['style_options']['columns']['mail'],
$view->display['default']->handler->options['style_options']['info']['mail']
);
}
}
}
?>When I logout, my view no longer shows the email column:






















Comments
Great post
wanted to ask
why you did not used
global $user;
if (!$user->id)
instead of
if (in_array('anonymous user',$GLOBALS['user']->roles))
Thank u!
super globals
Just as a personal perference, I only use the global keyword if I want to modify the variable, and bring it into my current scope.
Great info. May add few direction.
Thanks for the great info on hook_views_pre_build() function. I have a suggestion. Your post is explanatory yet you may add at some point that a user need to create his own module or where to add these codes. It would be a great help for any newbie.
With regards
Mohanraj Thangarasu
www.drupaltricks.com
suggestion
Thanks Moharraj, great suggestion. Someday I should write a blog entry about creating a basic Drupal module. Until then: http://drupal.org/developing/modules
Cheers,
Eric
drupal blog
I heard that Drupal is a nice blog CMS. I am interested to use but i do not know how. Is it working like wordpress?
personally
I have used Wordpress and Joomla in the past, and I have since switched to Drupal. I can't see myself ever going back at this point. Although Drupal can blog out of the box just fine, it is oriented as a framework of building blocks.
Awesome!
Thanks, I just needed something like this!
DOES Remove HTML
Answered my own question. The solution you provided *does* remove the table HTML as well. Thanks so much!
Remove Table HTML?
Thanks for the post! Love your site.
I'm interested to know if your solution removes the table html as well as the cell contents. The garland/minelli table styling makes it impossible to tell visually whether or not there are still empty or tags. Does your code actually remove all of the HTML table code, or the just content of the column?
Hope that makes sense. I'm really trying to find a good solution to this.