Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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

share|improve this answer

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
        }
    }
}
share|improve this answer
    
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 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 at 8:28

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(),
    );
  }
}
share|improve this answer

protected by kiamlaluno Nov 9 '15 at 6:25

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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