0

I am trying to add a disable attribute in a field using the hook_form_id_alter function. I created a module and built the function

function disableIncentiveform_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-drive-clean-incentive-search-page-1'){
    $form['field_new_used']['#attributes']['disabled'] = 'disabled';
  }
}

However, this does not add an attribute to the form.

I know there used to be a hook_form_views_exposed_form_alter function in D7 but I can't find anything specific for D8. Is there a correct way to do this?

  • Have you also tried it with just hook_form_alter? – leymannx Jun 25 at 6:39
  • I don't know if it matters, but I thought module names have to be all lower case: disableincentiveform_form_views_exposed_form_alter() – Hudri Jun 25 at 6:45
0

This code should do it:

function MYMODULE_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $view_ids = ['YOUR_VIEW_ID'];
  $view = $form_state->getStorage('view');
  if ($form_id == 'views_exposed_form' && in_array($view['view']->id(), $view_ids)) {
    $form['field_new_used']['#attributes']['disabled'] = 'disabled';
  }
}

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.