Issue #2303765 by davidhernandez, larowlan, mgifford: Make the default 'alt' attribute for Image fields required

8.0.x
Alex Pott 2015-03-22 14:12:54 +00:00
parent e5d6c142ff
commit d87af02686
9 changed files with 63 additions and 21 deletions

View File

@ -76,8 +76,8 @@ class ImageItem extends FileItem {
public static function defaultFieldSettings() {
$settings = array(
'file_extensions' => 'png gif jpg jpeg',
'alt_field' => 0,
'alt_field_required' => 0,
'alt_field' => 1,
'alt_field_required' => 1,
'title_field' => 0,
'title_field_required' => 0,
'max_resolution' => '',
@ -263,7 +263,7 @@ class ImageItem extends FileItem {
'#type' => 'checkbox',
'#title' => t('Enable <em>Alt</em> field'),
'#default_value' => $settings['alt_field'],
'#description' => t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded. Enabling this field is recommended'),
'#description' => t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded. Enabling this field is recommended.'),
'#weight' => 9,
);
$element['alt_field_required'] = array(

View File

@ -296,7 +296,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node = Node::load($nid);
// Get node field original image URI.
@ -430,7 +430,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node = Node::load($nid);
// Get node field original image URI.

View File

@ -49,7 +49,8 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
function _testImageFieldFormatters($scheme) {
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$field_name = strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
$field_settings = array('alt_field_required' => 0);
$instance = $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme), $field_settings);
// Go to manage display page.
$this->drupalGet("admin/structure/types/manage/article/display");
@ -78,8 +79,16 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Ensure that preview works.
$this->previewNodeImage($test_image, $field_name, 'article');
// After previewing, make the alt field required. It cannot be required
// during preview because the form validation will fail.
$instance->settings['alt_field_required'] = 1;
$instance->save();
// Create alt text for the image.
$alt = $this->randomMachineName();
// Save node.
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
@ -90,6 +99,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
$default_output = str_replace("\n", NULL, drupal_render($image));
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
@ -108,6 +118,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
$default_output = '<a href="' . file_create_url($image_uri) . '">' . drupal_render($image) . '</a>';
$this->drupalGet('node/' . $nid);
@ -145,12 +156,13 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$elements = $this->xpath(
'//a[@href=:path]/img[@src=:url and @alt="" and @width=:width and @height=:height]',
'//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]',
array(
':path' => $node->url(),
':url' => file_create_url($image['#uri']),
':width' => $image['#width'],
':height' => $image['#height'],
':alt' => $alt,
)
);
$this->assertEqual(count($elements), 1, 'Image linked to content formatter displaying correctly on full node view.');
@ -170,6 +182,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
'#width' => 40,
'#height' => 20,
'#style_name' => 'thumbnail',
'#alt' => $alt,
);
$default_output = drupal_render($image_style);
$this->drupalGet('node/' . $nid);
@ -221,7 +234,11 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// We have to create the article first and then edit it because the alt
// and title fields do not display until the image has been attached.
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
// Create alt text for the image.
$alt = $this->randomMachineName();
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$this->drupalGet('node/' . $nid . '/edit');
$this->assertFieldByName($field_name . '[0][alt]', '', 'Alt field displayed on article form.');
$this->assertFieldByName($field_name . '[0][title]', '', 'Title field displayed on article form.');
@ -243,7 +260,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$image = array(
'#theme' => 'image',
'#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
'#alt' => $this->randomMachineName(),
'#alt' => $alt,
'#title' => $this->randomMachineName(),
'#width' => 40,
'#height' => 20,
@ -281,15 +298,19 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// providing all settings, even if they are not used.
// @see FileWidget::formMultipleElements().
$this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/storage', array('field_storage[cardinality]' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED), t('Save field settings'));
$edit = array();
$edit['files[' . $field_name . '_1][]'] = drupal_realpath($test_image->uri);
$edit = array(
'files[' . $field_name . '_1][]' => drupal_realpath($test_image->uri),
);
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
// Add the required alt text.
$this->drupalPostForm(NULL, [$field_name . '[1][alt]' => $alt], t('Save and keep published'));
$this->assertText(format_string('Article @title has been updated.', array('@title' => $node->getTitle())));
// Assert ImageWidget::process() calls FieldWidget::process().
$this->drupalGet('node/' . $node->id() . '/edit');
$edit = array();
$edit['files[' . $field_name . '_2][]'] = drupal_realpath($test_image->uri);
$edit = array(
'files[' . $field_name . '_2][]' => drupal_realpath($test_image->uri),
);
$this->drupalPostAjaxForm(NULL, $edit, $field_name . '_2_upload_button');
$this->assertNoRaw('<input multiple type="file" id="edit-' . strtr($field_name, '_', '-') . '-2-upload" name="files[' . $field_name . '_2][]" size="22" class="form-file">');
$this->assertRaw('<input multiple type="file" id="edit-' . strtr($field_name, '_', '-') . '-3-upload" name="files[' . $field_name . '_3][]" size="22" class="form-file">');
@ -346,7 +367,11 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Create a node with an image attached and ensure that the default image
// is not displayed.
$nid = $this->uploadNodeImage($images[1], $field_name, 'article');
// Create alt text for the image.
$alt = $this->randomMachineName();
$nid = $this->uploadNodeImage($images[1], $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
$image = array(
@ -354,6 +379,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
'#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
$image_output = str_replace("\n", NULL, drupal_render($image));
$this->drupalGet('node/' . $nid);

View File

@ -129,13 +129,19 @@ abstract class ImageFieldTestBase extends WebTestBase {
* Name of the image field the image should be attached to.
* @param $type
* The type of node to create.
* @param $alt
* The alt text for the image. Use if the field settings require alt text.
*/
function uploadNodeImage($image, $field_name, $type) {
function uploadNodeImage($image, $field_name, $type, $alt = '') {
$edit = array(
'title[0][value]' => $this->randomMachineName(),
);
$edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('node/add/' . $type, $edit, t('Save and publish'));
if ($alt) {
// Add alt text.
$this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save and publish'));
}
// Retrieve ID of the newly created node from the current URL.
$matches = array();

View File

@ -23,6 +23,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
$field_settings = array(
'max_resolution' => $max_resolution . 'x' . $max_resolution,
'min_resolution' => $min_resolution . 'x' . $min_resolution,
'alt_field' => 0,
);
$this->createImageField($field_name, 'article', array(), $field_settings);

View File

@ -66,7 +66,7 @@ class ImageFieldAttributesTest extends ImageFieldTestBase {
$image = current($this->drupalGetTestFiles('image'));
// Save a node with the image.
$nid = $this->uploadNodeImage($image, $this->fieldName, 'article');
$nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName());
$this->node = Node::load($nid);
$this->file = file_load($this->node->{$this->fieldName}->target_id);
}

View File

@ -168,7 +168,11 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
// Create a new node with an image attached. Make sure we use a large image
// so the scale effects of the image styles always have an effect.
$test_image = current($this->drupalGetTestFiles('image', 39325));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
// Create alt text for the image.
$alt = $this->randomMachineName();
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
@ -179,6 +183,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
'#uri' => $image_uri,
'#width' => 360,
'#height' => 240,
'#alt' => $alt,
);
$default_output = str_replace("\n", NULL, drupal_render($image));
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
@ -199,6 +204,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
'#uri' => $image_uri,
'#width' => 360,
'#height' => 240,
'#alt' => $alt,
);
$default_output = '<a href="' . file_create_url($image_uri) . '">' . drupal_render($image) . '</a>';
$this->drupalGet('node/' . $nid);
@ -273,6 +279,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
$image = \Drupal::service('image.factory')->get($image_uri);
$fallback_image = array(
'#theme' => 'image',
'#alt' => $alt,
'#srcset' => array(
array(
'uri' => $large_style->buildUrl($image->getSource()),
@ -329,7 +336,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
$this->createImageField($field_name, 'article', array('uri_scheme' => 'public'));
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node_storage->resetCache(array($nid));
// Use the responsive image formatter linked to file formatter.
@ -366,7 +373,8 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
*/
private function assertResponsiveImageFieldFormattersLink($link_type) {
$field_name = Unicode::strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', array('uri_scheme' => 'public'));
$field_settings = array('alt_field_required' => 0);
$this->createImageField($field_name, 'article', array('uri_scheme' => 'public'), $field_settings);
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));

View File

@ -78,6 +78,7 @@ class TaxonomyImageTest extends TaxonomyTestBase {
$edit['name[0][value]'] = $this->randomMachineName();
$edit['files[field_test_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
$this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
$terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $edit['name[0][value]']));
$term = reset($terms);
$this->assertText(t('Created new term @name.', array('@name' => $term->getName())));

View File

@ -15,7 +15,7 @@ settings:
min_resolution: ''
alt_field: true
title_field: false
alt_field_required: false
alt_field_required: true
title_field_required: false
default_image:
uuid: null