You are here

function CollageFormatter::collageformatter_render_box in Collage Formatter 8

Recursive function to render the box.

1 call to CollageFormatter::collageformatter_render_box()
CollageFormatter::collageformatter_render_collage in src/Plugin/Field/FieldFormatter/CollageFormatter.php
Returns renderable array of collages.

File

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

Class

CollageFormatter
Plugin implementation of the 'collageformatter' formatter.

Namespace

Drupal\collageformatter\Plugin\Field\FieldFormatter

Code

function collageformatter_render_box($box, $settings) {
  $output = '';

  // Check if parent dimensions changed - and change yourself.
  if (array_key_exists('parent_box_orientation', $box)) {
    if ($box['parent_box_orientation'] == 'vertical') {
      $box['total_width'] = $box['parent_total_width'];
    }
    elseif ($box['parent_box_orientation'] == 'horizontal') {
      $box['total_height'] = $box['parent_total_height'];
    }
  }

  // Perform pixel check. // Pixel check has to be fixed!
  if ($box['pixel_check']) {
    if ($box['parent_box_orientation'] == 'vertical') {
      $pixels = round($box['parent_total_height'] - 0.5) - round($box['total_height'] - 0.5) - round($box['siblings_total_height'] - 0.5);
      if ($pixels) {
        $box['total_height'] += $pixels;
      }
    }
    if ($box['parent_box_orientation'] == 'horizontal') {
      $pixels = round($box['parent_total_width'] - 0.5) - round($box['total_width'] - 0.5) - round($box['siblings_total_width'] - 0.5);
      if ($pixels) {
        $box['total_width'] += $pixels;
      }
    }
  }

  // Ensure that children have correct parent dimensions.
  if ($box['box_type'] == 'box') {
    $box[1]['parent_total_height'] = $box[2]['parent_total_height'] = $box['total_height'];
    $box[1]['parent_total_width'] = $box[2]['parent_total_width'] = $box['total_width'];
  }
  if ($box['box_type'] == 'box') {
    $box_style = [
      'float: left;',
      'max-width: ' . round($box['total_width'] - 0.5) . 'px;',
    ];
    $box_style[] = 'width: ' . 100 * (round($box['total_width'] - 0.5) / round($box['parent_total_width'] - 0.5)) . '%;';
    $content[] = $this
      ->collageformatter_render_box($box[1], $settings);
    $content[] = $this
      ->collageformatter_render_box($box[2], $settings);
    $output = [
      '#theme' => 'collageformatter_collage_box',
      '#box' => $content,
      '#box_style' => implode(' ', $box_style),
    ];
  }
  elseif ($box['box_type'] == 'image') {
    $image_uri = $this
      ->collageformatter_image_file_check($box, $settings);
    $style = ImageStyle::load('collageformatter');
    $image_style = [
      'display: block;',
      'max-width: 100%;',
      'height: auto;',
      'margin: 0;',
    ];

    // $image = [
    //    '#theme' => 'image_style',
    //    '#style_name' => 'thumbnail',
    //    '#path' => $image_uri,
    //    '#alt' => $box['alt'],
    //    '#title' => $box['title'],
    //    '#attributes' => ['style' => implode(' ', $image_style)],
    //  ];
    // FIXME: This has to be rendered programmatically.
    $image = '<img style="' . implode(' ', $image_style) . '" typeof="foaf:Image" src="' . file_create_url($image_uri) . '" alt="' . $box['alt'] . '" title="' . $box['title'] . '" />';

    // Create image derivatives.
    if ($settings['generate_image_derivatives']) {
      $derivative_uri = $style
        ->buildUri($image_uri);
      if (!file_exists($derivative_uri)) {
        if (!$style
          ->createDerivative(file_create_url($image_uri), $derivative_uri)) {
          \Drupal::logger('collageformatter')
            ->notice('Unable to generate the derived image located at %path.', [
            '%path' => $derivative_uri,
          ]);
        }
      }
    }
    $attached = [];

    // Process image linking and modal gallery settings.
    if ($settings['image_link'] == 'content') {
      $class = $settings['image_link_class'] ? [
        $settings['image_link_class'],
      ] : [];
      $rel = $settings['image_link_rel'];

      // $image = Link::fromTextAndUrl($image,
      //           Url::fromUri('/node/1', [
      //                         'attributes' => [
      //                           'title' => $box['title'],
      //                           'class' => $class,
      //                           'rel' => $rel,
      //                         ],
      //                         'html' => TRUE,
      //                       ]))->toString();
      // FIXME: Render HTML programmatically
      $image = '<a href="' . $box['content_uri'] . '" title="' . $box['title'] . '" class="active ' . $class . '" rel="' . $rel . '">' . $image . '</a>';
    }
    elseif ($settings['image_link'] == 'file') {
      $image_dimensions = [
        'width' => $box['width'],
        'height' => $box['height'],
      ];
      if (empty($settings['image_link_image_style'])) {
        $image_url = file_create_url($box['uri']);
      }
      else {
        $image_url = ImageStyle::load($settings['image_link_image_style'])
          ->buildUrl($box['uri']);
        ImageStyle::load($settings['image_link_image_style'])
          ->transformDimensions($image_dimensions, $image);
      }
      $class = $settings['image_link_class'] ? [
        $settings['image_link_class'],
      ] : [];
      $rel = $settings['image_link_rel'];
      $attributes = [];
      switch ($settings['image_link_modal']) {
        case 'colorbox':
          $class[] = 'colorbox';
          $rel = 'colorbox-' . $settings['gallery'];
          break;
        case 'shadowbox':
          $rel = 'shadowbox[' . $settings['gallery'] . ']';
          break;
        case 'fancybox':
          $class[] = 'fancybox';
          $attributes['data-fancybox-group'] = 'fancybox-' . $settings['gallery'];
          break;
        case 'photobox':
          $class[] = 'photobox';
          $attached = photobox_attached_resources();
          $attributes['data-photobox-gallery'] = 'photobox-' . $settings['gallery'];
          break;
        case 'photoswipe':
          $class[] = 'photoswipe';
          photoswipe_load_assets();
          $attributes['data-size'] = $image_dimensions['width'] . 'x' . $image_dimensions['height'];
          break;
        case 'lightbox2':
          $rel = 'lightbox[' . $settings['gallery'] . ']';
          break;
        default:
      }

      // $image = Link::fromTextAndUrl($image,
      //           Url::fromUri($image_url, [
      //                         'attributes' => [
      //                           'title' => $box['title'],
      //                           'class' => $class,
      //                           'rel' => $rel,
      //                         ] + $attributes,
      //                         'html' => TRUE,
      //                       ]))->toString();
      // FIXME: Render HTML programmatically
      // FIXME: Add on properties if there are modules linked
      $image = '<a href="' . $image_url . '" title="' . $box['title'] . '" class="' . $class . '" rel="' . $rel . '">' . $image . '</a>';
    }
    $image_wrapper_style = [
      'float: left;',
      'max-width: ' . round($box['total_width'] - $settings['gap_size'] - 0.5) . 'px;',
      'width: ' . 100 * (round($box['total_width'] - $settings['gap_size'] - 0.5) / round($box['parent_total_width'] - 0.5)) . '%;',
    ];
    if ($settings['gap_size']) {
      $margin_percentage = 100 * ($settings['gap_size'] / round($box['parent_total_width'] - 0.5));
      $image_wrapper_style[] = 'margin: ' . $margin_percentage . '% 0 0 ' . $margin_percentage . '%;';
    }
    $border = '';
    if ($settings['border_size']) {
      $border = 'border: ' . $settings['border_size'] . 'px solid';
      if ($settings['border_color']) {
        $border .= ' ' . $settings['border_color'];
      }
      $border .= ';';
    }
    $output = [
      '#theme' => 'collageformatter_collage_image',
      '#image' => $image,
      '#image_wrapper_class' => [
        'collageformatter-collage-image-wrapper-' . $box['delta'],
      ],
      '#image_wrapper_style' => implode(' ', $image_wrapper_style),
      '#image_style' => $border,
      '#attached' => $attached,
    ];
  }
  return $output;
}