0

I'm trying to programmatically set the starting value of an exposed view filter (so that it initially loads the view with data matching that value).

I've managed to change the filter field value with this code in a views_pre_build hook (doesn't work in pre_execute or pre_render):

$view->display[$view->current_display]->handler->handlers['filter']['field_year_value']->value['value'] = $year;

But the view still loads with the value from the view filter's config value. How can I get my change reflected in the view when it loads?

1

For some reason a site I was adding a feature to had a years taxonomy vocabulary, with a number of terms, one for each year in a range. So I wanted to auto set the exposed filter to the current year, but to do so needed to set the filter value with the tid of the right term.

I implement hook_views_query_alter().

check if the view is the right name and if the view doesn't have any user selected input (this view only had one filter).

You have to alter the value for the filter's query value

You have to unset the exposed filter area markup and regenerate it.

 function mymodule_views_query_alter(&$view, &$query) {

      if($view->name == 'executive_committee' && empty($view->exposed_input)) {
        $current_year = date('Y');
        // find year term id
        $term_query = new EntityFieldQuery();
        $tids = $term_query->entityCondition('entity_type', 'taxonomy_term')
          ->propertyCondition('name', $current_year)
          ->propertyCondition('vid', 3)
          ->execute();

        if(!empty($tids['taxonomy_term'])) {
          $tids = array_keys($tids['taxonomy_term']);
          $current_year_tid = reset($tids);

          // set values to the views object and query objet
          $view->exposed_data['field_year_tid'] = $current_year_tid;
          unset($view->exposed_widgets);
          $view->exposed_raw_input['field_year_tid'] = $current_year_tid;
          $query->where[1]['conditions'][2]['value'] = $current_year_tid;
          // rebuild the exposed form
          $form_state = array(
            'view' => $view,
            'display' => $view->display_handler->display,
            'exposed_form_plugin' => $view->display_handler->get_plugin('exposed_form'),
            'method' => 'get',
            'rerender' => TRUE,
            'no_redirect' => TRUE,
            'always_process' => TRUE, // This is important for handle the form status.
          );
          $form = drupal_build_form('views_exposed_form', $form_state);
          $form['field_year_tid']['#value'] = $current_year_tid;
          $view->exposed_widgets = drupal_render($form);

        }

      }
    }
  • You didn't say which Drupal version, but this is for D7 – jackrabbithanna Jun 3 '17 at 15:53
  • It is 7, thought I'd put that in the tittle but I guess not... – whiterabbit25 Jun 3 '17 at 21:12
  • This is looking good, thanks! The only thing was it wasn't updating the field's value, just the view's output. I poked around a bit and managed to do that with this code: $view->display_handler->display->handler->view->display[$view->current_display]->handler->handlers['filter']['field_year_value']->value['value'] = $year; – whiterabbit25 Jun 3 '17 at 21:49
  • That of course goes before declaring the form_state – whiterabbit25 Jun 3 '17 at 21:55
  • Scratch that, I'm dumb. I copy/pasted your code and forgot to change one of your fieldname instances to mine. Works perfectly once I drilled down to where the value's stored in the form. – whiterabbit25 Jun 4 '17 at 4:26

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.