Skip to content
Snippets Groups Projects
Commit 80ba1c1b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2307647 by Wim Leers | anandps: [Follow-up] Allow manual override of...

Issue #2307647 by Wim Leers | anandps: [Follow-up] Allow manual override of required image alt text in the Text Editor image dialog when appropriate.
parent 2212b4b3
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -95,11 +95,23 @@ public function buildForm(array $form, FormStateInterface $form_state, FilterFor
$form['fid']['#required'] = FALSE;
}
// The alt attribute is *required*, but we allow users to opt-in to empty
// alt attributes for the very rare edge cases where that is valid by
// specifying two double quotes as the alternative text in the dialog.
// However, that *is* stored as an empty alt attribute, so if we're editing
// an existing image (which means the src attribute is set) and its alt
// attribute is empty, then we show that as two double quotes in the dialog.
// @see https://www.drupal.org/node/2307647
$alt = isset($image_element['alt']) ? $image_element['alt'] : '';
if ($alt === '' && !empty($image_element['src'])) {
$alt = '""';
}
$form['attributes']['alt'] = array(
'#title' => $this->t('Alternative text'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => isset($image_element['alt']) ? $image_element['alt'] : '',
'#required_error' => $this->t('Alternative text is required.<br><em>(Only in rare cases should this be left empty. To create empty alternative text, enter <code>""</code> — two double quotes without any content).'),
'#default_value' => $alt,
'#maxlength' => 2048,
);
$form['dimensions'] = array(
......@@ -201,6 +213,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setValue(array('attributes', 'data-editor-file-uuid'), $file->uuid());
}
// When the alt attribute is set to two double quotes, transform it to the
// empty string: two double quotes signify "empty alt attribute". See above.
if (trim($form_state->getValue(array('attributes', 'alt'))) === '""') {
$form_state->setValue(array('attributes', 'alt'), '');
}
if (form_get_errors($form_state)) {
unset($form['#prefix'], $form['#suffix']);
$status_messages = array('#theme' => 'status_messages');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment