summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2018-01-24 13:49:48 (GMT)
committerNathaniel Catchpole2018-01-24 13:50:41 (GMT)
commit2249c07ee03fe89569c8c8ad895a1d62dbd1badd (patch)
tree18c13bfc660e91bdd4852e7712fa5c76a6ac5006
parent1708b460b8e430ef319acf5b69307299199b0838 (diff)
Issue #1014816 by mondrake, Dave Reid, m1n0, leschekfm, jwilson3, claudiu.cristea, fietserwin, guybedford: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only)
-rw-r--r--core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php44
-rw-r--r--core/modules/image/tests/src/Functional/ImageFieldWidgetTest.php18
2 files changed, 58 insertions, 4 deletions
diff --git a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php
index e66d590..7b7ce1a 100644
--- a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php
+++ b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php
@@ -2,9 +2,12 @@
namespace Drupal\image\Plugin\Field\FieldWidget;
+use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Image\ImageFactory;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\file\Entity\File;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
use Drupal\image\Entity\ImageStyle;
@@ -23,6 +26,36 @@ use Drupal\image\Entity\ImageStyle;
class ImageWidget extends FileWidget {
/**
+ * The image factory service.
+ *
+ * @var \Drupal\Core\Image\ImageFactory
+ */
+ protected $imageFactory;
+
+ /**
+ * Constructs an ImageWidget object.
+ *
+ * @param string $plugin_id
+ * The plugin_id for the widget.
+ * @param mixed $plugin_definition
+ * The plugin implementation definition.
+ * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
+ * The definition of the field to which the widget is associated.
+ * @param array $settings
+ * The widget settings.
+ * @param array $third_party_settings
+ * Any third party settings.
+ * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
+ * The element info manager service.
+ * @param \Drupal\Core\Image\ImageFactory $image_factory
+ * The image factory service.
+ */
+ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ElementInfoManagerInterface $element_info, ImageFactory $image_factory = NULL) {
+ parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $element_info);
+ $this->imageFactory = $image_factory ?: \Drupal::service('image.factory');
+ }
+
+ /**
* {@inheritdoc}
*/
public static function defaultSettings() {
@@ -120,10 +153,13 @@ class ImageWidget extends FileWidget {
$element['#upload_validators']['file_validate_image_resolution'] = [$field_settings['max_resolution'], $field_settings['min_resolution']];
}
- // If not using custom extension validation, ensure this is an image.
- $supported_extensions = ['png', 'gif', 'jpg', 'jpeg'];
- $extensions = isset($element['#upload_validators']['file_validate_extensions'][0]) ? $element['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
- $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
+ $extensions = $field_settings['file_extensions'];
+ $supported_extensions = $this->imageFactory->getSupportedExtensions();
+
+ // If using custom extension validation, ensure that the extensions are
+ // supported by the current image toolkit. Otherwise, validate against all
+ // toolkit supported extensions.
+ $extensions = !empty($extensions) ? array_intersect(explode(' ', $extensions), $supported_extensions) : $supported_extensions;
$element['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);
// Add mobile device image capture acceptance.
diff --git a/core/modules/image/tests/src/Functional/ImageFieldWidgetTest.php b/core/modules/image/tests/src/Functional/ImageFieldWidgetTest.php
index 7c3b94d..cdec5b9 100644
--- a/core/modules/image/tests/src/Functional/ImageFieldWidgetTest.php
+++ b/core/modules/image/tests/src/Functional/ImageFieldWidgetTest.php
@@ -2,6 +2,8 @@
namespace Drupal\Tests\image\Functional;
+use Drupal\field\Entity\FieldConfig;
+
/**
* Tests the image field widget.
*
@@ -27,6 +29,22 @@ class ImageFieldWidgetTest extends ImageFieldTestBase {
$this->assertNotEqual(0, count($this->xpath('//div[contains(@class, "field--widget-image-image")]')), 'Image field widget found on add/node page', 'Browser');
$this->assertNotEqual(0, count($this->xpath('//input[contains(@accept, "image/*")]')), 'Image field widget limits accepted files.', 'Browser');
$this->assertNoText('Image test on [site:name]');
+
+ // Check for allowed image file extensions - default.
+ $this->assertText('Allowed types: png gif jpg jpeg.');
+
+ // Try adding to the field config an unsupported extension, should not
+ // appear in the allowed types.
+ $field_config = FieldConfig::loadByName('node', 'article', $field_name);
+ $field_config->setSetting('file_extensions', 'png gif jpg jpeg tiff')->save();
+ $this->drupalGet('node/add/article');
+ $this->assertText('Allowed types: png gif jpg jpeg.');
+
+ // Add a supported extension and remove some supported ones, we should see
+ // the intersect of those entered in field config with those supported.
+ $field_config->setSetting('file_extensions', 'png jpe tiff')->save();
+ $this->drupalGet('node/add/article');
+ $this->assertText('Allowed types: png jpe.');
}
}