annotate core/modules/image/image.module @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Exposes global functionality for creating image styles.
Chris@0 6 */
Chris@0 7
Chris@18 8 use Drupal\Core\Url;
Chris@0 9 use Drupal\Core\Entity\EntityInterface;
Chris@18 10 use Drupal\Core\File\FileSystemInterface;
Chris@0 11 use Drupal\Core\Routing\RouteMatchInterface;
Chris@14 12 use Drupal\file\FileInterface;
Chris@0 13 use Drupal\field\FieldStorageConfigInterface;
Chris@0 14 use Drupal\field\FieldConfigInterface;
Chris@0 15 use Drupal\image\Entity\ImageStyle;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Image style constant for user presets in the database.
Chris@0 19 *
Chris@0 20 * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0.
Chris@0 21 *
Chris@0 22 * @see https://www.drupal.org/node/1820974
Chris@0 23 */
Chris@0 24 const IMAGE_STORAGE_NORMAL = 1;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Image style constant for user presets that override module-defined presets.
Chris@0 28 *
Chris@0 29 * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0.
Chris@0 30 *
Chris@0 31 * @see https://www.drupal.org/node/1820974
Chris@0 32 */
Chris@0 33 const IMAGE_STORAGE_OVERRIDE = 2;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Image style constant for module-defined presets in code.
Chris@0 37 *
Chris@0 38 * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0.
Chris@0 39 *
Chris@0 40 * @see https://www.drupal.org/node/1820974
Chris@0 41 */
Chris@0 42 const IMAGE_STORAGE_DEFAULT = 4;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Image style constant to represent an editable preset.
Chris@0 46 *
Chris@0 47 * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0.
Chris@0 48 *
Chris@0 49 * @see https://www.drupal.org/node/1820974
Chris@0 50 */
Chris@0 51 define('IMAGE_STORAGE_EDITABLE', IMAGE_STORAGE_NORMAL | IMAGE_STORAGE_OVERRIDE);
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Image style constant to represent any module-based preset.
Chris@0 55 *
Chris@0 56 * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0.
Chris@0 57 *
Chris@0 58 * @see https://www.drupal.org/node/1820974
Chris@0 59 */
Chris@0 60 define('IMAGE_STORAGE_MODULE', IMAGE_STORAGE_OVERRIDE | IMAGE_STORAGE_DEFAULT);
Chris@0 61
Chris@0 62 /**
Chris@0 63 * The name of the query parameter for image derivative tokens.
Chris@0 64 */
Chris@0 65 define('IMAGE_DERIVATIVE_TOKEN', 'itok');
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Implements hook_help().
Chris@0 69 */
Chris@0 70 function image_help($route_name, RouteMatchInterface $route_match) {
Chris@0 71 switch ($route_name) {
Chris@0 72 case 'help.page.image':
Chris@18 73 $field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#';
Chris@0 74
Chris@0 75 $output = '';
Chris@0 76 $output .= '<h3>' . t('About') . '</h3>';
Chris@18 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>';
Chris@0 78 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 79 $output .= '<dt>' . t('Defining image styles') . '</dt>';
Chris@18 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()]);
Chris@0 81 $output .= '<dt>' . t('Naming image styles') . '</dt>';
Chris@0 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>';
Chris@0 83 $output .= '<dt>' . t('Configuring image fields') . '</dt>';
Chris@0 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>';
Chris@0 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>';
Chris@18 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>';
Chris@0 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>';
Chris@0 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>';
Chris@0 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>';
Chris@0 90 $output .= '<dt>' . t('Configuring displays and form displays') . '</dt>';
Chris@0 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>';
Chris@0 92 $output .= '</dl>';
Chris@0 93 return $output;
Chris@0 94
Chris@0 95 case 'entity.image_style.collection':
Chris@0 96 return '<p>' . t('Image styles commonly provide thumbnail sizes by scaling and cropping images, but can also add various effects before an image is displayed. When an image is displayed with a style, a new file is created and the original image is left unchanged.') . '</p>';
Chris@0 97
Chris@0 98 case 'image.effect_add_form':
Chris@0 99 $effect = \Drupal::service('plugin.manager.image.effect')->getDefinition($route_match->getParameter('image_effect'));
Chris@0 100 return isset($effect['description']) ? ('<p>' . $effect['description'] . '</p>') : NULL;
Chris@0 101
Chris@0 102 case 'image.effect_edit_form':
Chris@0 103 $effect = $route_match->getParameter('image_style')->getEffect($route_match->getParameter('image_effect'));
Chris@0 104 $effect_definition = $effect->getPluginDefinition();
Chris@0 105 return isset($effect_definition['description']) ? ('<p>' . $effect_definition['description'] . '</p>') : NULL;
Chris@0 106 }
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Implements hook_theme().
Chris@0 111 */
Chris@0 112 function image_theme() {
Chris@0 113 return [
Chris@0 114 // Theme functions in image.module.
Chris@0 115 'image_style' => [
Chris@0 116 // HTML 4 and XHTML 1.0 always require an alt attribute. The HTML 5 draft
Chris@0 117 // allows the alt attribute to be omitted in some cases. Therefore,
Chris@0 118 // default the alt attribute to an empty string, but allow code using
Chris@0 119 // '#theme' => 'image_style' to pass explicit NULL for it to be omitted.
Chris@0 120 // Usually, neither omission nor an empty string satisfies accessibility
Chris@0 121 // requirements, so it is strongly encouraged for code using '#theme' =>
Chris@0 122 // 'image_style' to pass a meaningful value for the alt variable.
Chris@0 123 // - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
Chris@0 124 // - http://www.w3.org/TR/xhtml1/dtds.html
Chris@0 125 // - http://dev.w3.org/html5/spec/Overview.html#alt
Chris@0 126 // The title attribute is optional in all cases, so it is omitted by
Chris@0 127 // default.
Chris@0 128 'variables' => [
Chris@0 129 'style_name' => NULL,
Chris@0 130 'uri' => NULL,
Chris@0 131 'width' => NULL,
Chris@0 132 'height' => NULL,
Chris@0 133 'alt' => '',
Chris@0 134 'title' => NULL,
Chris@0 135 'attributes' => [],
Chris@0 136 ],
Chris@0 137 ],
Chris@0 138
Chris@0 139 // Theme functions in image.admin.inc.
Chris@0 140 'image_style_preview' => [
Chris@0 141 'variables' => ['style' => NULL],
Chris@0 142 'file' => 'image.admin.inc',
Chris@0 143 ],
Chris@0 144 'image_anchor' => [
Chris@0 145 'render element' => 'element',
Chris@0 146 'file' => 'image.admin.inc',
Chris@0 147 ],
Chris@0 148 'image_resize_summary' => [
Chris@0 149 'variables' => ['data' => NULL, 'effect' => []],
Chris@0 150 ],
Chris@0 151 'image_scale_summary' => [
Chris@0 152 'variables' => ['data' => NULL, 'effect' => []],
Chris@0 153 ],
Chris@0 154 'image_crop_summary' => [
Chris@0 155 'variables' => ['data' => NULL, 'effect' => []],
Chris@0 156 ],
Chris@17 157 'image_scale_and_crop_summary' => [
Chris@17 158 'variables' => ['data' => NULL, 'effect' => []],
Chris@17 159 ],
Chris@0 160 'image_rotate_summary' => [
Chris@0 161 'variables' => ['data' => NULL, 'effect' => []],
Chris@0 162 ],
Chris@0 163
Chris@0 164 // Theme functions in image.field.inc.
Chris@0 165 'image_widget' => [
Chris@0 166 'render element' => 'element',
Chris@0 167 'file' => 'image.field.inc',
Chris@0 168 ],
Chris@0 169 'image_formatter' => [
Chris@0 170 'variables' => ['item' => NULL, 'item_attributes' => NULL, 'url' => NULL, 'image_style' => NULL],
Chris@0 171 'file' => 'image.field.inc',
Chris@0 172 ],
Chris@0 173 ];
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Implements hook_file_download().
Chris@0 178 *
Chris@0 179 * Control the access to files underneath the styles directory.
Chris@0 180 */
Chris@0 181 function image_file_download($uri) {
Chris@0 182 $path = file_uri_target($uri);
Chris@0 183
Chris@0 184 // Private file access for image style derivatives.
Chris@0 185 if (strpos($path, 'styles/') === 0) {
Chris@0 186 $args = explode('/', $path);
Chris@0 187
Chris@0 188 // Discard "styles", style name, and scheme from the path
Chris@0 189 $args = array_slice($args, 3);
Chris@0 190
Chris@0 191 // Then the remaining parts are the path to the image.
Chris@0 192 $original_uri = file_uri_scheme($uri) . '://' . implode('/', $args);
Chris@0 193
Chris@0 194 // Check that the file exists and is an image.
Chris@0 195 $image = \Drupal::service('image.factory')->get($uri);
Chris@0 196 if ($image->isValid()) {
Chris@0 197 // Check the permissions of the original to grant access to this image.
Chris@0 198 $headers = \Drupal::moduleHandler()->invokeAll('file_download', [$original_uri]);
Chris@0 199 // Confirm there's at least one module granting access and none denying access.
Chris@0 200 if (!empty($headers) && !in_array(-1, $headers)) {
Chris@0 201 return [
Chris@0 202 // Send headers describing the image's size, and MIME-type.
Chris@0 203 'Content-Type' => $image->getMimeType(),
Chris@0 204 'Content-Length' => $image->getFileSize(),
Chris@0 205 // By not explicitly setting them here, this uses normal Drupal
Chris@0 206 // Expires, Cache-Control and ETag headers to prevent proxy or
Chris@0 207 // browser caching of private images.
Chris@0 208 ];
Chris@0 209 }
Chris@0 210 }
Chris@0 211 return -1;
Chris@0 212 }
Chris@0 213 }
Chris@0 214
Chris@0 215 /**
Chris@0 216 * Implements hook_file_move().
Chris@0 217 */
Chris@14 218 function image_file_move(FileInterface $file, FileInterface $source) {
Chris@0 219 // Delete any image derivatives at the original image path.
Chris@0 220 image_path_flush($source->getFileUri());
Chris@0 221 }
Chris@0 222
Chris@0 223 /**
Chris@0 224 * Implements hook_ENTITY_TYPE_predelete() for file entities.
Chris@0 225 */
Chris@14 226 function image_file_predelete(FileInterface $file) {
Chris@0 227 // Delete any image derivatives of this image.
Chris@0 228 image_path_flush($file->getFileUri());
Chris@0 229 }
Chris@0 230
Chris@0 231 /**
Chris@0 232 * Clears cached versions of a specific file in all styles.
Chris@0 233 *
Chris@0 234 * @param $path
Chris@0 235 * The Drupal file path to the original image.
Chris@0 236 */
Chris@0 237 function image_path_flush($path) {
Chris@0 238 $styles = ImageStyle::loadMultiple();
Chris@0 239 foreach ($styles as $style) {
Chris@0 240 $style->flush($path);
Chris@0 241 }
Chris@0 242 }
Chris@0 243
Chris@0 244 /**
Chris@0 245 * Gets an array of image styles suitable for using as select list options.
Chris@0 246 *
Chris@0 247 * @param $include_empty
Chris@0 248 * If TRUE a '- None -' option will be inserted in the options array.
Chris@0 249 * @return
Chris@0 250 * Array of image styles both key and value are set to style name.
Chris@0 251 */
Chris@0 252 function image_style_options($include_empty = TRUE) {
Chris@0 253 $styles = ImageStyle::loadMultiple();
Chris@0 254 $options = [];
Chris@0 255 if ($include_empty && !empty($styles)) {
Chris@0 256 $options[''] = t('- None -');
Chris@0 257 }
Chris@0 258 foreach ($styles as $name => $style) {
Chris@0 259 $options[$name] = $style->label();
Chris@0 260 }
Chris@0 261
Chris@0 262 if (empty($options)) {
Chris@0 263 $options[''] = t('No defined styles');
Chris@0 264 }
Chris@0 265 return $options;
Chris@0 266 }
Chris@0 267
Chris@0 268 /**
Chris@0 269 * Prepares variables for image style templates.
Chris@0 270 *
Chris@0 271 * Default template: image-style.html.twig.
Chris@0 272 *
Chris@0 273 * @param array $variables
Chris@0 274 * An associative array containing:
Chris@0 275 * - width: The width of the image.
Chris@0 276 * - height: The height of the image.
Chris@0 277 * - style_name: The name of the image style to be applied.
Chris@0 278 * - attributes: Additional attributes to apply to the image.
Chris@0 279 * - uri: URI of the source image before styling.
Chris@0 280 * - alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0
Chris@0 281 * always require an alt attribute. The HTML 5 draft allows the alt
Chris@0 282 * attribute to be omitted in some cases. Therefore, this variable defaults
Chris@0 283 * to an empty string, but can be set to NULL for the attribute to be
Chris@0 284 * omitted. Usually, neither omission nor an empty string satisfies
Chris@0 285 * accessibility requirements, so it is strongly encouraged for code using
Chris@0 286 * '#theme' => 'image_style' to pass a meaningful value for this variable.
Chris@0 287 * - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
Chris@0 288 * - http://www.w3.org/TR/xhtml1/dtds.html
Chris@0 289 * - http://dev.w3.org/html5/spec/Overview.html#alt
Chris@0 290 * - title: The title text is displayed when the image is hovered in some
Chris@0 291 * popular browsers.
Chris@0 292 * - attributes: Associative array of attributes to be placed in the img tag.
Chris@0 293 */
Chris@0 294 function template_preprocess_image_style(&$variables) {
Chris@0 295 $style = ImageStyle::load($variables['style_name']);
Chris@0 296
Chris@0 297 // Determine the dimensions of the styled image.
Chris@0 298 $dimensions = [
Chris@0 299 'width' => $variables['width'],
Chris@0 300 'height' => $variables['height'],
Chris@0 301 ];
Chris@0 302
Chris@0 303 $style->transformDimensions($dimensions, $variables['uri']);
Chris@0 304
Chris@0 305 $variables['image'] = [
Chris@0 306 '#theme' => 'image',
Chris@0 307 '#width' => $dimensions['width'],
Chris@0 308 '#height' => $dimensions['height'],
Chris@0 309 '#attributes' => $variables['attributes'],
Chris@0 310 '#style_name' => $variables['style_name'],
Chris@0 311 ];
Chris@0 312
Chris@0 313 // If the current image toolkit supports this file type, prepare the URI for
Chris@0 314 // the derivative image. If not, just use the original image resized to the
Chris@0 315 // dimensions specified by the style.
Chris@0 316 if ($style->supportsUri($variables['uri'])) {
Chris@0 317 $variables['image']['#uri'] = $style->buildUrl($variables['uri']);
Chris@0 318 }
Chris@0 319 else {
Chris@0 320 $variables['image']['#uri'] = $variables['uri'];
Chris@0 321 // Don't render the image by default, but allow other preprocess functions
Chris@0 322 // to override that if they need to.
Chris@0 323 $variables['image']['#access'] = FALSE;
Chris@0 324
Chris@0 325 // Inform the site builders why their image didn't work.
Chris@0 326 \Drupal::logger('image')->warning('Could not apply @style image style to @uri because the style does not support it.', [
Chris@0 327 '@style' => $style->label(),
Chris@0 328 '@uri' => $variables['uri'],
Chris@0 329 ]);
Chris@0 330 }
Chris@0 331
Chris@0 332 if (isset($variables['alt']) || array_key_exists('alt', $variables)) {
Chris@0 333 $variables['image']['#alt'] = $variables['alt'];
Chris@0 334 }
Chris@0 335 if (isset($variables['title']) || array_key_exists('title', $variables)) {
Chris@0 336 $variables['image']['#title'] = $variables['title'];
Chris@0 337 }
Chris@0 338
Chris@0 339 }
Chris@0 340
Chris@0 341 /**
Chris@0 342 * Accepts a keyword (center, top, left, etc) and returns it as a pixel offset.
Chris@0 343 *
Chris@0 344 * @param $value
Chris@0 345 * @param $current_pixels
Chris@0 346 * @param $new_pixels
Chris@0 347 */
Chris@0 348 function image_filter_keyword($value, $current_pixels, $new_pixels) {
Chris@0 349 switch ($value) {
Chris@0 350 case 'top':
Chris@0 351 case 'left':
Chris@0 352 return 0;
Chris@0 353
Chris@0 354 case 'bottom':
Chris@0 355 case 'right':
Chris@0 356 return $current_pixels - $new_pixels;
Chris@0 357
Chris@0 358 case 'center':
Chris@0 359 return $current_pixels / 2 - $new_pixels / 2;
Chris@0 360 }
Chris@0 361 return $value;
Chris@0 362 }
Chris@0 363
Chris@0 364 /**
Chris@0 365 * Implements hook_entity_presave().
Chris@0 366 *
Chris@0 367 * Transforms default image of image field from array into single value at save.
Chris@0 368 */
Chris@0 369 function image_entity_presave(EntityInterface $entity) {
Chris@0 370 // Get the default image settings, return if not saving an image field storage
Chris@0 371 // or image field entity.
Chris@0 372 $default_image = [];
Chris@0 373 if (($entity instanceof FieldStorageConfigInterface || $entity instanceof FieldConfigInterface) && $entity->getType() == 'image') {
Chris@0 374 $default_image = $entity->getSetting('default_image');
Chris@0 375 }
Chris@0 376 else {
Chris@0 377 return;
Chris@0 378 }
Chris@0 379
Chris@0 380 if ($entity->isSyncing()) {
Chris@0 381 return;
Chris@0 382 }
Chris@0 383
Chris@0 384 $uuid = $default_image['uuid'];
Chris@0 385 if ($uuid) {
Chris@0 386 $original_uuid = isset($entity->original) ? $entity->original->getSetting('default_image')['uuid'] : NULL;
Chris@0 387 if ($uuid != $original_uuid) {
Chris@18 388 $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid);
Chris@0 389 if ($file) {
Chris@0 390 $image = \Drupal::service('image.factory')->get($file->getFileUri());
Chris@0 391 $default_image['width'] = $image->getWidth();
Chris@0 392 $default_image['height'] = $image->getHeight();
Chris@0 393 }
Chris@0 394 else {
Chris@0 395 $default_image['uuid'] = NULL;
Chris@0 396 }
Chris@0 397 }
Chris@0 398 }
Chris@0 399 // Both FieldStorageConfigInterface and FieldConfigInterface have a
Chris@0 400 // setSetting() method.
Chris@0 401 $entity->setSetting('default_image', $default_image);
Chris@0 402 }
Chris@0 403
Chris@0 404 /**
Chris@0 405 * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.
Chris@0 406 */
Chris@0 407 function image_field_storage_config_update(FieldStorageConfigInterface $field_storage) {
Chris@0 408 if ($field_storage->getType() != 'image') {
Chris@0 409 // Only act on image fields.
Chris@0 410 return;
Chris@0 411 }
Chris@0 412
Chris@0 413 $prior_field_storage = $field_storage->original;
Chris@0 414
Chris@0 415 // The value of a managed_file element can be an array if #extended == TRUE.
Chris@0 416 $uuid_new = $field_storage->getSetting('default_image')['uuid'];
Chris@0 417 $uuid_old = $prior_field_storage->getSetting('default_image')['uuid'];
Chris@0 418
Chris@18 419 $file_new = $uuid_new ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_new) : FALSE;
Chris@0 420
Chris@0 421 if ($uuid_new != $uuid_old) {
Chris@0 422
Chris@0 423 // Is there a new file?
Chris@0 424 if ($file_new) {
Chris@0 425 $file_new->status = FILE_STATUS_PERMANENT;
Chris@0 426 $file_new->save();
Chris@0 427 \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_storage->uuid());
Chris@0 428 }
Chris@0 429
Chris@0 430 // Is there an old file?
Chris@18 431 if ($uuid_old && ($file_old = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_old))) {
Chris@0 432 \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid());
Chris@0 433 }
Chris@0 434 }
Chris@0 435
Chris@0 436 // If the upload destination changed, then move the file.
Chris@0 437 if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) {
Chris@0 438 $directory = $field_storage->getSetting('uri_scheme') . '://default_images/';
Chris@18 439 \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
Chris@0 440 file_move($file_new, $directory . $file_new->getFilename());
Chris@0 441 }
Chris@0 442 }
Chris@0 443
Chris@0 444 /**
Chris@0 445 * Implements hook_ENTITY_TYPE_update() for 'field_config'.
Chris@0 446 */
Chris@0 447 function image_field_config_update(FieldConfigInterface $field) {
Chris@0 448 $field_storage = $field->getFieldStorageDefinition();
Chris@0 449 if ($field_storage->getType() != 'image') {
Chris@0 450 // Only act on image fields.
Chris@0 451 return;
Chris@0 452 }
Chris@0 453
Chris@0 454 $prior_instance = $field->original;
Chris@0 455
Chris@0 456 $uuid_new = $field->getSetting('default_image')['uuid'];
Chris@0 457 $uuid_old = $prior_instance->getSetting('default_image')['uuid'];
Chris@0 458
Chris@0 459 // If the old and new files do not match, update the default accordingly.
Chris@18 460 $file_new = $uuid_new ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_new) : FALSE;
Chris@0 461 if ($uuid_new != $uuid_old) {
Chris@0 462 // Save the new file, if present.
Chris@0 463 if ($file_new) {
Chris@0 464 $file_new->status = FILE_STATUS_PERMANENT;
Chris@0 465 $file_new->save();
Chris@0 466 \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid());
Chris@0 467 }
Chris@0 468 // Delete the old file, if present.
Chris@18 469 if ($uuid_old && ($file_old = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid_old))) {
Chris@0 470 \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid());
Chris@0 471 }
Chris@0 472 }
Chris@0 473
Chris@0 474 // If the upload destination changed, then move the file.
Chris@0 475 if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) {
Chris@0 476 $directory = $field_storage->getSetting('uri_scheme') . '://default_images/';
Chris@18 477 \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
Chris@0 478 file_move($file_new, $directory . $file_new->getFilename());
Chris@0 479 }
Chris@0 480 }
Chris@0 481
Chris@0 482 /**
Chris@0 483 * Implements hook_ENTITY_TYPE_delete() for 'field_storage_config'.
Chris@0 484 */
Chris@0 485 function image_field_storage_config_delete(FieldStorageConfigInterface $field) {
Chris@0 486 if ($field->getType() != 'image') {
Chris@0 487 // Only act on image fields.
Chris@0 488 return;
Chris@0 489 }
Chris@0 490
Chris@0 491 // The value of a managed_file element can be an array if #extended == TRUE.
Chris@0 492 $uuid = $field->getSetting('default_image')['uuid'];
Chris@18 493 if ($uuid && ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid))) {
Chris@0 494 \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid());
Chris@0 495 }
Chris@0 496 }
Chris@0 497
Chris@0 498 /**
Chris@0 499 * Implements hook_ENTITY_TYPE_delete() for 'field_config'.
Chris@0 500 */
Chris@0 501 function image_field_config_delete(FieldConfigInterface $field) {
Chris@0 502 $field_storage = $field->getFieldStorageDefinition();
Chris@0 503 if ($field_storage->getType() != 'image') {
Chris@0 504 // Only act on image fields.
Chris@0 505 return;
Chris@0 506 }
Chris@0 507
Chris@0 508 // The value of a managed_file element can be an array if #extended == TRUE.
Chris@0 509 $uuid = $field->getSetting('default_image')['uuid'];
Chris@0 510
Chris@0 511 // Remove the default image when the instance is deleted.
Chris@18 512 if ($uuid && ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid))) {
Chris@0 513 \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid());
Chris@0 514 }
Chris@0 515 }