ImageItem.php 17.2 KB
Newer Older
1 2
<?php

3
namespace Drupal\image\Plugin\Field\FieldType;
4

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 10
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
13
use Drupal\Core\TypedData\DataDefinition;
14
use Drupal\file\Entity\File;
15
use Drupal\file\Plugin\Field\FieldType\FileItem;
16 17 18 19 20 21 22 23

/**
 * Plugin implementation of the 'image' field type.
 *
 * @FieldType(
 *   id = "image",
 *   label = @Translation("Image"),
 *   description = @Translation("This field stores the ID of an image file as an integer value."),
24
 *   category = @Translation("Reference"),
25 26
 *   default_widget = "image_image",
 *   default_formatter = "image",
27 28 29 30 31
 *   column_groups = {
 *     "file" = {
 *       "label" = @Translation("File"),
 *       "columns" = {
 *         "target_id", "width", "height"
32 33
 *       },
 *       "require_all_groups_for_translation" = TRUE
34 35 36 37 38 39 40 41 42 43
 *     },
 *     "alt" = {
 *       "label" = @Translation("Alt"),
 *       "translatable" = TRUE
 *     },
 *     "title" = {
 *       "label" = @Translation("Title"),
 *       "translatable" = TRUE
 *     },
 *   },
44
 *   list_class = "\Drupal\file\Plugin\Field\FieldType\FileFieldItemList",
45
 *   constraints = {"ReferenceAccess" = {}, "FileValidation" = {}}
46 47 48 49
 * )
 */
class ImageItem extends FileItem {

50 51 52
  /**
   * {@inheritdoc}
   */
53
  public static function defaultStorageSettings() {
54 55
    return [
      'default_image' => [
56
        'uuid' => NULL,
57 58 59 60
        'alt' => '',
        'title' => '',
        'width' => NULL,
        'height' => NULL,
61 62
      ],
    ] + parent::defaultStorageSettings();
63 64 65 66 67
  }

  /**
   * {@inheritdoc}
   */
68
  public static function defaultFieldSettings() {
69
    $settings = [
70
      'file_extensions' => 'png gif jpg jpeg',
71 72
      'alt_field' => 1,
      'alt_field_required' => 1,
73 74 75 76
      'title_field' => 0,
      'title_field_required' => 0,
      'max_resolution' => '',
      'min_resolution' => '',
77
      'default_image' => [
78
        'uuid' => NULL,
79
        'alt' => '',
80
        'title' => '',
81 82
        'width' => NULL,
        'height' => NULL,
83 84
      ],
    ] + parent::defaultFieldSettings();
85 86 87 88 89

    unset($settings['description_field']);
    return $settings;
  }

90 91 92
  /**
   * {@inheritdoc}
   */
93
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
94 95 96
    return [
      'columns' => [
        'target_id' => [
97 98 99
          'description' => 'The ID of the file entity.',
          'type' => 'int',
          'unsigned' => TRUE,
100 101
        ],
        'alt' => [
102 103 104
          'description' => "Alternative image text, for the image's 'alt' attribute.",
          'type' => 'varchar',
          'length' => 512,
105 106
        ],
        'title' => [
107 108 109
          'description' => "Image title text, for the image's 'title' attribute.",
          'type' => 'varchar',
          'length' => 1024,
110 111
        ],
        'width' => [
112 113 114
          'description' => 'The width of the image in pixels.',
          'type' => 'int',
          'unsigned' => TRUE,
115 116
        ],
        'height' => [
117 118 119
          'description' => 'The height of the image in pixels.',
          'type' => 'int',
          'unsigned' => TRUE,
120 121 122 123 124 125 126
        ],
      ],
      'indexes' => [
        'target_id' => ['target_id'],
      ],
      'foreign keys' => [
        'target_id' => [
127
          'table' => 'file_managed',
128 129 130 131
          'columns' => ['target_id' => 'fid'],
        ],
      ],
    ];
132 133 134 135 136
  }

  /**
   * {@inheritdoc}
   */
137
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
138
    $properties = parent::propertyDefinitions($field_definition);
139

140 141 142
    unset($properties['display']);
    unset($properties['description']);

143
    $properties['alt'] = DataDefinition::create('string')
144 145
      ->setLabel(t('Alternative text'))
      ->setDescription(t("Alternative image text, for the image's 'alt' attribute."));
146

