12

I'm having some trouble using hook_form_alter() with Views 3 and Drupal 7.

I'm unable to add an if statement to uniquely identify a view; whatever I put in, the if statement is never found to be true. However, I can easily use the form_id using the following so I'm not sure where I am going wrong!

if ($form_id == 'views_exposed_form') {
}

How do I use hook_form_alter() to uniquely alter a single view of mine?

25

You can get access to the view object within hook_form_alter by accessing $form_state['view']. That allows you to identify a specific view:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'views_exposed_form') {
        $view = $form_state['view'];
        if ($view->name == 'my_view_machine_name' && $view->current_display == 'display_name') {
            // alter your exposed form here
        }
    }
}
  • this is a better solution since $form_id is not enough for exposed forms(unlike how other answers state), unfortunetly current_display may not be enough is some situations with complex sets of views on same page – Rotari Radu Jul 29 '15 at 7:57
  • Agreed - this is the more targeted solution. Very useful if you're using multiple views exposed filter forms. – Sage Apr 20 '16 at 1:31
  • Use drupal_set_message($view->name); & drupal_set_message($view->current_display); to get your names. I was slightly confused thinking it was "views_deals", but it was just "deals" in my case. – Collins Jun 8 '16 at 8:28
  • How we can do same thing Drupal 8? – Sharique Nov 20 '18 at 16:36
  • Better to use mymodule_form_views_exposed_form_alter() instead of mymodule_form_alter, you can then skip the check for $form_id. – Berend de Boer Feb 8 '19 at 21:12
18

What i would do is take it a step back and see if you're even registering this hook. Often $form_id's change.

Debugging process:

  • Turn on devel
  • Insert dsm($form_id) inside your hook as you used to with D6
  • Clear your cache

Example code (inside mymodule.module or template.php file) from drupal.org:

<?php
/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'form_views_exposed_form') {
    drupal_set_message("Form ID is : " . $form_id);
  }
}
?>

You'll see a bunch of form id's printed out then select your views exposed form hook and work from there.

Also, with drupal 7 you're able to target specific forms by ID directly.

Example:

<?php
/**
 * Implements hook_form_alter().
 */
function mymodule_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  drupal_set_message("Form ID is : " . $form_id);
}
?>

Note that if you're using multiple exposed forms you can check for a specific view by examining $form['#id'] in the form alter function.

Finally, if that doesn't work i would recommend reviewing this issue queue: How to alter filter in exposed filters form trough form_alter hook

2

recently I've bumped in this issue too, but I'm using feature module instead of custom one. This helped for me to solve the issue.

/**
 * Implements hook_views_api_alter()
 */
function MY_FEATURE_views_api_alter(&$vars) {
  if (isset($vars['MY_FEATURE'])) {
    $vars['MY_FEATURE']['path'] = drupal_get_path('module', 'MY_FEATURE');
  }
}

And then You can use your hook_form_views_exposed_form_alter() :

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function MY_FEATURE_form_views_exposed_form_alter(&$form, &$form_state) {
  if($form['#id'] == 'views-exposed-form-search-products-page') {
    $form['price'] = array(
      '#type'    => 'select',
      '#size'    => NULL,
      '#options' => _getPriceOptions(),
    );
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.