Commit 2249c07e authored by catch's avatar catch

Issue #1014816 by mondrake, Dave Reid, m1n0, leschekfm, jwilson3,...

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)
parent 1708b460
......@@ -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;
......@@ -22,6 +25,36 @@
*/
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}
*/
......@@ -120,10 +153,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$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.
......
......@@ -2,6 +2,8 @@
namespace Drupal\Tests\image\Functional;
use Drupal\field\Entity\FieldConfig;
/**
* Tests the image field widget.
*
......@@ -27,6 +29,22 @@ public function testWidgetElement() {
$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.');
}
}
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