0

I've tried to use solution from Can I prepopulate an exposed filter text field? but failed. Cache clearing does not help.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "views_exposed_form" && $form['#id'] == "views-exposed-form-test-populate-page" ) {
    $form['title']['#default_value'] = 'test title';
    dpm($form);
  }
}

enter image description here

0

Dimetry. Since it is a drupal view you can add the default value in views itself. Please open your view and in the Filter Criteria section select the field you want to add the default value. It opens up a popup in that you can able to see "Value" field. Please give your default value there.

Please see the below picture for your reference. enter image description here

  • Thanks for answer. But I need more complex variant other than just fixed value. And I need to understand how to assign default value to the exposed filter by PHP code. – Dimetry Feb 27 '17 at 9:30
  • Try using views_pre_build to set the default value by php code. – user15837 Feb 27 '17 at 9:35
  • I've tried this solution without quick success. Well. I'll try to dig it more carefully. – Dimetry Feb 27 '17 at 9:43
0

I figured it out as below with hook_views_pre_build.

function module_views_pre_build(&$view, &$display_id, &$args) {
    if ($view->name == 'VIEW_NAME') {
        $view->display['page_1']->handler->handlers['filter']['title']->value = 'Test title';
    }
}

Replace 'page_1' with the correct display_id.

  • Well. It works. But it is too complex for me to understand. I hope there is more simple solution. – Dimetry Feb 27 '17 at 11:06

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.