Beware of Drupal's Radios FAPI empty handling
Did you ever switch a Drupal Form API element from "select" to "radios" and expect it would behave equally? You're wrong!
Here's (sadly) why...
In our drowl_paragraphs module we wanted to switch a setting from "select" to "radios" to allow style preview:
- $style_boxstyle_options = [];
- // [...]
- $element['style']['style_boxstyle']['style_boxstyle'] = [
- '#type' => 'select',
- '#title' => $this->t('Box style'),
- '#options' => $style_boxstyle_options,
- '#empty_option' => $this->t('- None -'),
- '#description' => $this->t('Predefined styling of this container.'),
- ];
What I expected was, that it's a simple switch and both would behave indentially regarding their empty value.
But while a "select" automatically adds a "None" option if
- '#required' => FALSE
to make it de-selectable, a radio doesn't add such an option. It even doesn't allow for
- '#empty_option' => $this->t('- None -')
- // or
- '#empty_value' => NULL
or similar.
It simply has all radios unchecked by default and if you've checked a radio, you'll be lost for the future.
So the result of my research which lead to to some interesting information here: https://www.drupal.org/project/drupal/issues/1381140 is:
If you switch the Drupal Form Element "select" to "radios" you have to add the empty option yourself and it seems best to use an empty string as key and '#default_value':
- $style_boxstyle_options = [
- // Add none option ("radios" FAPI doesn't provide that)
- '' => $this->t('- None -')
- ];
- // [...]
- $element['style']['style_boxstyle']['style_boxstyle'] = [
- '#type' => 'radios',
- '#title' => $this->t('Box style'),
- '#options' => $style_boxstyle_options,
- '#required' => FALSE,
- '#description' => $this->t('Predefined styling of this container.'),
- ];
Julian Pustkuchen, M. Sc. Wirtschaftsinformatik ist passionierter Software- & Webentwickler mit den Schwerpunkten Softwaredesign, Objektorientierung sowie Usability- & Ablaufoptimierung bei webks. Ein weiterer Schwerpunkt ist die Entwicklung im CMS Drupal.
Entwickelt Julian gerade keine Software, fährt er Ski, Mountainbike, spielt Badminton, segelt oder verbringt Zeit mit Freunden.
Dieser Blog dient als sein "öffentlicher Notizblo(ck/g)".
Euch gefällt mein Blog?
Vielleicht hilft mir auch ein Buch, euch zukünftig (noch) bessere Artikel zu bieten? (Amazon Wunschliste)
Gerne bewerte ich auch ab und zu einmal unabhängig ein Produkt.
Ich freue mich über jede Art der Unterstützung in Form von Kommentaren, Lob oder Kritik.
Auf zum Kontaktformular!
Zu meinem Google+ Profil
Webbasierte Lösungen? Machen wir!
webks: websolutions kept simple
http://www.webks.de
Tags in Blogeintrag - Kategorie
Dir hat dieser Beitrag viel Zeit und Nerven gespart?
Dann freue ich mich über jede kleine Anerkennung:
Oder du suchst weitere professionelle Unterstützung?
Dann bietet dir unser webks.de Softwareentwickler-Team professionelle Dienstleistungen im Web-Development und der Drupal CMS Entwicklung!
Kommentare
Kommentar hinzufügen