You are here

function template_preprocess_insert_image in Insert 8

Same name and namespace in other branches
  1. 8.2 insert.module \template_preprocess_insert_image()
  2. 6 includes/insert.inc \template_preprocess_insert_image()
  3. 7 includes/insert.inc \template_preprocess_insert_image()

Preprocess variables for the insert-image.html.twig file. The function is called for each image style configured to be used. The particular image is transformed according to the image style.

File

./insert.module, line 209

Code

function template_preprocess_insert_image(&$vars) {
  if (count($vars['item']['fids']) === 0) {
    return;
  }
  $vars['file'] = File::load($vars['item']['fids'][0]);

  /** @var \Drupal\Core\Image\ImageInterface $image */
  $image = \Drupal::service('image.factory')
    ->get($vars['file']
    ->getFileUri());
  if ($image
    ->isValid()) {
    $vars['width'] = $image
      ->getWidth();
    $vars['height'] = $image
      ->getHeight();
  }
  else {
    $vars['width'] = $variables['height'] = NULL;
  }
  if ($vars['style_name'] === 'image') {

    // Preprocessing template for inserting original image.
    return;
  }
  $style = ImageStyle::load($vars['style_name']);
  if ($style === null) {
    return;
  }
  $style
    ->transformDimensions($vars, $vars['file']
    ->getFileUri());
}