0

I'm using the Content:Nid contextual filter in my view (with exposed filters) and would like to dynamically set the value of this contextual filter from the exposed form validation callback. Is this possible?

2 Answers 2

0

Contextual filters work with arguments from the url so for example {drupal site}/search-nid/% where search-nid is the url of your page and % is the value you wish to submit to the contextual filter.

An exposed filter tends to take it's values from the query string E.G. {drupal site}/search-nid?field_nid=% (Again search-nid is the page url and % is the value of the nid)

If you are trying to do what I think you are, (Filter results to a specific nid using a URL to "autofill" the filter) I did this in a hook_form_submit function and not a hook_form_validate function. The function below assumes your module is mymodule and your form id is advancedsearch_form. The function looked as follows:

<?php 
/**
 * Implements hook_form_submit()
 * Takes values from submitted form and appends them to query string
 * uses @see drupal_goto for info one $form_state['#redirect']
 */
function mymodule_advancedsearch_form_submit($form, &$form_state) {
  $qsa = array(); //for QSA arguments

  $field_nid = $form_state['values']['field_nid'];
  $qsa['field-nid'] = $field_nid;

  $form_state['redirect'] = array(
    'search-nid', 
    array(
      'query' => $qsa,
    ),
    302,
  );
}

This may not be best practise (In fact I'm sure it isn't as it was a quick hack I implemented in about 5 mins!) but I hope it does answer your question.

3
  • This is not exactly what I'm trying to do. I'm not using URL arguments at all and I need to be able to dynamically setup the contextual filter from the views exposed form validation callback.
    – ovi
    Mar 20, 2013 at 19:57
  • Do you mean you want to dynamically add which field is used for the contextual filter? Mar 23, 2013 at 10:46
  • No, I want to setup the value of the Content: Nid contextual filter from inside the view exposed form validation callback.
    – ovi
    Mar 23, 2013 at 14:26
0

I succeeded doing this by modifying $form_state['view']->args[0], which is the first argument (contextual filter) for the view. In my case, this was the Content: Nid contextual filter.

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.