2

I have a view with exposed filters in Drupal 7 and I need to set the default value of a filter to current year. I also need to unset/remove the - Any - option from the select list.

7 Answers 7

5

This solution is working on Drupal >= 8

function YOURMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-my-view-display') {
    $request = \Drupal::request();
    if (is_null($request->get('EXPOSED_FILTER_FIELD_MACHINE_NAME'))) {
      // get all input fields first then modify the one you want to change
      $input = $form_state->getUserInput();
      $input['EXPOSED_FILTER_FIELD_MACHINE_NAME'] = 'DEFAULT_VALUE;
      $form_state->setUserInput($input);
    }
  }
}

for instance, here I set the current node as the default category referenced

<?php
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function unsw_dev_form_alter(&$form, FormStateInterface &$form_state, $form_id) {

  // set default category
  if ($form_id == 'views_exposed_form') {
    $request = \Drupal::request();
    if (is_null($request->get('field_cause_category_target_id'))) {
      $node = \Drupal::routeMatch()->getParameter('node');
      if ($node instanceof \Drupal\node\NodeInterface && $node->getType() == 'cause') {
        $form_state->setUserInput(['field_cause_category_target_id' => $node->id()]);
      }
    }
  }
}
1
  • 1
    add if ($form_id == 'views_exposed_form' && isset($form['field_cause_category_target_id'] to protect rest of exposed filters in your website
    – Augusto
    Apr 22, 2021 at 13:05
3

You can use hook_form_alter() to alter the any drupal form. example :

function yourmodule_name_form_alter(&$form,&$form_state,$form_id){
  if($form_id == 'views_exposed_form'){
  $form['field_width_value']['#default_value'] = 'whatever you want';
  unset($form['field_width_value']['#options']['All']);

 } 
}
10
  • I am getting: [field_year_value] => Array ( [#type] => select [#default_value] => All [#options] => Array ( [All] => - Any - [1] => 2011 [2] => 2013 [3] => 2015 ) [#bef_description] => 2015 [#description] => 2015 ) but this is not changing my default value
    – jsh
    Apr 15, 2015 at 7:28
  • $form['field_year_value']['#default_value'] = '2015'; I am using this .. but not working for me
    – jsh
    Apr 15, 2015 at 7:30
  • you should use key of the value([3] => 2015). example $form['field_year_value']['#default_value'] = 3; Apr 15, 2015 at 7:35
  • its working but only one time.. when i cleared cache.. If I select other year and then refresh page..then doesn't show default value.
    – jsh
    Apr 15, 2015 at 7:43
  • Have you set remember option in views filter option? Because my side this is working fine Apr 15, 2015 at 8:07
2

My case scenario for a taxonomy field and filtered results, not just default value:

if ($form_id == 'views_exposed_form') {
  $account = user_load($user->uid);
  $country = taxonomy_term_load($account->field_country[LANGUAGE_NONE][0]['tid']);
  $form['country']['#options'] = array($country->tid => $country->name);
  $form['country']['#default_value'] = $country->tid;
  $form_state['input']['country'] = $country->tid;
}
1
  • the problem is that when you change the value and submit the form it sets it back to the default value Nov 19, 2019 at 10:42
1

where are you going to set default value of current year? If you want to remove -any- from options, make it required. or use Better Expose Filter to override the -any-.

I'm not sure if this post is your duplicate but I'm going to put my answer here also.

An expose filter but by default is the current year.

This is what you're going to do.

  • Add your date field to your filter criterion
  • Date selection form element: SELECT
  • Filter granularity: YEAR
  • Expose it
  • Operator: Is equal to
  • Choose "Enter Relative Date"
  • Type this: now

First Part:

enter image description here

Second Setting: enter image description here

Expose Filter with Default Value

Expose Filter with default

Is this what you want?

2
  • I want to remove -any- from options. And my filter like this:year[2014][2015][2016] something like this. I need to set by defualt value current year .
    – jsh
    Apr 15, 2015 at 7:05
  • I edited my answer, please have a look
    – Danz
    Apr 16, 2015 at 1:09
0

Having also this need I just found this module: Views Advanced Labels and successfully used it, replacing the - Any - by a custom simple text for 2 exposed filters in the same view.

It says that it works with tokens. So I guess using [date:custom:Y] or maybe creating a date format could be an option.

Some more resources here: Auto update to the current year in the copyright footer (© 2010-2013)

-2

This is my solution. Working for Drupal7

function **THEME_NAME**_form_alter(&$form,&$form_state,$form_id){
    if($form_id == 'views_exposed_form'){
      $form['**FIELD_ID**']['#options']['All'] = '**YOUR TEXT**';
    }
}
1
  • 1
    Implementing a hook_form_alter is discouraged. Hooks implemented on any theme should be theme-related, any other feature should be covered by modules.
    – d70rr3s
    Jan 16, 2020 at 14:20
-2
function yourmodule_name_form_alter(&$form,&$form_state,$form_id){
  if($form_id == 'views_exposed_form'){
  $form['field_width_value']['#default_value'] = 'whatever you want';
  unset($form['field_width_value']['#options']['All']);
 } 
}
1
  • This is a duplicate from older answers
    – d70rr3s
    Jan 16, 2020 at 14:21

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.