147
    $properties['title'] = DataDefinition::create('string')
148 149
      ->setLabel(t('Title'))
      ->setDescription(t("Image title text, for the image's 'title' attribute."));
150

151
    $properties['width'] = DataDefinition::create('integer')
152 153
      ->setLabel(t('Width'))
      ->setDescription(t('The width of the image in pixels.'));
154

155
    $properties['height'] = DataDefinition::create('integer')
156 157
      ->setLabel(t('Height'))
      ->setDescription(t('The height of the image in pixels.'));
158

159
    return $properties;
160 161 162 163 164
  }

  /**
   * {@inheritdoc}
   */
165
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
166
    $element = [];
167 168 169 170

    // We need the field-level 'default_image' setting, and $this->getSettings()
    // will only provide the instance-level one, so we need to explicitly fetch
    // the field.
171
    $settings = $this->getFieldDefinition()->getFieldStorageDefinition()->getSettings();
172

173
    $scheme_options = \Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::WRITE_VISIBLE);
174
    $element['uri_scheme'] = [
175 176 177 178 179
      '#type' => 'radios',
      '#title' => t('Upload destination'),
      '#options' => $scheme_options,
      '#default_value' => $settings['uri_scheme'],
      '#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.'),
180
    ];
181

182 183 184
    // Add default_image element.
    static::defaultImageForm($element, $settings);
    $element['default_image']['#description'] = t('If no image is uploaded, this image will be shown on display.');
185 186 187 188 189 190 191

    return $element;
  }

  /**
   * {@inheritdoc}
   */
192 193 194
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
    // Get base form from FileItem.
    $element = parent::fieldSettingsForm($form, $form_state);
195

196
    $settings = $this->getSettings();
197 198

    // Add maximum and minimum resolution settings.
199 200
    $max_resolution = explode('x', $settings['max_resolution']) + ['', ''];
    $element['max_resolution'] = [
201 202
      '#type' => 'item',
      '#title' => t('Maximum image resolution'),
203
      '#element_validate' => [[get_class($this), 'validateResolution']],
204
      '#weight' => 4.1,
205
      '#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.'),
206 207
    ];
    $element['max_resolution']['x'] = [
208 209 210 211 212
      '#type' => 'number',
      '#title' => t('Maximum width'),
      '#title_display' => 'invisible',
      '#default_value' => $max_resolution[0],
      '#min' => 1,
213
      '#field_suffix' => ' × ',
214
      '#prefix' => '<div class="form--inline clearfix">',
215 216
    ];
    $element['max_resolution']['y'] = [
217 218 219 220 221 222
      '#type' => 'number',
      '#title' => t('Maximum height'),
      '#title_display' => 'invisible',
      '#default_value' => $max_resolution[1],
      '#min' => 1,
      '#field_suffix' => ' ' . t('pixels'),
223
      '#suffix' => '</div>',
224
    ];
225

226 227
    $min_resolution = explode('x', $settings['min_resolution']) + ['', ''];
    $element['min_resolution'] = [
228 229
      '#type' => 'item',
      '#title' => t('Minimum image resolution'),
230
      '#element_validate' => [[get_class($this), 'validateResolution']],
231
      '#weight' => 4.2,
232
      '#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.'),
233 234
    ];
    $element['min_resolution']['x'] = [
235 236 237 238 239
      '#type' => 'number',
      '#title' => t('Minimum width'),
      '#title_display' => 'invisible',
      '#default_value' => $min_resolution[0],
      '#min' => 1,
240
      '#field_suffix' => ' × ',
241
      '#prefix' => '<div class="form--inline clearfix">',
242 243
    ];
    $element['min_resolution']['y'] = [
244 245 246 247 248 249
      '#type' => 'number',
      '#title' => t('Minimum height'),
      '#title_display' => 'invisible',
      '#default_value' => $min_resolution[1],
      '#min' => 1,
      '#field_suffix' => ' ' . t('pixels'),
250
      '#suffix' => '</div>',
251
    ];
252 253 254 255 256

    // Remove the description option.
    unset($element['description_field']);

    // Add title and alt configuration options.
