From 185c50b124e6f3c22ff72422d07d6f5f64ea7c2f Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Thu, 26 Dec 2013 15:05:32 +0000 Subject: [PATCH] Issue #2050113 by swentel, longwave, Hydra, yched, marthinal, netsensei: PHP notice on multiple items image field. --- .../file/Plugin/Field/FieldWidget/FileWidget.php | 5 +++++ .../Plugin/Field/FieldWidget/ImageWidget.php | 3 ++- .../Drupal/image/Tests/ImageFieldDisplayTest.php | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldWidget/FileWidget.php b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldWidget/FileWidget.php index 948a06d9a22e..a7c8d3028f8d 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldWidget/FileWidget.php +++ b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldWidget/FileWidget.php @@ -152,6 +152,11 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f $elements['#field_name'] = $element['#field_name']; $elements['#language'] = $element['#language']; $elements['#display_field'] = (bool) $this->getFieldSetting('display_field'); + // The field settings include defaults for the field type. However, this + // widget is a base class for other widgets (e.g., ImageWidget) that may + // act on field types without these expected settings. + $field_settings = $this->getFieldSettings() + array('display_field' => NULL); + $elements['#display_field'] = (bool) $field_settings['display_field']; // Add some properties that will eventually be added to the file upload // field. These are added here so that they may be referenced easily diff --git a/core/modules/image/lib/Drupal/image/Plugin/Field/FieldWidget/ImageWidget.php b/core/modules/image/lib/Drupal/image/Plugin/Field/FieldWidget/ImageWidget.php index c3afdbb8d0e6..0ecca824c3b4 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/Field/FieldWidget/ImageWidget.php +++ b/core/modules/image/lib/Drupal/image/Plugin/Field/FieldWidget/ImageWidget.php @@ -81,6 +81,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f $cardinality = $this->fieldDefinition->getCardinality(); $file_upload_help = array( '#theme' => 'file_upload_help', + '#description' => '', '#upload_validators' => $elements[0]['#upload_validators'], '#cardinality' => $cardinality, ); @@ -92,7 +93,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f } } else { - $elements['#file_upload_description'] = drupal_render($file_upload_help); + $elements['#file_upload_description'] = $file_upload_help; } return $elements; diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php index 0071179c235e..aa54d8976891 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php @@ -7,6 +7,8 @@ namespace Drupal\image\Tests; +use Drupal\Core\Field\FieldDefinitionInterface; + /** * Test class to check that formatters and display settings are working. */ @@ -221,6 +223,19 @@ function testImageFieldSettings() { '%max' => $schema['columns']['title']['length'], '%length' => $test_size, ))); + + // Set cardinality to unlimited and add upload a second image. + // The image widget is extending on the file widget, but the image field + // type does not have the 'display_field' setting which is expected by + // the file widget. This resulted in notices before when cardinality is not + // 1, so we need to make sure the file widget prevents these notices by + // providing all settings, even if they are not used. + // @see FileWidget::formMultipleElements(). + $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/field', array('field[cardinality]' => FieldDefinitionInterface::CARDINALITY_UNLIMITED), t('Save field settings')); + $edit = array(); + $edit['files[' . $field_name . '_1][]'] = drupal_realpath($test_image->uri); + $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published')); + $this->assertText(format_string('Article @title has been updated.', array('@title' => $node->getTitle()))); } /** -- GitLab