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.
6 Answers
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()]);
}
}
}
}
-
1add
if ($form_id == 'views_exposed_form' && isset($form['field_cause_category_target_id']
to protect rest of exposed filters in your website– AugustoApr 22, 2021 at 13:05 -
1
-
1
You can use hook_form_alter()
to alter any form.
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']);
}
}
-
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– jshApr 15, 2015 at 7:28
-
$form['field_year_value']['#default_value'] = '2015'; I am using this .. but not working for me– jshApr 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.– jshApr 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
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;
}
-
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
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:
Second Setting:
Expose Filter with Default Value
Is this what you want?
-
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 .– jshApr 15, 2015 at 7:05
-
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)
This is my solution; it works for Drupal 7 too.
function my_theme_form_alter(&$form,&$form_state,$form_id) {
if ($form_id == 'views_exposed_form') {
$form['**FIELD_ID**']['#options']['All'] = '**YOUR TEXT**';
}
}
-
1Implementing a
hook_form_alter
is discouraged. Hooks implemented on any theme should be theme-related, any other feature should be covered by modules.– d70rr3sJan 16, 2020 at 14:20