πŸ‡¬πŸ‡§United Kingdom @johan.gant

Belfast, UK
Account created on 8 March 2011, over 13 years ago
  • Development Manager at DeesonΒ 
  • Senior Drupal Developer at TorchboxΒ 
#

Merge Requests

Recent comments

πŸ‡¬πŸ‡§United Kingdom johan.gant Belfast, UK

For what it's worth, I found that I could work around this with something like:

$form['foo'] = [
  '#type' => 'select',
  '#required' => FALSE,
  '#empty_value' => FALSE,
  '#sort_options' => TRUE,
  '#options' => [... list of key/value options ...],
 ];

combined with something like this in a form_alter hook:

$foo_value = \Drupal::request()->query->get('foo');
if (empty($foo_value) {
  $form['foo']['#value'] = 'key_value_from_one_of_the_options',
}

Which essentially means if there's no query string value for the filter, it sets a default value.

Wouldn't it be a lot handier to just set a #default_value attribute on the render element and internalise any other complications around that to the Select render element class? I appreciate I might not fully understand the existing reasons so would be more than happy if someone who can explain it in more simple terms could do that and enlighten me.

πŸ‡¬πŸ‡§United Kingdom johan.gant Belfast, UK

I've been scratching my head about this for a while today. I have a views exposed form element as a select element, with about 10 options and should only take one value. I can use #value to pre-populate the initial value but this is then fixed and can't be changed by the user. If I try to use #default_value to indicate a starting value/pre-selection it's completely ignored on this element type.

I'm pretty sure I've used #default_value as an attribute in Drupal 7 and 8. The Select.php class docs state:

* - #default_value: Must be NULL or not set in case there is no value for the
 *   element yet, in which case a first default option is inserted by default.
 *   Whether this first option is a valid option depends on whether the field
 *   is #required or not.

If I try to adjust #empty_value the form tells me 'The submitted value is not allowed'.

Entirely probable that I'm applying the wrong combination of properties (somehow) but I'm confused how #default_value can't be applied on this element (string for single value, array for multiple values), when other elements accept this property in a consistent manner.

What can be done to clarify the appropriate way to use the Select element via the class docs, or is this a bug?

πŸ‡¬πŸ‡§United Kingdom johan.gant Belfast, UK

Re-rolled #11 to work against 2.3.x

πŸ‡¬πŸ‡§United Kingdom johan.gant Belfast, UK

@rcodina no problem, attached is an interdiff between #10 and #16. Thanks

πŸ‡¬πŸ‡§United Kingdom johan.gant Belfast, UK

Here's a patch which also changes the domain source field to have a 'field_' prefix, as nodes do.

Production build 0.69.0 2024