257
    $element['alt_field'] = [
258 259 260
      '#type' => 'checkbox',
      '#title' => t('Enable <em>Alt</em> field'),
      '#default_value' => $settings['alt_field'],
261
      '#description' => t('Short description of the image used by screen readers and displayed when the image is not loaded. Enabling this field is recommended.'),
262
      '#weight' => 9,
263 264
    ];
    $element['alt_field_required'] = [
265 266 267
      '#type' => 'checkbox',
      '#title' => t('<em>Alt</em> field required'),
      '#default_value' => $settings['alt_field_required'],
268
      '#description' => t('Making this field required is recommended.'),
269
      '#weight' => 10,
270 271 272 273 274 275 276
      '#states' => [
        'visible' => [
          ':input[name="settings[alt_field]"]' => ['checked' => TRUE],
        ],
      ],
    ];
    $element['title_field'] = [
277 278 279
      '#type' => 'checkbox',
      '#title' => t('Enable <em>Title</em> field'),
      '#default_value' => $settings['title_field'],
280
      '#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.'),
281
      '#weight' => 11,
282 283
    ];
    $element['title_field_required'] = [
284 285 286 287
      '#type' => 'checkbox',
      '#title' => t('<em>Title</em> field required'),
      '#default_value' => $settings['title_field_required'],
      '#weight' => 12,
288 289 290 291 292 293
      '#states' => [
        'visible' => [
          ':input[name="settings[title_field]"]' => ['checked' => TRUE],
        ],
      ],
    ];
294

295 296 297
    // Add default_image element.
    static::defaultImageForm($element, $settings);
    $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.");
298 299 300 301 302 303 304 305

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave() {
306 307
    parent::preSave();

308 309 310 311
    $width = $this->width;
    $height = $this->height;

    // Determine the dimensions if necessary.
312 313 314 315 316 317 318
    if ($this->entity && $this->entity instanceof EntityInterface) {
      if (empty($width) || empty($height)) {
        $image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
        if ($image->isValid()) {
          $this->width = $image->getWidth();
          $this->height = $image->getHeight();
        }
319 320
      }
    }
321 322 323
    else {
      trigger_error(sprintf("Missing file with ID %s.", $this->target_id), E_USER_WARNING);
    }
324 325
  }

326 327 328 329 330 331
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $random = new Random();
    $settings = $field_definition->getSettings();
332
    static $images = [];
333 334 335

    $min_resolution = empty($settings['min_resolution']) ? '100x100' : $settings['min_resolution'];
    $max_resolution = empty($settings['max_resolution']) ? '600x600' : $settings['max_resolution'];
336
    $extensions = array_intersect(explode(' ', $settings['file_extensions']), ['png', 'gif', 'jpg', 'jpeg']);
337 338 339
    $extension = array_rand(array_combine($extensions, $extensions));
    // Generate a max of 5 different images.
    if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= 5) {
340 341
      /** @var \Drupal\Core\File\FileSystemInterface $file_system */
      $file_system = \Drupal::service('file_system');
342 343
      $tmp_file = $file_system->tempnam('temporary://', 'generateImage_');
      $destination = $tmp_file . '.' . $extension;
344 345 346 347 348 349 350
      try {
        $file_system->move($tmp_file, $destination);
      }
      catch (FileException $e) {
        // Ignore failed move.
      }
      if ($path = $random->image($file_system->realpath($destination), $min_resolution, $max_resolution)) {
351 352
        $image = File::create();
        $image->setFileUri($path);
353
        $image->setOwnerId(\Drupal::currentUser()->id());
354
        $image->setMimeType(\Drupal::service('file.mime_type.guesser')->guess($path));
355
        $image->setFileName($file_system->basename($path));
356
        $destination_dir = static::doGetUploadLocation($settings);
357
        $file_system->prepareDirectory($destination_dir, FileSystemInterface::CREATE_DIRECTORY);
358
        $destination = $destination_dir . '/' . basename($path);
359
        $file = file_move($image, $destination);
360 361 362
        $images[$extension][$min_resolution][$max_resolution][$file->id()] = $file;
      }
      else {
363
        return [];
364 365 366 367 368 369 370 371 372
      }
    }
    else {
      // Select one of the images we've already generated for this field.
      $image_index = array_rand($images[$extension][$min_resolution][$max_resolution]);
      $file = $images[$extension][$min_resolution][$max_resolution][$image_index];
    }

    list($width, $height) = getimagesize($file->getFileUri());
373
    $values = [
374 375 376
      'target_id' => $file->id(),
      'alt' => $random->sentences(4),
      'title' => $random->sentences(4),
377
      'width' => $width,
378
      'height' => $height,
379
    ];
