Skip to content
Snippets Groups Projects

Issue #3037446: Forms with required fields marked by asterisk do not have text explaining what the asterisk means

Open Issue #3037446: Forms with required fields marked by asterisk do not have text explaining what the asterisk means
3 unresolved threads
3 unresolved threads
Files
4
@@ -18,6 +18,7 @@
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\HttpFoundation\FileBag;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -277,6 +278,7 @@ public function buildForm($form_arg, FormStateInterface &$form_state) {
}
$form = $this->retrieveForm($form_id, $form_state);
$this->addAsteriskExplanation($form_id, $form);
$this->prepareForm($form_id, $form, $form_state);
// self::setCache() removes uncacheable $form_state keys (see properties
@@ -638,6 +640,68 @@ public function processForm($form_id, &$form, FormStateInterface &$form_state) {
}
}
/**
* {@inheritdoc}
*/
public function addAsteriskExplanation(string $form_id, array &$form): void {
$form_note_identifier = $form_id . '_required_fields_note';
foreach ($form as $form_key => $form_item) {
if (str_starts_with($form_key, '#')) {
// We'll skip over the special fields.
continue;
}
else {
// Check if type is primitive.
if (isset($form_item['#type']) && $this->isComplexElement($form_item['#type'])) {
$this->addAsteriskExplanation($form_id, $form_item);
if (isset($form_item[$form_note_identifier])) {
$form[$form_note_identifier] = $form_item[$form_note_identifier];
unset($form_item[$form_note_identifier]);
return;
}
}
else {
if (isset($form_item['#required']) && (bool) $form_item['#required'] === TRUE) {
$form[$form_note_identifier] = [
'#type' => 'container',
'#markup' => new TranslatableMarkup('<strong>@strong-markup: </strong><label>@label-markup *.</label>', [
'@strong-markup' => new TranslatableMarkup('Note'),
'@label-markup' => new TranslatableMarkup('Required fields are marked with an asterisk'),
]),
'#weight' => -1000,
];
return;
}
}
}
}
}
/**
* Checks to see if a specified type is used for grouping elements.
*
* @param string $type
* String representation of the type.
*
* @return bool
* Is the type in the array?
*/
public function isComplexElement($type) {
Please register or sign in to reply
return in_array($type, [
'actions',
'container',
'details',
'dropbutton',
'fieldgroup',
'fieldset',
'form',
'operations',
]);
}
/**
* Renders a form action URL. It's a #lazy_builder callback.
*
Loading