Skip to main content
replaced http://drupal.stackexchange.com/ with https://drupal.stackexchange.com/
Source Link

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

[EDIT]

As for actually adding the filter. I recommend using the view::add_item() function instead of just manually adding to the view object because it does some extra work with the handler that isn't being done by your current code.

Some examples of using add_item() are http://drupal.stackexchange.com/a/134872/10729https://drupal.stackexchange.com/a/134872/10729 and http://drupal.stackexchange.com/a/70649/10729https://drupal.stackexchange.com/a/70649/10729

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

[EDIT]

As for actually adding the filter. I recommend using the view::add_item() function instead of just manually adding to the view object because it does some extra work with the handler that isn't being done by your current code.

Some examples of using add_item() are http://drupal.stackexchange.com/a/134872/10729 and http://drupal.stackexchange.com/a/70649/10729

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

[EDIT]

As for actually adding the filter. I recommend using the view::add_item() function instead of just manually adding to the view object because it does some extra work with the handler that isn't being done by your current code.

Some examples of using add_item() are https://drupal.stackexchange.com/a/134872/10729 and https://drupal.stackexchange.com/a/70649/10729

Added part about using the add_item() function.
Source Link
rooby
  • 14.7k
  • 1
  • 40
  • 82

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

[EDIT]

As for actually adding the filter. I recommend using the view::add_item() function instead of just manually adding to the view object because it does some extra work with the handler that isn't being done by your current code.

Some examples of using add_item() are http://drupal.stackexchange.com/a/134872/10729 and http://drupal.stackexchange.com/a/70649/10729

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

[EDIT]

As for actually adding the filter. I recommend using the view::add_item() function instead of just manually adding to the view object because it does some extra work with the handler that isn't being done by your current code.

Some examples of using add_item() are http://drupal.stackexchange.com/a/134872/10729 and http://drupal.stackexchange.com/a/70649/10729

Add a comment to the code example.
Source Link
rooby
  • 14.7k
  • 1
  • 40
  • 82

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();

To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view->pre_execute();
$output = $view->display_handler->preview();
$view->execute();
$view->post_execute();

They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view->save();
Source Link
rooby
  • 14.7k
  • 1
  • 40
  • 82
Loading