You are here

function photos_preprocess_photos_image_html in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 photos.module \photos_preprocess_photos_image_html()
  2. 8.4 photos.module \photos_preprocess_photos_image_html()

Implements hook_preprocess_HOOK().

File

./photos.module, line 907
Implementation of photos.module.

Code

function photos_preprocess_photos_image_html(&$variables, $hook) {
  $styleName = $variables['style_name'];
  $image = $variables['image'];
  if (isset($image['uri'])) {
    $uri = $image['uri'];
  }
  else {
    $uri = $image['file']
      ->getFileUri();
  }
  $title = $image['title'];
  $alt = isset($image['alt']) ? strip_tags($image['alt']) : $title;
  if ($styleName == 'photos_original') {
    $image_styles = image_style_options(FALSE);
    if (isset($image_styles['photos_original'])) {

      // Original image style override.
      // Render image view.
      $image_view_array = [
        '#theme' => 'image_style',
        '#style_name' => 'photos_original',
        '#uri' => $uri,
        '#title' => $title,
        '#alt' => $alt,
      ];
    }
    else {

      // Original image.
      $image_view_array = [
        '#theme' => 'image',
        '#uri' => $uri,
        '#width' => $image['width'],
        '#height' => $image['height'],
        '#title' => $title,
        '#alt' => $alt,
      ];
    }
  }
  else {

    // Check scheme and prep image.
    $scheme = \Drupal::service('stream_wrapper_manager')
      ->getScheme($uri);
    $styleUri = FALSE;

    // If private create temporary derivative.
    if ($scheme == 'private') {

      // @todo update this, get width and height.
      $photos_image = new PhotosImageFile($image['file']
        ->id());
      $url = $photos_image
        ->derivative($uri, $styleName, $scheme);

      // Do not use filename as alt or title with private files.
      $filename = $image['file']
        ->getFilename();

      // @todo check if alt or title are filename without -, _, extension etc.
      $titleTest = pathinfo($filename, PATHINFO_FILENAME);
      if ($alt == $filename || $alt == $titleTest) {
        $alt = '';
      }
      if ($title == $filename || $title == $titleTest) {
        $title = '';
      }
    }
    else {

      // Public and all other images.
      $style = ImageStyle::load($styleName);
      $styleUri = $style
        ->buildUri($uri);
      if (!file_exists($styleUri)) {
        $style
          ->createDerivative($uri, $styleUri);
      }
      $url = file_create_url($styleUri);
    }

    // Render image view.
    $image_view_array = [
      '#theme' => 'image',
      '#uri' => $url,
      '#title' => $title,
      '#alt' => $alt,
    ];
    if ($styleUri) {
      $imageData = \Drupal::service('image.factory')
        ->get($styleUri);

      // Check if valid image.
      if ($imageData
        ->isValid()) {
        $image_view_array['#width'] = $imageData
          ->getWidth();
        $image_view_array['#height'] = $imageData
          ->getHeight();
      }
    }
  }

  // @todo fix view original link.
  $variables['image']['view'] = $image_view_array;
}