Articles


How to add an argument to a view pro grammatically in Drupal 7?

How to add an argument to a view pro grammatically in Drupal 7?



Posted byJibin Jose,1st Nov 2016

For  one of our drupal 7 Site need to pass the argument to the views programatically. The Drupal 7 contributed hook helps for the same. 

The below is a sample for adding the views argument programatically.

/** * Implement hooks_views_pre_view * */

function hook_views_pre_view(&$view, &$display_id, &$args) {
  // Change the display if the acting user has 'administer site configuration'
  // permission, to display something radically different.
  // (Note that this is not necessarily the best way to solve that task. Feel
  // free to contribute another example!)
  if ($view->name == 'my_view_machine_name') {
     $view->set_arguments(array('arg1', 'arg2'));
  }
}

Related Articles