Commit 80ba1c1b authored by alexpott's avatar alexpott

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
......@@ -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');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment