3 namespace Drupal\image\Plugin\Field\FieldType;
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
11 use Drupal\Core\TypedData\DataDefinition;
12 use Drupal\file\Entity\File;
13 use Drupal\file\Plugin\Field\FieldType\FileItem;
16 * Plugin implementation of the 'image' field type.
20 * label = @Translation("Image"),
21 * description = @Translation("This field stores the ID of an image file as an integer value."),
22 * category = @Translation("Reference"),
23 * default_widget = "image_image",
24 * default_formatter = "image",
27 * "label" = @Translation("File"),
29 * "target_id", "width", "height"
31 * "require_all_groups_for_translation" = TRUE
34 * "label" = @Translation("Alt"),
35 * "translatable" = TRUE
38 * "label" = @Translation("Title"),
39 * "translatable" = TRUE
42 * list_class = "\Drupal\file\Plugin\Field\FieldType\FileFieldItemList",
43 * constraints = {"ReferenceAccess" = {}, "FileValidation" = {}}
46 class ImageItem extends FileItem {
51 * @var \Drupal\Core\Entity\EntityManagerInterface
53 protected $entityManager;
58 public static function defaultStorageSettings() {
67 ] + parent::defaultStorageSettings();
73 public static function defaultFieldSettings() {
75 'file_extensions' => 'png gif jpg jpeg',
77 'alt_field_required' => 1,
79 'title_field_required' => 0,
80 'max_resolution' => '',
81 'min_resolution' => '',
89 ] + parent::defaultFieldSettings();
91 unset($settings['description_field']);
98 public static function schema(FieldStorageDefinitionInterface $field_definition) {
102 'description' => 'The ID of the file entity.',
107 'description' => "Alternative image text, for the image's 'alt' attribute.",
112 'description' => "Image title text, for the image's 'title' attribute.",
117 'description' => 'The width of the image in pixels.',
122 'description' => 'The height of the image in pixels.',
128 'target_id' => ['target_id'],
132 'table' => 'file_managed',
133 'columns' => ['target_id' => 'fid'],
142 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
143 $properties = parent::propertyDefinitions($field_definition);
145 unset($properties['display']);
146 unset($properties['description']);
148 $properties['alt'] = DataDefinition::create('string')
149 ->setLabel(t('Alternative text'))
150 ->setDescription(t("Alternative image text, for the image's 'alt' attribute."));
152 $properties['title'] = DataDefinition::create('string')
153 ->setLabel(t('Title'))
154 ->setDescription(t("Image title text, for the image's 'title' attribute."));
156 $properties['width'] = DataDefinition::create('integer')
157 ->setLabel(t('Width'))
158 ->setDescription(t('The width of the image in pixels.'));
160 $properties['height'] = DataDefinition::create('integer')
161 ->setLabel(t('Height'))
162 ->setDescription(t('The height of the image in pixels.'));
170 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
173 // We need the field-level 'default_image' setting, and $this->getSettings()
174 // will only provide the instance-level one, so we need to explicitly fetch
176 $settings = $this->getFieldDefinition()->getFieldStorageDefinition()->getSettings();
178 $scheme_options = \Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::WRITE_VISIBLE);
179 $element['uri_scheme'] = [
181 '#title' => t('Upload destination'),
182 '#options' => $scheme_options,
183 '#default_value' => $settings['uri_scheme'],
184 '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'),
187 // Add default_image element.
188 static::defaultImageForm($element, $settings);
189 $element['default_image']['#description'] = t('If no image is uploaded, this image will be shown on display.');
197 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
198 // Get base form from FileItem.
199 $element = parent::fieldSettingsForm($form, $form_state);
201 $settings = $this->getSettings();
203 // Add maximum and minimum resolution settings.
204 $max_resolution = explode('x', $settings['max_resolution']) + ['', ''];
205 $element['max_resolution'] = [
207 '#title' => t('Maximum image resolution'),
208 '#element_validate' => [[get_class($this), 'validateResolution']],
210 '#field_prefix' => '<div class="container-inline">',
211 '#field_suffix' => '</div>',
212 '#description' => t('The maximum allowed image size expressed as WIDTH×HEIGHT (e.g. 640×480). Leave blank for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height. Resizing images on upload will cause the loss of <a href="http://wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image.'),
214 $element['max_resolution']['x'] = [
216 '#title' => t('Maximum width'),
217 '#title_display' => 'invisible',
218 '#default_value' => $max_resolution[0],
220 '#field_suffix' => ' × ',
222 $element['max_resolution']['y'] = [
224 '#title' => t('Maximum height'),
225 '#title_display' => 'invisible',
226 '#default_value' => $max_resolution[1],
228 '#field_suffix' => ' ' . t('pixels'),
231 $min_resolution = explode('x', $settings['min_resolution']) + ['', ''];
232 $element['min_resolution'] = [
234 '#title' => t('Minimum image resolution'),
235 '#element_validate' => [[get_class($this), 'validateResolution']],
237 '#field_prefix' => '<div class="container-inline">',
238 '#field_suffix' => '</div>',
239 '#description' => t('The minimum allowed image size expressed as WIDTH×HEIGHT (e.g. 640×480). Leave blank for no restriction. If a smaller image is uploaded, it will be rejected.'),
241 $element['min_resolution']['x'] = [
243 '#title' => t('Minimum width'),
244 '#title_display' => 'invisible',
245 '#default_value' => $min_resolution[0],
247 '#field_suffix' => ' × ',
249 $element['min_resolution']['y'] = [
251 '#title' => t('Minimum height'),
252 '#title_display' => 'invisible',
253 '#default_value' => $min_resolution[1],
255 '#field_suffix' => ' ' . t('pixels'),
258 // Remove the description option.
259 unset($element['description_field']);
261 // Add title and alt configuration options.
262 $element['alt_field'] = [
263 '#type' => 'checkbox',
264 '#title' => t('Enable <em>Alt</em> field'),
265 '#default_value' => $settings['alt_field'],
266 '#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.'),
269 $element['alt_field_required'] = [
270 '#type' => 'checkbox',
271 '#title' => t('<em>Alt</em> field required'),
272 '#default_value' => $settings['alt_field_required'],
273 '#description' => t('Making this field required is recommended.'),
277 ':input[name="settings[alt_field]"]' => ['checked' => TRUE],
281 $element['title_field'] = [
282 '#type' => 'checkbox',
283 '#title' => t('Enable <em>Title</em> field'),
284 '#default_value' => $settings['title_field'],
285 '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image. Enabling this field is not recommended as it can cause problems with screen readers.'),
288 $element['title_field_required'] = [
289 '#type' => 'checkbox',
290 '#title' => t('<em>Title</em> field required'),
291 '#default_value' => $settings['title_field_required'],
295 ':input[name="settings[title_field]"]' => ['checked' => TRUE],
300 // Add default_image element.
301 static::defaultImageForm($element, $settings);
302 $element['default_image']['#description'] = t("If no image is uploaded, this image will be shown on display and will override the field's default image.");
310 public function preSave() {
313 $width = $this->width;
314 $height = $this->height;
316 // Determine the dimensions if necessary.
317 if ($this->entity && $this->entity instanceof EntityInterface) {
318 if (empty($width) || empty($height)) {
319 $image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
320 if ($image->isValid()) {
321 $this->width = $image->getWidth();
322 $this->height = $image->getHeight();
327 trigger_error(sprintf("Missing file with ID %s.", $this->target_id), E_USER_WARNING);
334 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
335 $random = new Random();
336 $settings = $field_definition->getSettings();
339 $min_resolution = empty($settings['min_resolution']) ? '100x100' : $settings['min_resolution'];
340 $max_resolution = empty($settings['max_resolution']) ? '600x600' : $settings['max_resolution'];
341 $extensions = array_intersect(explode(' ', $settings['file_extensions']), ['png', 'gif', 'jpg', 'jpeg']);
342 $extension = array_rand(array_combine($extensions, $extensions));
343 // Generate a max of 5 different images.
344 if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= 5) {
345 $tmp_file = drupal_tempnam('temporary://', 'generateImage_');
346 $destination = $tmp_file . '.' . $extension;
347 file_unmanaged_move($tmp_file, $destination);
348 if ($path = $random->image(\Drupal::service('file_system')->realpath($destination), $min_resolution, $max_resolution)) {
349 $image = File::create();
350 $image->setFileUri($path);
351 $image->setOwnerId(\Drupal::currentUser()->id());
352 $image->setMimeType(\Drupal::service('file.mime_type.guesser')->guess($path));
353 $image->setFileName(drupal_basename($path));
354 $destination_dir = static::doGetUploadLocation($settings);
355 file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
356 $destination = $destination_dir . '/' . basename($path);
357 $file = file_move($image, $destination);
358 $images[$extension][$min_resolution][$max_resolution][$file->id()] = $file;
365 // Select one of the images we've already generated for this field.
366 $image_index = array_rand($images[$extension][$min_resolution][$max_resolution]);
367 $file = $images[$extension][$min_resolution][$max_resolution][$image_index];
370 list($width, $height) = getimagesize($file->getFileUri());
372 'target_id' => $file->id(),
373 'alt' => $random->sentences(4),
374 'title' => $random->sentences(4),
382 * Element validate function for resolution fields.
384 public static function validateResolution($element, FormStateInterface $form_state) {
385 if (!empty($element['x']['#value']) || !empty($element['y']['#value'])) {
386 foreach (['x', 'y'] as $dimension) {
387 if (!$element[$dimension]['#value']) {
388 // We expect the field name placeholder value to be wrapped in t()
389 // here, so it won't be escaped again as it's already marked safe.
390 $form_state->setError($element[$dimension], t('Both a height and width value must be specified in the @name field.', ['@name' => $element['#title']]));
394 $form_state->setValueForElement($element, $element['x']['#value'] . 'x' . $element['y']['#value']);
397 $form_state->setValueForElement($element, '');
402 * Builds the default_image details element.
404 * @param array $element
405 * The form associative array passed by reference.
406 * @param array $settings
407 * The field settings array.
409 protected function defaultImageForm(array &$element, array $settings) {
410 $element['default_image'] = [
411 '#type' => 'details',
412 '#title' => t('Default image'),
415 // Convert the stored UUID to a FID.
417 $uuid = $settings['default_image']['uuid'];
418 if ($uuid && ($file = $this->getEntityManager()->loadEntityByUuid('file', $uuid))) {
419 $fids[0] = $file->id();
421 $element['default_image']['uuid'] = [
422 '#type' => 'managed_file',
423 '#title' => t('Image'),
424 '#description' => t('Image to be shown if no image is uploaded.'),
425 '#default_value' => $fids,
426 '#upload_location' => $settings['uri_scheme'] . '://default_images/',
427 '#element_validate' => [
428 '\Drupal\file\Element\ManagedFile::validateManagedFile',
429 [get_class($this), 'validateDefaultImageForm'],
431 '#upload_validators' => $this->getUploadValidators(),
433 $element['default_image']['alt'] = [
434 '#type' => 'textfield',
435 '#title' => t('Alternative text'),
436 '#description' => t('This text will be used by screen readers, search engines, and when the image cannot be loaded.'),
437 '#default_value' => $settings['default_image']['alt'],
440 $element['default_image']['title'] = [
441 '#type' => 'textfield',
442 '#title' => t('Title'),
443 '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image.'),
444 '#default_value' => $settings['default_image']['title'],
445 '#maxlength' => 1024,
447 $element['default_image']['width'] = [
449 '#value' => $settings['default_image']['width'],
451 $element['default_image']['height'] = [
453 '#value' => $settings['default_image']['height'],
458 * Validates the managed_file element for the default Image form.
460 * This function ensures the fid is a scalar value and not an array. It is
461 * assigned as a #element_validate callback in
462 * \Drupal\image\Plugin\Field\FieldType\ImageItem::defaultImageForm().
464 * @param array $element
465 * The form element to process.
466 * @param \Drupal\Core\Form\FormStateInterface $form_state
469 public static function validateDefaultImageForm(array &$element, FormStateInterface $form_state) {
470 // Consolidate the array value of this field to a single FID as #extended
471 // for default image is not TRUE and this is a single value.
472 if (isset($element['fids']['#value'][0])) {
473 $value = $element['fids']['#value'][0];
474 // Convert the file ID to a uuid.
475 if ($file = \Drupal::entityManager()->getStorage('file')->load($value)) {
476 $value = $file->uuid();
482 $form_state->setValueForElement($element, $value);
488 public function isDisplayed() {
489 // Image items do not have per-item visibility settings.
494 * Gets the entity manager.
496 * @return \Drupal\Core\Entity\EntityManagerInterface
498 protected function getEntityManager() {
499 if (!isset($this->entityManager)) {
500 $this->entityManager = \Drupal::entityManager();
502 return $this->entityManager;