You are here

public function PhotosAlbum::nodeView in Album Photos 6.0.x

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

Page and Teaser display settings.

File

src/PhotosAlbum.php, line 42

Class

PhotosAlbum
Create an album object.

Namespace

Drupal\photos

Code

public function nodeView($node, $display, $view_mode) {

  // @todo convert to field api. Preserve for legacy mode.
  $album = [];
  $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.
      // @todo get photos_image id and load cover display.
      // @todo add field setting option to link to album...
      $render_photos_image = [];
      if (isset($node->album['cover'])) {
        $render_photos_image = $node->album['cover'];
      }
      else {
        $db = \Drupal::database();
        $cover_id = $db
          ->query('SELECT cover_id FROM {photos_album} WHERE album_id = :nid', [
          ':nid' => $node
            ->id(),
        ])
          ->fetchField();
        if ($cover_id) {
          $photos_image = \Drupal::entityTypeManager()
            ->getStorage('photos_image')
            ->load($cover_id);
          if ($photos_image) {

            // @todo add setting to override cover view_mode?
            $render_photos_image = \Drupal::entityTypeManager()
              ->getViewBuilder('photos_image')
              ->view($photos_image, 'cover');
          }
        }
      }
      return $render_photos_image;
    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');

      // @note currently legacy mode requires default field_image.
      $query
        ->join('photos_image__field_image', 'i', 'i.field_image_target_id = f.fid');
      $query
        ->join('photos_image_field_data', 'p', 'p.revision_id = i.revision_id');
      $query
        ->fields('f', [
        'fid',
      ]);
      $query
        ->condition('p.album_id', $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');
      }

      // @todo this can use a teaser display mode, but we want to keep legacy
      // support, so if fid can be found from a file or image field that will
      // be a nice backup option.
      $album = [];

      // Thumbnails.
      foreach ($result as $data) {
        $photos_image = new PhotosImageFile($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');

        // @todo p.fid will fail.
        $query
          ->join('photos_image_field_data', '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.album_id', $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;
}