comparison core/modules/image/image.module @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
3 /** 3 /**
4 * @file 4 * @file
5 * Exposes global functionality for creating image styles. 5 * Exposes global functionality for creating image styles.
6 */ 6 */
7 7
8 use Drupal\Core\Url;
8 use Drupal\Core\Entity\EntityInterface; 9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\File\FileSystemInterface;
9 use Drupal\Core\Routing\RouteMatchInterface; 11 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\file\FileInterface; 12 use Drupal\file\FileInterface;
11 use Drupal\field\FieldStorageConfigInterface; 13 use Drupal\field\FieldStorageConfigInterface;
12 use Drupal\field\FieldConfigInterface; 14 use Drupal\field\FieldConfigInterface;
13 use Drupal\image\Entity\ImageStyle; 15 use Drupal\image\Entity\ImageStyle;
66 * Implements hook_help(). 68 * Implements hook_help().
67 */ 69 */
68 function image_help($route_name, RouteMatchInterface $route_match) { 70 function image_help($route_name, RouteMatchInterface $route_match) {
69 switch ($route_name) { 71 switch ($route_name) {
70 case 'help.page.image': 72 case 'help.page.image':
71 $field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#'; 73 $field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#';
72 74
73 $output = ''; 75 $output = '';
74 $output .= '<h3>' . t('About') . '</h3>'; 76 $output .= '<h3>' . t('About') . '</h3>';
75 $output .= '<p>' . t('The Image module allows you to create fields that contain image files and to configure <a href=":image_styles">Image styles</a> that can be used to manipulate the display of images. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for terminology and general information on entities, fields, and how to create and manage fields. For more information, see the <a href=":image_documentation">online documentation for the Image module</a>.', [':image_styles' => \Drupal::url('entity.image_style.collection'), ':field' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => $field_ui_url, ':image_documentation' => 'https://www.drupal.org/documentation/modules/image']) . '</p>'; 77 $output .= '<p>' . t('The Image module allows you to create fields that contain image files and to configure <a href=":image_styles">Image styles</a> that can be used to manipulate the display of images. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for terminology and general information on entities, fields, and how to create and manage fields. For more information, see the <a href=":image_documentation">online documentation for the Image module</a>.', [':image_styles' => Url::fromRoute('entity.image_style.collection')->toString(), ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => $field_ui_url, ':image_documentation' => 'https://www.drupal.org/documentation/modules/image']) . '</p>';
76 $output .= '<h3>' . t('Uses') . '</h3>'; 78 $output .= '<h3>' . t('Uses') . '</h3>';
77 $output .= '<dt>' . t('Defining image styles') . '</dt>'; 79 $output .= '<dt>' . t('Defining image styles') . '</dt>';
78 $output .= '<dd>' . t('The concept of image styles is that you can upload a single image but display it in several ways; each display variation, or <em>image style</em>, is the result of applying one or more <em>effects</em> to the original image. As an example, you might upload a high-resolution image with a 4:3 aspect ratio, and display it scaled down, square cropped, or black-and-white (or any combination of these effects). The Image module provides a way to do this efficiently: you configure an image style with the desired effects on the <a href=":image">Image styles page</a>, and the first time a particular image is requested in that style, the effects are applied. The resulting image is saved, and the next time that same style is requested, the saved image is retrieved without the need to recalculate the effects. Drupal core provides several effects that you can use to define styles; others may be provided by contributed modules.', [':image' => \Drupal::url('entity.image_style.collection')]); 80 $output .= '<dd>' . t('The concept of image styles is that you can upload a single image but display it in several ways; each display variation, or <em>image style</em>, is the result of applying one or more <em>effects</em> to the original image. As an example, you might upload a high-resolution image with a 4:3 aspect ratio, and display it scaled down, square cropped, or black-and-white (or any combination of these effects). The Image module provides a way to do this efficiently: you configure an image style with the desired effects on the <a href=":image">Image styles page</a>, and the first time a particular image is requested in that style, the effects are applied. The resulting image is saved, and the next time that same style is requested, the saved image is retrieved without the need to recalculate the effects. Drupal core provides several effects that you can use to define styles; others may be provided by contributed modules.', [':image' => Url::fromRoute('entity.image_style.collection')->toString()]);
79 $output .= '<dt>' . t('Naming image styles') . '</dt>'; 81 $output .= '<dt>' . t('Naming image styles') . '</dt>';
80 $output .= '<dd>' . t('When you define an image style, you will need to choose a displayed name and a machine name. The displayed name is shown in administrative pages, and the machine name is used to generate the URL for accessing an image processed in that style. There are two common approaches to naming image styles: either based on the effects being applied (for example, <em>Square 85x85</em>), or based on where you plan to use it (for example, <em>Profile picture</em>).') . '</dd>'; 82 $output .= '<dd>' . t('When you define an image style, you will need to choose a displayed name and a machine name. The displayed name is shown in administrative pages, and the machine name is used to generate the URL for accessing an image processed in that style. There are two common approaches to naming image styles: either based on the effects being applied (for example, <em>Square 85x85</em>), or based on where you plan to use it (for example, <em>Profile picture</em>).') . '</dd>';
81 $output .= '<dt>' . t('Configuring image fields') . '</dt>'; 83 $output .= '<dt>' . t('Configuring image fields') . '</dt>';
82 $output .= '<dd>' . t('A few of the settings for image fields are defined once when you create the field and cannot be changed later; these include the choice of public or private file storage and the number of images that can be stored in the field. The rest of the settings can be edited later; these settings include the field label, help text, allowed file extensions, image resolution restrictions, and the subdirectory in the public or private file storage where the images will be stored. The editable settings can also have different values for different entity sub-types; for instance, if your image field is used on both Page and Article content types, you can store the files in a different subdirectory for the two content types.') . '</dd>'; 84 $output .= '<dd>' . t('A few of the settings for image fields are defined once when you create the field and cannot be changed later; these include the choice of public or private file storage and the number of images that can be stored in the field. The rest of the settings can be edited later; these settings include the field label, help text, allowed file extensions, image resolution restrictions, and the subdirectory in the public or private file storage where the images will be stored. The editable settings can also have different values for different entity sub-types; for instance, if your image field is used on both Page and Article content types, you can store the files in a different subdirectory for the two content types.') . '</dd>';
83 $output .= '<dd>' . t('For accessibility and search engine optimization, all images that convey meaning on web sites should have alternate text. Drupal also allows entry of title text for images, but it can lead to confusion for screen reader users and its use is not recommended. Image fields can be configured so that alternate and title text fields are enabled or disabled; if enabled, the fields can be set to be required. The recommended setting is to enable and require alternate text and disable title text.') . '</dd>'; 85 $output .= '<dd>' . t('For accessibility and search engine optimization, all images that convey meaning on web sites should have alternate text. Drupal also allows entry of title text for images, but it can lead to confusion for screen reader users and its use is not recommended. Image fields can be configured so that alternate and title text fields are enabled or disabled; if enabled, the fields can be set to be required. The recommended setting is to enable and require alternate text and disable title text.') . '</dd>';
84 $output .= '<dd>' . t('When you create an image field, you will need to choose whether the uploaded images will be stored in the public or private file directory defined in your settings.php file and shown on the <a href=":file-system">File system page</a>. This choice cannot be changed later. You can also configure your field to store files in a subdirectory of the public or private directory; this setting can be changed later and can be different for each entity sub-type using the field. For more information on file storage, see the <a href=":system-help">System module help page</a>.', [':file-system' => \Drupal::url('system.file_system_settings'), ':system-help' => \Drupal::url('help.page', ['name' => 'system'])]) . '</dd>'; 86 $output .= '<dd>' . t('When you create an image field, you will need to choose whether the uploaded images will be stored in the public or private file directory defined in your settings.php file and shown on the <a href=":file-system">File system page</a>. This choice cannot be changed later. You can also configure your field to store files in a subdirectory of the public or private directory; this setting can be changed later and can be different for each entity sub-type using the field. For more information on file storage, see the <a href=":system-help">System module help page</a>.', [':file-system' => Url::fromRoute('system.file_system_settings')->toString(), ':system-help' => Url::fromRoute('help.page', ['name' => 'system'])->toString()]) . '</dd>';
85 $output .= '<dd>' . t('The maximum file size that can be uploaded is limited by PHP settings of the server, but you can restrict it further by configuring a <em>Maximum upload size</em> in the field settings (this setting can be changed later). The maximum file size, either from PHP server settings or field configuration, is automatically displayed to users in the help text of the image field.') . '</dd>'; 87 $output .= '<dd>' . t('The maximum file size that can be uploaded is limited by PHP settings of the server, but you can restrict it further by configuring a <em>Maximum upload size</em> in the field settings (this setting can be changed later). The maximum file size, either from PHP server settings or field configuration, is automatically displayed to users in the help text of the image field.') . '</dd>';
86 $output .= '<dd>' . t('You can also configure a minimum and/or maximum resolution for uploaded images. Images that are too small will be rejected. Images that are to large will be resized. During the resizing the <a href="http://wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image will be lost.') . '</dd>'; 88 $output .= '<dd>' . t('You can also configure a minimum and/or maximum resolution for uploaded images. Images that are too small will be rejected. Images that are to large will be resized. During the resizing the <a href="http://wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image will be lost.') . '</dd>';
87 $output .= '<dd>' . t('You can also configure a default image that will be used if no image is uploaded in an image field. This default can be defined for all instances of the field in the field storage settings when you create a field, and the setting can be overridden for each entity sub-type that uses the field.') . '</dd>'; 89 $output .= '<dd>' . t('You can also configure a default image that will be used if no image is uploaded in an image field. This default can be defined for all instances of the field in the field storage settings when you create a field, and the setting can be overridden for each entity sub-type that uses the field.') . '</dd>';
88 $output .= '<dt>' . t('Configuring displays and form displays') . '</dt>'; 90 $output .= '<dt>' . t('Configuring displays and form displays') . '</dt>';
89 $output .= '<dd>' . t('On the <em>Manage display</em> page, you can choose the image formatter, which determines the image style used to display the image in each display mode and whether or not to display the image as a link. On the <em>Manage form display</em> page, you can configure the image upload widget, including setting the preview image style shown on the entity edit form.') . '</dd>'; 91 $output .= '<dd>' . t('On the <em>Manage display</em> page, you can choose the image formatter, which determines the image style used to display the image in each display mode and whether or not to display the image as a link. On the <em>Manage form display</em> page, you can configure the image upload widget, including setting the preview image style shown on the entity edit form.') . '</dd>';
381 383
382 $uuid = $default_image['uuid']; 384 $uuid = $default_image['uuid'];
383 if ($uuid) { 385 if ($uuid) {
384 $original_uuid = isset($entity->original) ? $entity->original->getSetting('default_image')['uuid'] : NULL; 386 $original_uuid = isset($entity->original) ? $entity->original->getSetting('default_image')['uuid'] : NULL;
385 if ($uuid != $original_uuid) { 387 if ($uuid != $original_uuid) {
386 $file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid); 388 $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid);
387 if ($file) { 389 if ($file) {
388 $image = \Drupal::service('image.factory')->get($file->getFileUri()); 390 $image = \Drupal::service('image.factory')->get($file->getFileUri());
389 $default_image['width'] = $image->getWidth(); 391 $default_image['width'] = $image->getWidth();
390 $default_image['height'] = $image->getHeight(); 392 $default_image['height'] = $image->getHeight();
391 } 393 }
412 414
413 // The value of a managed_file element can be an array if #extended == TRUE. 415 // The value of a managed_file element can be an array if #extended == TRUE.
414 $uuid_new = $field_storage->getSetting('default_image')['uuid']; 416 $uuid_new = $field_storage->getSetting('default_image')['uuid'];
415 $uuid_old = $prior_field_storage->getSetting('default_image')['uuid']; 417 $uuid_old = $prior_field_storage->getSetting('default_image')['uuid'];
416 418
417 $file_new = $uuid_new ? \Drupal::entityManager()->loadEntityByUuid('file', $uuid_new) : FALSE; 419 $file_new = $uuid_new ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_new) : FALSE;
418 420
419 if ($uuid_new != $uuid_old) { 421 if ($uuid_new != $uuid_old) {
420 422
421 // Is there a new file? 423 // Is there a new file?
422 if ($file_new) { 424 if ($file_new) {
424 $file_new->save(); 426 $file_new->save();
425 \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_storage->uuid()); 427 \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_storage->uuid());
426 } 428 }
427 429
428 // Is there an old file? 430 // Is there an old file?
429 if ($uuid_old && ($file_old = \Drupal::entityManager()->loadEntityByUuid('file', $uuid_old))) { 431 if ($uuid_old && ($file_old = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_old))) {
430 \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid()); 432 \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid());
431 } 433 }
432 } 434 }
433 435
434 // If the upload destination changed, then move the file. 436 // If the upload destination changed, then move the file.
435 if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) { 437 if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) {
436 $directory = $field_storage->getSetting('uri_scheme') . '://default_images/'; 438 $directory = $field_storage->getSetting('uri_scheme') . '://default_images/';
437 file_prepare_directory($directory, FILE_CREATE_DIRECTORY); 439 \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
438 file_move($file_new, $directory . $file_new->getFilename()); 440 file_move($file_new, $directory . $file_new->getFilename());
439 } 441 }
440 } 442 }
441 443
442 /** 444 /**
453 455
454 $uuid_new = $field->getSetting('default_image')['uuid']; 456 $uuid_new = $field->getSetting('default_image')['uuid'];
455 $uuid_old = $prior_instance->getSetting('default_image')['uuid']; 457 $uuid_old = $prior_instance->getSetting('default_image')['uuid'];
456 458
457 // If the old and new files do not match, update the default accordingly. 459 // If the old and new files do not match, update the default accordingly.
458 $file_new = $uuid_new ? \Drupal::entityManager()->loadEntityByUuid('file', $uuid_new) : FALSE; 460 $file_new = $uuid_new ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_new) : FALSE;
459 if ($uuid_new != $uuid_old) { 461 if ($uuid_new != $uuid_old) {
460 // Save the new file, if present. 462 // Save the new file, if present.
461 if ($file_new) { 463 if ($file_new) {
462 $file_new->status = FILE_STATUS_PERMANENT; 464 $file_new->status = FILE_STATUS_PERMANENT;
463 $file_new->save(); 465 $file_new->save();
464 \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid()); 466 \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid());
465 } 467 }
466 // Delete the old file, if present. 468 // Delete the old file, if present.
467 if ($uuid_old && ($file_old = \Drupal::entityManager()->loadEntityByUuid('file', $uuid_old))) { 469 if ($uuid_old && ($file_old = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_old))) {
468 \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid()); 470 \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid());
469 } 471 }
470 } 472 }
471 473
472 // If the upload destination changed, then move the file. 474 // If the upload destination changed, then move the file.
473 if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) { 475 if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) {
474 $directory = $field_storage->getSetting('uri_scheme') . '://default_images/'; 476 $directory = $field_storage->getSetting('uri_scheme') . '://default_images/';
475 file_prepare_directory($directory, FILE_CREATE_DIRECTORY); 477 \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
476 file_move($file_new, $directory . $file_new->getFilename()); 478 file_move($file_new, $directory . $file_new->getFilename());
477 } 479 }
478 } 480 }
479 481
480 /** 482 /**
486 return; 488 return;
487 } 489 }
488 490
489 // The value of a managed_file element can be an array if #extended == TRUE. 491 // The value of a managed_file element can be an array if #extended == TRUE.
490 $uuid = $field->getSetting('default_image')['uuid']; 492 $uuid = $field->getSetting('default_image')['uuid'];
491 if ($uuid && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid))) { 493 if ($uuid && ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid))) {
492 \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid()); 494 \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid());
493 } 495 }
494 } 496 }
495 497
496 /** 498 /**
505 507
506 // The value of a managed_file element can be an array if #extended == TRUE. 508 // The value of a managed_file element can be an array if #extended == TRUE.
507 $uuid = $field->getSetting('default_image')['uuid']; 509 $uuid = $field->getSetting('default_image')['uuid'];
508 510
509 // Remove the default image when the instance is deleted. 511 // Remove the default image when the instance is deleted.
510 if ($uuid && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid))) { 512 if ($uuid && ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid))) {
511 \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid()); 513 \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid());
512 } 514 }
513 } 515 }