380 381 382
    return $values;
  }

383 384 385
  /**
   * Element validate function for resolution fields.
   */
386
  public static function validateResolution($element, FormStateInterface $form_state) {
387
    if (!empty($element['x']['#value']) || !empty($element['y']['#value'])) {
388
      foreach (['x', 'y'] as $dimension) {
389
        if (!$element[$dimension]['#value']) {
390 391
          // We expect the field name placeholder value to be wrapped in t()
          // here, so it won't be escaped again as it's already marked safe.
392
          $form_state->setError($element[$dimension], t('Both a height and width value must be specified in the @name field.', ['@name' => $element['#title']]));
393 394 395
          return;
        }
      }
396
      $form_state->setValueForElement($element, $element['x']['#value'] . 'x' . $element['y']['#value']);
397 398
    }
    else {
399
      $form_state->setValueForElement($element, '');
400 401 402
    }
  }

403 404 405 406 407 408 409 410 411
  /**
   * Builds the default_image details element.
   *
   * @param array $element
   *   The form associative array passed by reference.
   * @param array $settings
   *   The field settings array.
   */
  protected function defaultImageForm(array &$element, array $settings) {
412
    $element['default_image'] = [
413 414 415
      '#type' => 'details',
      '#title' => t('Default image'),
      '#open' => TRUE,
416
    ];
417 418
    // Convert the stored UUID to a FID.
    $fids = [];
419
    $uuid = $settings['default_image']['uuid'];
420
    if ($uuid && ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid))) {
421 422
      $fids[0] = $file->id();
    }
423
    $element['default_image']['uuid'] = [
424 425 426
      '#type' => 'managed_file',
      '#title' => t('Image'),
      '#description' => t('Image to be shown if no image is uploaded.'),
427
      '#default_value' => $fids,
428
      '#upload_location' => $settings['uri_scheme'] . '://default_images/',
429
      '#element_validate' => [
430
        '\Drupal\file\Element\ManagedFile::validateManagedFile',
431 432
        [get_class($this), 'validateDefaultImageForm'],
      ],
433
      '#upload_validators' => $this->getUploadValidators(),
434 435
    ];
    $element['default_image']['alt'] = [
436
      '#type' => 'textfield',
437
      '#title' => t('Alternative text'),
438
      '#description' => t('Short description of the image used by screen readers and displayed when the image is not loaded. This is important for accessibility.'),
439 440
      '#default_value' => $settings['default_image']['alt'],
      '#maxlength' => 512,
441 442
    ];
    $element['default_image']['title'] = [
443 444 445 446 447
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image.'),
      '#default_value' => $settings['default_image']['title'],
      '#maxlength' => 1024,
448 449
    ];
    $element['default_image']['width'] = [
450 451
      '#type' => 'value',
      '#value' => $settings['default_image']['width'],
452 453
    ];
    $element['default_image']['height'] = [
454 455
      '#type' => 'value',
      '#value' => $settings['default_image']['height'],
456
    ];
457 458
  }

459 460 461 462 463 464 465 466 467
  /**
   * Validates the managed_file element for the default Image form.
   *
   * This function ensures the fid is a scalar value and not an array. It is
   * assigned as a #element_validate callback in
   * \Drupal\image\Plugin\Field\FieldType\ImageItem::defaultImageForm().
   *
   * @param array $element
   *   The form element to process.
468
   * @param \Drupal\Core\Form\FormStateInterface $form_state
469 470
   *   The form state.
   */
471
  public static function validateDefaultImageForm(array &$element, FormStateInterface $form_state) {
472 473 474 475
    // Consolidate the array value of this field to a single FID as #extended
    // for default image is not TRUE and this is a single value.
    if (isset($element['fids']['#value'][0])) {
      $value = $element['fids']['#value'][0];
476
      // Convert the file ID to a uuid.
477
      if ($file = \Drupal::entityTypeManager()->getStorage('file')->load($value)) {
478 479
        $value = $file->uuid();
      }
480 481
    }
    else {
482
      $value = '';
483
    }
484
    $form_state->setValueForElement($element, $value);
485 486
  }

487 488 489 490 491 492 493 494
  /**
   * {@inheritdoc}
   */
  public function isDisplayed() {
    // Image items do not have per-item visibility settings.
    return TRUE;
  }

495
}