2

First off, I have tried to do my homework before coming here. I have referenced this, and this and I still cannot get my code to work.

I am simply trying to set a default value in a select form field on a Drupal 8 block configuration form. Here are the relevant parts of my code:

$options = array(
  'test' => 'value',
  'random' => 'string',
  'kiwi' => 'pear',
);

$form['menu_selected'] = array(
  '#type' => 'select',
  '#title' => 'Test Title',
  '#description' => 'Test Description',
  '#options' => $options,
  '#default_value' => 'random',
);

The $form then gets returned as per usual in a block build. Everything else works in the form; The options populate, the description and title display. The only thing that doesn't work is the default value. I have tried using keys and values. I even saw a comment that said to use '#value' instead of '#default_value' and still it didn't work. Can someone please tell me what I'm doing wrong? I can provide more code if needed.

  • Your code looks fine and works for me well. Can you try like this '#default_value' => ['random']. – Ajay Reddy Mar 15 '18 at 7:29
  • Your code is fine. Try doing a hard refresh of the page. Or give us more context if there are special circumstances you haven't mentioned. – Jaypan Mar 15 '18 at 10:05
  • Thanks for the suggestions! @Jaypan I cleared cache (server and browser) several times and it still wouldn't work. But what I marked as the answer below did work. I don't know the reasoning, but I am just glad it works now. – AntonioXIII Mar 15 '18 at 14:33
1

Your code is absolutely correct but if it not work then also try one thing as

$options = array(
  'test' => 'value',
  'random' => 'string',
  'kiwi' => 'pear',
);

$default = 'random';

$form['menu_selected'] = array(
  '#type' => 'select',
  '#title' => 'Test Title',
  '#description' => 'Test Description',
  '#options' => $options,
  '#default_value' => $default,
);

I hope it will helpful.

  • That is so strange, but using the variable worked! Don't know why it wouldn't accept a hardcoded value. Regardless, thank you very much! – AntonioXIII Mar 15 '18 at 14:27
5

On Drupal 8 form, I was having an issue with Firefox that was caching the value of a select field, selected by the user before refreshing the page.

It was basically overriding the #default_value of the field.

My solution was to Turn Off Form Autocompletion (https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion).

You can do it at the level on the input if that's an input, but since it was a select in my case I had to do it at the form level :

$form['#attributes']['autocomplete'] = 'off';
  • 1
    I feel like this is closer to being the actual answer. Forms get cached. So if OP was hitting F5 then he'll get the old form. Just click on the address bar and hit Enter to request a new form. – Beebee Jun 13 '18 at 15:11

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.