I need to alter my view filter option!

The view must display some comments. Every comment, that I need, has custom field "node reference". And I want to filter comments by this field.

E.g. if my view displays on page with node 9(nid), view should return me all comments where "node reference" = 9;

I have found such solution: http://www.drupaldump.com/view-3-alter-filters-programmatically

And try to rewrite for me:

function mymodule_views_pre_view(&$view) {

  if ($view->name === 'contest_winners') {
    $view_filters = $view->display_handler->get_option('filters');

    $node_id = arg(1);

    $view_filters['field_for_contest_nid']['value'] = null;
    $view_filters['field_for_contest_nid']['value'][$node_id] = inttostr($node_id);
    $overrides = array();
    $overrides['filters'] = $view_filters;
    foreach ($overrides as $option => $definition) {
      $view->display_handler->override_option($option, $definition);

    dpm($view_filters); // here value = node_id, but view use default value



But this does not help: view uses default value.

Maybe i have made some stupid mistake?


It seems you read not complete article :(. before this method getting call we should tell hey views we want use your hooks

something like in your .module file

* Implements hook_views_api().
function yourmodule_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'yourmodule') . '/views',

Then create a views directory, create yourmodule..views.inc file there and put your code there ( please be sure your code getting execution with some dsm or die() ).

I know your change your way but I think it help you in another task.

take a look at : this article

  • thanks. It seems this is a solution. But I have already fixed it and don`t remember in which way – Q-bart Jun 18 '16 at 9:50
  • @Q-bart you're welcome,I post this maybe help another one in future.and if you think it's ok mark as accepted please. – Yusef Jun 18 '16 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.