You are here

public function CollageFormatter::collageformatter_render_collage in Collage Formatter 8

Returns renderable array of collages.

1 call to CollageFormatter::collageformatter_render_collage()
CollageFormatter::viewElements in src/Plugin/Field/FieldFormatter/CollageFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/CollageFormatter.php, line 446
Contains \Drupal\collageformatter\src\Plugin\Field\FieldFormatter\CollageFormatter.

Class

CollageFormatter
Plugin implementation of the 'collageformatter' formatter.

Namespace

Drupal\collageformatter\Plugin\Field\FieldFormatter

Code

public function collageformatter_render_collage($images, $settings) {
  $collage = [];

  // Remove images to skip
  if ($settings['images_to_skip']) {
    $images = array_slice($images, $settings['images_to_skip']);
  }

  // Prepare images.
  foreach ($images as $delta => $image) {
    $image_properties = $image->_referringItem;
    if (!isset($image_properties->width) || !isset($image_properties->height)) {
      $image += $image_info = $image
        ->getFileUri() ?? $image_info;
    }
    $image_properties
      ->set('uri', $image
      ->getFileUri());
    $image_properties
      ->set('content_uri', '/node/1');
    $image_properties
      ->set('box_type', 'image');
    $image_properties
      ->set('delta', $delta);
    $image_properties
      ->set('total_width', $image_properties->width + 2 * $settings['border_size'] + $settings['gap_size']);
    $image_properties
      ->set('total_height', $image_properties->height + 2 * $settings['border_size'] + $settings['gap_size']);

    // NOTE: Currently setting values individually. Find a way to set the whole array to the configs. Look into $image_properties->setValue($values)
    // $image_propertiesValues = $image_properties->getValue();
    // $image_propertiesValues += [
    //   'box_type' => 'image',
    //   'delta' => $delta,
    //   'total_width' => $image_properties->width + 2 * $settings['border_size'] + $settings['gap_size'],
    //   'total_height' => $image_properties->height + (2 * $settings['border_size']) + $settings['gap_size'],
    // ];
  }

  // Determine the number of collages and how many images to include in a collage
  $collage_number = $settings['collage_number'];
  $images_per_collage = $settings['images_per_collage'] ? $settings['images_per_collage'] : round(count($images) / $collage_number);

  // Generate collages.
  while ($collage_number > 0) {
    $collage_number--;

    // If last collage and all images option - take all images.
    if ($collage_number == 0 && !$settings['images_per_collage']) {
      $collage_images = $images;
    }
    else {
      $collage_images = array_slice($images, 0, $images_per_collage);

      // Update images array and set as the last collage if there are no more images.
      if (!($images = array_slice($images, $images_per_collage))) {
        $collage_number = 0;
      }
    }

    // Generate collage layout.
    $box = $this
      ->collageformatter_layout_box($collage_images, $settings['collage_orientation']);

    // Scale the collage.
    if ($settings['collage_width']) {
      $box['parent_total_width'] = $settings['collage_width'] - 2 * $settings['collage_border_size'];
      $dimensions = [
        'width' => $box['parent_total_width'] - $settings['gap_size'],
      ];
      $box = $this
        ->collageformatter_scale_box($box, $dimensions);
      $box['parent_total_height'] = $box['total_height'] + $settings['gap_size'];
    }
    elseif ($settings['collage_height']) {
      $box['parent_total_height'] = $settings['collage_height'] - 2 * $settings['collage_border_size'];
      $dimensions = [
        'height' => $box['parent_total_height'] - $settings['gap_size'],
      ];
      $box = $this
        ->collageformatter_scale_box($box, $dimensions);
      $box['parent_total_width'] = $box['total_width'] + $settings['gap_size'];
    }
    else {
      $box['parent_total_width'] = $box['total_width'] + $settings['gap_size'];
      $box['parent_total_height'] = $box['total_height'] + $settings['gap_size'];
    }

    // Resize the collage if both with and height are set.
    if ($settings['collage_width'] && $settings['collage_height']) {
      $box['parent_total_width'] = $settings['collage_width'] - 2 * $settings['collage_border_size'];
      $box['parent_total_height'] = $settings['collage_height'] - 2 * $settings['collage_border_size'];
      $dimensions = [
        'width' => $box['parent_total_width'] - $settings['gap_size'],
        'height' => $box['parent_total_height'] - $settings['gap_size'],
      ];
      $box = $this
        ->collageformatter_resize_box($box, $dimensions);
    }

    // Check for upscaled images and prevent upscaling.
    if ($settings['prevent_upscale']) {
      $scale = $this
        ->collageformatter_upscaling_check($box, $settings);
      if ($scale < 1) {
        $dimensions = [
          'width' => $scale * $box['total_width'],
        ];
        $box = $this
          ->collageformatter_scale_box($box, $dimensions);
        $box['parent_total_width'] = $box['total_width'] + $settings['gap_size'];
        $box['parent_total_height'] = $box['total_height'] + $settings['gap_size'];
      }
    }
    $collage_wrapper_style = [];
    $collage_wrapper_style[] = 'max-width: ' . round($box['parent_total_width'] + 2 * $settings['collage_border_size'] - 0.5) . 'px;';

    // $collage_wrapper_style[] = 'box-sizing: border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box;';
    $collage_style = [];

    // $collage_style[] = 'box-sizing: border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box;';
    if ($settings['collage_border_size']) {
      $border = 'border: ' . $settings['collage_border_size'] . 'px solid';
      $border .= $settings['collage_border_color'] ? ' ' . $settings['collage_border_color'] : '';
      $collage_style[] = $border . ';';
    }
    if ($settings['gap_color']) {
      $collage_style[] = 'background-color: ' . $settings['gap_color'] . ';';
    }
    $collage_wrapper_class = [
      'collageformatter-collage-wrapper',
    ];
    if ($settings['image_link_modal'] == 'photoswipe') {
      $collage_wrapper_class[] = 'photoswipe-gallery';
    }
    $collage[] = [
      '#theme' => 'collageformatter_collage',
      '#collage' => $this
        ->collageformatter_render_box($box, $settings),
      '#collage_wrapper_class' => implode(' ', $collage_wrapper_class),
      '#collage_wrapper_style' => implode(' ', $collage_wrapper_style),
      '#collage_style' => implode(' ', $collage_style),
      '#collage_bottom_style' => 'clear: both; margin-bottom: ' . 100 * ($settings['gap_size'] / round($box['parent_total_width'] - 0.5)) . '%',
    ];
  }
  return $collage;
}