You are here

public function PhotosAlbum::nodeView in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 src/PhotosAlbum.php \Drupal\photos\PhotosAlbum::nodeView()
  2. 6.0.x src/PhotosAlbum.php \Drupal\photos\PhotosAlbum::nodeView()

Page and Teaser display settings.

File

src/PhotosAlbum.php, line 37

Class

PhotosAlbum
Create an album object.

Namespace

Drupal\photos

Code

public function nodeView($node, $display, $view_mode) {
  $album = [];
  $order = 0;
  $style_name = $default_style = 'medium';
  if ($display != 0) {
    $default_order = \Drupal::config('photos.settings')
      ->get('photos_display_imageorder');
    $order = explode('|', isset($node->album['imageorder']) ? $node->album['imageorder'] : $default_order);
    $order = PhotosAlbum::orderValueChange($order[0], $order[1]);
    $default_style = \Drupal::config('photos.settings')
      ->get('photos_display_' . $view_mode . '_imagesize') ?: 'thumbnail';
    $style_name = isset($node->album[$view_mode . '_imagesize']) ? $node->album[$view_mode . '_imagesize'] : $default_style;
  }
  switch ($display) {
    case 0:

      // Display none.
      break;
    case 1:

      // Display cover.
      $album = [];
      if (isset($node->album['cover'])) {
        if (isset($node->album['cover']['view'])) {
          $album = $node->album['cover']['view'];
        }
        else {
          if (isset($node->album['cover']['uri'])) {
            $image = new \stdClass();
            $variables = [
              'style_name' => $style_name,
              'uri' => $node->album['cover']['uri'],
            ];

            // The image.factory service will check if our image is valid.
            $image_info = \Drupal::service('image.factory')
              ->get($node->album['cover']['uri']);
            if ($image_info
              ->isValid()) {
              $variables['width'] = $image_info
                ->getWidth();
              $variables['height'] = $image_info
                ->getHeight();
            }
            else {
              $variables['width'] = $variables['height'] = NULL;
            }
            $image_render_array = [
              '#theme' => 'image_style',
              '#uri' => $node->album['cover']['uri'],
              '#style_name' => $style_name,
            ];
            $image->view = $image_render_array;
            $image->href = 'photos/album/' . $node
              ->id();
            $image->uri = $node->album['cover']['uri'];
            $image->pid = $node
              ->id();
            $image->title = $node
              ->getTitle();
            $image_render_array = [
              '#theme' => 'photos_image_html',
              '#image' => $image,
              '#style_name' => $style_name,
            ];
            $album = $image_render_array;
          }
        }
      }
      break;
    case 2:

      // Display thumbnails.
      $get_field = \Drupal::request()->query
        ->get('field');
      $get_sort = \Drupal::request()->query
        ->get('sort');
      $column = $get_field ? Html::escape($get_field) : 0;
      $sort = $get_sort ? Html::escape($get_sort) : 0;
      $view_num = \Drupal::config('photos.settings')
        ->get('photos_display_' . $view_mode . '_viewnum') ?: 10;
      $limit = isset($node->album[$view_mode . '_viewnum']) ? $node->album[$view_mode . '_viewnum'] : $view_num;
      $term = PhotosAlbum::orderValue($column, $sort, $limit, $order);
      $db = \Drupal::database();
      $query = $db
        ->select('file_managed', 'f');
      $query
        ->join('photos_image', 'p', 'p.fid = f.fid');
      $query
        ->fields('f', [
        'fid',
      ]);
      $query
        ->condition('p.pid', $node
        ->id());
      $query
        ->orderBy($term['order']['column'], $term['order']['sort']);
      $query
        ->range(0, $term['limit']);
      $result = $query
        ->execute();
      $i = 0;

      // Necessary when upgrading from D6 to D7.
      $image_styles = image_style_options(FALSE);
      if (!isset($image_styles[$style_name])) {
        $style_name = \Drupal::config('photos.settings')
          ->get('photos_display_teaser_imagesize');
      }
      $album = [];

      // Thumbnails.
      foreach ($result as $data) {
        $photos_image = new PhotosImage($data->fid);
        $variables = [
          'href' => 'photos/image/' . $data->fid,
        ];
        $album[] = $photos_image
          ->view($style_name, $variables);
        ++$i;
      }
      break;
    case 3:

      // Get cover.
      $cover = FALSE;
      if (isset($node->album['cover']) && isset($node->album['cover']['uri'])) {
        $image_render_array = [
          '#theme' => 'image_style',
          '#style_name' => $style_name,
          '#uri' => $node->album['cover']['uri'],
          '#title' => $node
            ->getTitle(),
          '#alt' => $node
            ->getTitle(),
        ];
        $cover = $image_render_array;
      }
      if ($cover) {

        // Cover with colorbox gallery.
        $get_field = \Drupal::request()->query
          ->get('field');
        $get_sort = \Drupal::request()->query
          ->get('sort');
        $column = $get_field ? Html::escape($get_field) : 0;
        $sort = $get_sort ? Html::escape($get_sort) : 0;
        $view_num = \Drupal::config('photos.settings')
          ->get('photos_display_' . $view_mode . '_viewnum') ?: 10;
        $limit = FALSE;

        // Query all images in gallery.
        $term = PhotosAlbum::orderValue($column, $sort, $limit, $order);
        $db = \Drupal::database();
        $query = $db
          ->select('file_managed', 'f');
        $query
          ->join('photos_image', 'p', 'p.fid = f.fid');
        $query
          ->join('users_field_data', 'ufd', 'ufd.uid = f.uid');
        $query
          ->fields('f', [
          'uri',
          'filemime',
          'created',
          'filename',
          'filesize',
        ])
          ->fields('p')
          ->fields('ufd', [
          'uid',
          'name',
        ]);
        $query
          ->condition('p.pid', $node
          ->id());
        $query
          ->orderBy($term['order']['column'], $term['order']['sort']);
        $result = $query
          ->execute();
        $i = 0;

        // Setup colorbox.
        if (\Drupal::moduleHandler()
          ->moduleExists('colorbox')) {
          $style = \Drupal::config('colorbox.settings')
            ->get('custom.style');
          $album['#attached']['library'] = [
            'colorbox/colorbox',
            'colorbox/' . $style,
          ];
          $colorbox_height = \Drupal::config('photos.settings')
            ->get('photos_display_colorbox_max_height') ?: 100;
          $colorbox_width = \Drupal::config('photos.settings')
            ->get('photos_display_colorbox_max_width') ?: 50;
          $js_settings = [
            'maxWidth' => $colorbox_width . '%',
            'maxHeight' => $colorbox_height . '%',
          ];
          $album['#attached']['drupalSettings']['colorbox'] = $js_settings;
        }

        // Display cover and list colorbox image links.
        foreach ($result as $data) {
          $style_name = isset($node->album['view_imagesize']) ? $node->album['view_imagesize'] : $style_name;
          $style = ImageStyle::load($style_name);
          $file_url = $style
            ->buildUrl($data->uri);
          $image = NULL;
          if ($i == 0) {
            $image = $cover;
          }
          $album[] = [
            '#theme' => 'photos_image_colorbox_link',
            '#image' => $image,
            '#image_title' => $data->title,
            '#image_url' => $file_url,
            '#nid' => $node
              ->id(),
          ];
          ++$i;
        }
      }
      break;
  }
  return $album;
}