0

I'm writing a module to change input HTML attributes with the next code:

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function custom_numeric_filter_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $field = 'yourfieldname';
  if ($form_id == 'views_exposed_form' && array_key_exists($field, $form)) {
    $form[$field]['#type'] = "range";
    $form[$field]['#step'] = 5;
    $form[$field]['#size'] = 5;
    $form[$field]['#min'] = 10;
    $form[$field]['#max'] = 60;
  }
}

The problem is that the type gets overwritten back to number by something else.

I have Better Exposed Filters installed, maybe it gets rewritten there but why? And how can I make sure my module code has priority? With the help from NoSssweat I made sure it was executed after BEF and it didn't help.

6
  • 1
    How can I make sure my module code has priority? By giving your module higher weight so your hook executes last. You can achieve that like this or with this
    – No Sssweat
    Nov 13, 2020 at 12:56
  • @NoSssweat thanks, is there a way to see all these weights for installed modules?
    – int_ua
    Nov 13, 2020 at 13:03
  • 1
    yes, with this.
    – No Sssweat
    Nov 13, 2020 at 13:05
  • It was after BEF but I still changed the weight, cleared the cache and it didn't help unfortunately. Maybe it's something else. Like range being an unknown value for some reason.
    – int_ua
    Nov 13, 2020 at 13:12
  • Try setting type to BETWEEN, just a guess.
    – No Sssweat
    Nov 13, 2020 at 14:48

1 Answer 1

1

Try in this way:

function seven_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (in_array($form_id, ['node_page_form', 'node_page_edit_form'])) {
    $form['field_number']['widget'][0]['value']['#type'] = 'range';
  }
}

I think this post will help you, check once - https://www.drupal.org/project/drupal/issues/2958499#comment-12558959

4
  • Undefined index: widget error, sadly
    – int_ua
    Nov 15, 2020 at 16:04
  • This didn't solve the problem but the link is useful, thank you.
    – int_ua
    Nov 16, 2020 at 8:55
  • Okay, that's great that link was helpful to you, what you changed in your alter code ? After that error ?
    – Lokesh1024
    Nov 16, 2020 at 9:03
  • Not yet, I've just reverted it to a previous state for now.
    – int_ua
    Nov 16, 2020 at 9:21

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.