Programatically Remove Field from View (Views 2.x)

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
laceysanderson's picture

I'm working on a module that needs to programatically remove a field from a view (both in the display and the query). The field needs to only be removed for their particular instance (ie: if they have two pages of the same view open and decide to remove a given field from one it should still be visible in the other page as well as any other users pages). Is this possible?

I have the form with select list made available in the header (since it's not really a filter I didn't want to simply create a filter handler -seemed confusing to users). I've supplied the current view using $view = views_get_current_view();

To remove the field I tried:
Use views_db_object::set_item with NULL item

<?php
$view
->set_item('page_1','field','name_1', NULL);
?>

Where name_1 is the id of the field I want to remove. It was suggested that passing a NULL item into this function would remove the field but on dpm of the views object after this command the field was still there and when the page finished refreshing due to form submit, the field was also still there.

Unset field object in all places I could find

<?php
unset(
   
$view->display['page_1']->handler->handlers['field']['name_1'],
   
$view->field['name_1']
);
?>

When I dpm'd the views object directly after unset the field was gone wherever I expected it to be but on refresh of the page due to form submit, the field was still there even though it wasn't set in the views object. So apparently my modifactions are being kept when the view refreshes after the submit.

Thus I must simply be removing the field incorrectly. Maybe I've missed places the field object exists and should be unset? Or, hopefully, there is a better way altogether to do this...

Thanks for any help!
~Lacey

Comments

Same problem

Jivan's picture

Hi, just to up this topic.

I'm exactly in the same situation. So if you eventually found the answer, could you please post it ?

I know that for instance, this works :

<?php
unset($view->sort['my_sort_criteria_title']);
?>

But this doesn't :

<?php
unset($view->field['my_field_title']);
?>

Maybe $view->field is not the correct address for a field. Or maybe this is the wrong method, who could tell ?

A Little Insight -sort of a Solution

laceysanderson's picture

So it turned out the problem was not so much what I was unsetting to remove fields but when I was doing it. When you access the view object in the header of the view, the view has already finished rendering it's content part (where the fields are rendered). Therefore, unsetting the field at this point has no effect because it's essentially not used again at this point.

I was removing fields as part of a dynamic "Allow the user to add/remove columns to/from their table view" functionality. Thus I created a views style plugin so I could catch the view before rendering and unset the field. You could also use a filter handler (catch it at the handler::pre_render() function).

I went with simply hiding the fields using the tpl file, although I don't remember why...

Anyway in case it can help you, my module is in my sandbox: http://drupal.org/sandbox/laceysanderson/1647742
One thing to note: It's only compatible with Drupal 6 and Views 2 -I'll get around to updating it one day :)

Hope this helps!
~Lacey

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week