moduleExists('field_ui') ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#'; $output = ''; $output .= '
' . t('The Image module allows you to create fields that contain image files and to configure Image styles that can be used to manipulate the display of images. See the Field module help and the Field UI help pages for terminology and general information on entities, fields, and how to create and manage fields. For more information, see the online documentation for the Image module.', [':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']) . '
'; $output .= '' . 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.') . '
'; case 'image.effect_add_form': $effect = \Drupal::service('plugin.manager.image.effect')->getDefinition($route_match->getParameter('image_effect')); return isset($effect['description']) ? ('' . $effect['description'] . '
') : NULL; case 'image.effect_edit_form': $effect = $route_match->getParameter('image_style')->getEffect($route_match->getParameter('image_effect')); $effect_definition = $effect->getPluginDefinition(); return isset($effect_definition['description']) ? ('' . $effect_definition['description'] . '
') : NULL; } } /** * Implements hook_theme(). */ function image_theme() { return [ // Theme functions in image.module. 'image_style' => [ // HTML 4 and XHTML 1.0 always require an alt attribute. The HTML 5 draft // allows the alt attribute to be omitted in some cases. Therefore, // default the alt attribute to an empty string, but allow code using // '#theme' => 'image_style' to pass explicit NULL for it to be omitted. // Usually, neither omission nor an empty string satisfies accessibility // requirements, so it is strongly encouraged for code using '#theme' => // 'image_style' to pass a meaningful value for the alt variable. // - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8 // - http://www.w3.org/TR/xhtml1/dtds.html // - http://dev.w3.org/html5/spec/Overview.html#alt // The title attribute is optional in all cases, so it is omitted by // default. 'variables' => [ 'style_name' => NULL, 'uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => [], ], ], // Theme functions in image.admin.inc. 'image_style_preview' => [ 'variables' => ['style' => NULL], 'file' => 'image.admin.inc', ], 'image_anchor' => [ 'render element' => 'element', 'file' => 'image.admin.inc', ], 'image_resize_summary' => [ 'variables' => ['data' => NULL, 'effect' => []], ], 'image_scale_summary' => [ 'variables' => ['data' => NULL, 'effect' => []], ], 'image_crop_summary' => [ 'variables' => ['data' => NULL, 'effect' => []], ], 'image_rotate_summary' => [ 'variables' => ['data' => NULL, 'effect' => []], ], // Theme functions in image.field.inc. 'image_widget' => [ 'render element' => 'element', 'file' => 'image.field.inc', ], 'image_formatter' => [ 'variables' => ['item' => NULL, 'item_attributes' => NULL, 'url' => NULL, 'image_style' => NULL], 'file' => 'image.field.inc', ], ]; } /** * Implements hook_file_download(). * * Control the access to files underneath the styles directory. */ function image_file_download($uri) { $path = file_uri_target($uri); // Private file access for image style derivatives. if (strpos($path, 'styles/') === 0) { $args = explode('/', $path); // Discard "styles", style name, and scheme from the path $args = array_slice($args, 3); // Then the remaining parts are the path to the image. $original_uri = file_uri_scheme($uri) . '://' . implode('/', $args); // Check that the file exists and is an image. $image = \Drupal::service('image.factory')->get($uri); if ($image->isValid()) { // Check the permissions of the original to grant access to this image. $headers = \Drupal::moduleHandler()->invokeAll('file_download', [$original_uri]); // Confirm there's at least one module granting access and none denying access. if (!empty($headers) && !in_array(-1, $headers)) { return [ // Send headers describing the image's size, and MIME-type. 'Content-Type' => $image->getMimeType(), 'Content-Length' => $image->getFileSize(), // By not explicitly setting them here, this uses normal Drupal // Expires, Cache-Control and ETag headers to prevent proxy or // browser caching of private images. ]; } } return -1; } } /** * Implements hook_file_move(). */ function image_file_move(FileInterface $file, FileInterface $source) { // Delete any image derivatives at the original image path. image_path_flush($source->getFileUri()); } /** * Implements hook_ENTITY_TYPE_predelete() for file entities. */ function image_file_predelete(FileInterface $file) { // Delete any image derivatives of this image. image_path_flush($file->getFileUri()); } /** * Clears cached versions of a specific file in all styles. * * @param $path * The Drupal file path to the original image. */ function image_path_flush($path) { $styles = ImageStyle::loadMultiple(); foreach ($styles as $style) { $style->flush($path); } } /** * Gets an array of image styles suitable for using as select list options. * * @param $include_empty * If TRUE a '- None -' option will be inserted in the options array. * @return * Array of image styles both key and value are set to style name. */ function image_style_options($include_empty = TRUE) { $styles = ImageStyle::loadMultiple(); $options = []; if ($include_empty && !empty($styles)) { $options[''] = t('- None -'); } foreach ($styles as $name => $style) { $options[$name] = $style->label(); } if (empty($options)) { $options[''] = t('No defined styles'); } return $options; } /** * Prepares variables for image style templates. * * Default template: image-style.html.twig. * * @param array $variables * An associative array containing: * - width: The width of the image. * - height: The height of the image. * - style_name: The name of the image style to be applied. * - attributes: Additional attributes to apply to the image. * - uri: URI of the source image before styling. * - alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0 * always require an alt attribute. The HTML 5 draft allows the alt * attribute to be omitted in some cases. Therefore, this variable defaults * to an empty string, but can be set to NULL for the attribute to be * omitted. Usually, neither omission nor an empty string satisfies * accessibility requirements, so it is strongly encouraged for code using * '#theme' => 'image_style' to pass a meaningful value for this variable. * - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8 * - http://www.w3.org/TR/xhtml1/dtds.html * - http://dev.w3.org/html5/spec/Overview.html#alt * - title: The title text is displayed when the image is hovered in some * popular browsers. * - attributes: Associative array of attributes to be placed in the img tag. */ function template_preprocess_image_style(&$variables) { $style = ImageStyle::load($variables['style_name']); // Determine the dimensions of the styled image. $dimensions = [ 'width' => $variables['width'], 'height' => $variables['height'], ]; $style->transformDimensions($dimensions, $variables['uri']); $variables['image'] = [ '#theme' => 'image', '#width' => $dimensions['width'], '#height' => $dimensions['height'], '#attributes' => $variables['attributes'], '#style_name' => $variables['style_name'], ]; // If the current image toolkit supports this file type, prepare the URI for // the derivative image. If not, just use the original image resized to the // dimensions specified by the style. if ($style->supportsUri($variables['uri'])) { $variables['image']['#uri'] = $style->buildUrl($variables['uri']); } else { $variables['image']['#uri'] = $variables['uri']; // Don't render the image by default, but allow other preprocess functions // to override that if they need to. $variables['image']['#access'] = FALSE; // Inform the site builders why their image didn't work. \Drupal::logger('image')->warning('Could not apply @style image style to @uri because the style does not support it.', [ '@style' => $style->label(), '@uri' => $variables['uri'], ]); } if (isset($variables['alt']) || array_key_exists('alt', $variables)) { $variables['image']['#alt'] = $variables['alt']; } if (isset($variables['title']) || array_key_exists('title', $variables)) { $variables['image']['#title'] = $variables['title']; } } /** * Accepts a keyword (center, top, left, etc) and returns it as a pixel offset. * * @param $value * @param $current_pixels * @param $new_pixels */ function image_filter_keyword($value, $current_pixels, $new_pixels) { switch ($value) { case 'top': case 'left': return 0; case 'bottom': case 'right': return $current_pixels - $new_pixels; case 'center': return $current_pixels / 2 - $new_pixels / 2; } return $value; } /** * Implements hook_entity_presave(). * * Transforms default image of image field from array into single value at save. */ function image_entity_presave(EntityInterface $entity) { // Get the default image settings, return if not saving an image field storage // or image field entity. $default_image = []; if (($entity instanceof FieldStorageConfigInterface || $entity instanceof FieldConfigInterface) && $entity->getType() == 'image') { $default_image = $entity->getSetting('default_image'); } else { return; } if ($entity->isSyncing()) { return; } $uuid = $default_image['uuid']; if ($uuid) { $original_uuid = isset($entity->original) ? $entity->original->getSetting('default_image')['uuid'] : NULL; if ($uuid != $original_uuid) { $file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid); if ($file) { $image = \Drupal::service('image.factory')->get($file->getFileUri()); $default_image['width'] = $image->getWidth(); $default_image['height'] = $image->getHeight(); } else { $default_image['uuid'] = NULL; } } } // Both FieldStorageConfigInterface and FieldConfigInterface have a // setSetting() method. $entity->setSetting('default_image', $default_image); } /** * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'. */ function image_field_storage_config_update(FieldStorageConfigInterface $field_storage) { if ($field_storage->getType() != 'image') { // Only act on image fields. return; } $prior_field_storage = $field_storage->original; // The value of a managed_file element can be an array if #extended == TRUE. $uuid_new = $field_storage->getSetting('default_image')['uuid']; $uuid_old = $prior_field_storage->getSetting('default_image')['uuid']; $file_new = $uuid_new ? \Drupal::entityManager()->loadEntityByUuid('file', $uuid_new) : FALSE; if ($uuid_new != $uuid_old) { // Is there a new file? if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_storage->uuid()); } // Is there an old file? if ($uuid_old && ($file_old = \Drupal::entityManager()->loadEntityByUuid('file', $uuid_old))) { \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid()); } } // If the upload destination changed, then move the file. if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) { $directory = $field_storage->getSetting('uri_scheme') . '://default_images/'; file_prepare_directory($directory, FILE_CREATE_DIRECTORY); file_move($file_new, $directory . $file_new->getFilename()); } } /** * Implements hook_ENTITY_TYPE_update() for 'field_config'. */ function image_field_config_update(FieldConfigInterface $field) { $field_storage = $field->getFieldStorageDefinition(); if ($field_storage->getType() != 'image') { // Only act on image fields. return; } $prior_instance = $field->original; $uuid_new = $field->getSetting('default_image')['uuid']; $uuid_old = $prior_instance->getSetting('default_image')['uuid']; // If the old and new files do not match, update the default accordingly. $file_new = $uuid_new ? \Drupal::entityManager()->loadEntityByUuid('file', $uuid_new) : FALSE; if ($uuid_new != $uuid_old) { // Save the new file, if present. if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid()); } // Delete the old file, if present. if ($uuid_old && ($file_old = \Drupal::entityManager()->loadEntityByUuid('file', $uuid_old))) { \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid()); } } // If the upload destination changed, then move the file. if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) { $directory = $field_storage->getSetting('uri_scheme') . '://default_images/'; file_prepare_directory($directory, FILE_CREATE_DIRECTORY); file_move($file_new, $directory . $file_new->getFilename()); } } /** * Implements hook_ENTITY_TYPE_delete() for 'field_storage_config'. */ function image_field_storage_config_delete(FieldStorageConfigInterface $field) { if ($field->getType() != 'image') { // Only act on image fields. return; } // The value of a managed_file element can be an array if #extended == TRUE. $uuid = $field->getSetting('default_image')['uuid']; if ($uuid && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid))) { \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid()); } } /** * Implements hook_ENTITY_TYPE_delete() for 'field_config'. */ function image_field_config_delete(FieldConfigInterface $field) { $field_storage = $field->getFieldStorageDefinition(); if ($field_storage->getType() != 'image') { // Only act on image fields. return; } // The value of a managed_file element can be an array if #extended == TRUE. $uuid = $field->getSetting('default_image')['uuid']; // Remove the default image when the instance is deleted. if ($uuid && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid))) { \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid()); } }