1

So I want to create a view filter programatically using hook_update in the .install file. I feel like I have it going, but the changes will not show up in the UI. Here is what I am doing, and the results:

This view already contains 2 filters. I used this code to view those filters:

$filters = $view->display_handler->get_option('filters');
var_dump($filters);

and it was clear that they were there. Then I went into the UI and created the new filter using the GUI tools there. Then I ran the above code again, and it showed me all 3 filters (the 2 previous ones, plus my new one). So far so good.

So now I backtracked the database to the initial state, so I only had 2 filters again. Then inside my install file I ran this code:

$name = "soccer_scores";
$view_display = 'default';
$view = views_get_view($name);
$view->init_display($view_display);
$view->preview = TRUE;
$view->is_cacheable = FALSE;


$filters = $view->display_handler->get_option('filters');

$filters['field_is_livescore_value'] = array(
'id' => 'field_is_livescore_value',
'table' => 'field_data_field_is_livescore',
... a bunch of other array data

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

So after the install file runs, when that last var_dump runs, it prints out the $filters array and it looks exactly like when I created the new filter using the GUI. However, when I reload the admin area for that view, no new filter appears, it just shows the existing 2 filters, but not the new one.

So I feel there is some step I am missing to make the change "stick". I can find so few docs on this, can anyone help?

1 Answer 1

0

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

2
  • This doesn't quite work for me - it DOES save the current 2 filters to the database, but doesn't add the third. Is there any step I need to do before this, or after to finalize it or something? Commented Feb 1, 2016 at 16:33
  • I would say that means that the way you are adding the filter is not correct. See this post for adding fields - drupal.stackexchange.com/a/134872/10729 - and this one for adding header and footer fields - drupal.stackexchange.com/a/70649/10729 - You can add filters in the same way, by using the add_item() function.
    – rooby
    Commented Feb 1, 2016 at 21:34

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.