3

I am using hook_form_alter to hide a field in a view exposed filter.

function cars_form_alter(&$form, &$form_state, $form_id) { 
if($form_id == "views_exposed_form"){
    $form['field_price__1']['#type'] = 'hidden';
  }
}

My questions are :

  1. It only works if use $form_id == "views_exposed_form". But I don't want for all views only for my specific one which has form_id=views_exposed_form_car-results_panel_pane_3 ,but it doesn't work if I use the specific form id

  2. Using the above snippet,I can hide the field but not its Label.How can I hide also the label?

2
  • Hello. Could you please post one question per question, and format title in more meaningful way?
    – Mołot
    Oct 28, 2013 at 9:42
  • okay,sorry for this
    – Dion
    Oct 28, 2013 at 9:45

1 Answer 1

6

One possible way to alter the exposed filter for specific view is the following:

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
        // Check if the form contains exposed filter form element.
    if ($form_id == 'views_exposed_form') {
        // Here you can define the check for specific view so that it doesnt apply to all views.
        $view = $form_state['view'];
        // If you have multiple display in view than you can add a check for specific display
        // as '$view->current_display == 'DISPLAY_NAME' in the below if condition with '&&'.
        if ($view->name === 'MY_VIEW_MACHINE_NAME') {
            // Add your code for specific form.
        }
    }
}

And for hiding the label of particular element, check #title_display: Indicates how the label should be rendered. The label includes the #title and the required marker, if #required. If the #title is empty but the field is #required, the label will contain only the required marker.

'#title_display' => 'invisible'


UPDATE:

Also you may try using hook_form_views_exposed_form_alter as below:

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
  // To check the form id of your view.
  print_r($form_id);
}
11
  • you can also usee this function hook_views_exposed_form_alter(&$form, &$form_state, $form_id); and $form_id should match your view name. If you have devel installed you can use the function dpm($form_id); and it will put the contents in the message part of the page. Oct 28, 2013 at 10:04
  • @DanielHarper Thanks for mentioning that, that is also one of the possible way!! Oct 28, 2013 at 10:18
  • Here is one example: torontowebsitedeveloper.com/drupal-tips-tricks/… Oct 28, 2013 at 10:20
  • I can hide the field but not its label.I am using this snippet:What am I missing? $form['field_price__1']= array( '#type' => 'hidden', '#title_display' => 'invisible', );
    – Dion
    Oct 28, 2013 at 10:45
  • @sokratis have you checked it after clearing the cache. #title is rendered as a label element before the form element in the page markup, and is made invisible with the Drupal 7 .element-invisible CSS class. This makes #title remain available to screen-reader users, but hides it from being displayed visually in the browser. Oct 28, 2013 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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