You are here

public function BlazyFormatter::buildSettings in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/BlazyFormatter.php \Drupal\blazy\BlazyFormatter::buildSettings()
1 call to BlazyFormatter::buildSettings()
BlazyFormatter::preBuildElements in src/BlazyFormatter.php

File

src/BlazyFormatter.php, line 29

Class

BlazyFormatter
Implements BlazyFormatterInterface.

Namespace

Drupal\blazy

Code

public function buildSettings(array &$build, $items, $entity) {
  $settings =& $build['settings'];
  $count = count($items);
  $entity_type_id = $settings['entity_type_id'];
  $entity_id = $settings['entity_id'];
  $bundle = $settings['bundle'];
  $field_name = $settings['field_name'];
  $field_clean = str_replace('field_', '', $field_name);
  $view_mode = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode'];
  $namespace = $settings['namespace'] = empty($settings['namespace']) ? 'blazy' : $settings['namespace'];
  $id = isset($settings['id']) ? $settings['id'] : '';
  $gallery_id = "{$namespace}-{$entity_type_id}-{$bundle}-{$field_clean}-{$view_mode}";
  $id = Blazy::getHtmlId("{$gallery_id}-{$entity_id}", $id);
  $switch = empty($settings['media_switch']) ? '' : $settings['media_switch'];
  $internal_path = entity_uri($entity_type_id, $entity);
  $langcode = $settings['langcode'];

  // Pass settings to child elements.
  $settings += $this
    ->getCommonSettings();
  $settings['cache_metadata'] = [
    'keys' => [
      $id,
      $count,
      $langcode,
    ],
  ];
  $settings['content_url'] = isset($internal_path['path']) ? $internal_path['path'] : '';
  $settings['count'] = $count;
  $settings['gallery_id'] = str_replace('_', '-', $gallery_id . '-' . $switch);
  $settings['id'] = $id;
  $settings['lightbox'] = $switch && in_array($switch, $this
    ->getLightboxes()) ? $switch : FALSE;
  $settings['entity'] = empty($settings['lightbox']) ? NULL : $entity;
  $settings['resimage'] = function_exists('picture_mapping_load') && $this
    ->config('responsive_image', FALSE, 'blazy.settings') && !empty($settings['responsive_image_style']);

  // Don't bother with Vanilla on.
  if (!empty($settings['vanilla'])) {
    $settings = array_filter($settings);
    return;
  }

  // Don't bother if using Responsive image.
  $settings['breakpoints'] = isset($settings['breakpoints']) && empty($settings['responsive_image_style']) ? $settings['breakpoints'] : [];
  $settings['caption'] = empty($settings['caption']) ? [] : array_filter($settings['caption']);
  $settings['background'] = empty($settings['responsive_image_style']) && !empty($settings['background']);
  $settings['placeholder'] = $this
    ->config('placeholder', '', 'blazy.settings');

  // @todo change to BlazyBreakpoint::cleanUpBreakpoints() at blazy:7.x-2.0.
  $this
    ->cleanUpBreakpoints($settings);

  // Lazy load types: blazy, and slick: ondemand, anticipated, progressive.
  $settings['blazy'] = !empty($settings['blazy']) || $settings['background'] || !empty($settings['resimage']) || !empty($settings['breakpoints']);
  if ($settings['blazy']) {
    $settings['lazy'] = 'blazy';
  }

  // At D7, BlazyFilter can only attach globally, prevents blocking.
  // Allows lightboxes to provide its own optionsets.
  if ($switch) {
    $settings[$switch] = empty($settings[$switch]) ? $switch : $settings[$switch];
  }

  // Aspect ratio isn't working with Responsive image, yet.
  // However allows custom work to get going with an enforced.
  $ratio = FALSE;
  if (!empty($settings['ratio'])) {
    $ratio = empty($settings['responsive_image_style']);
    if ($settings['ratio'] == 'enforced' || $settings['background']) {
      $ratio = TRUE;
    }
  }
  $settings['ratio'] = $ratio ? $settings['ratio'] : FALSE;
}