You are here

public function BlazyManager::isBlazy in Blazy 8

Checks for Blazy formatter such as from within a Views style plugin.

Ensures the settings traverse up to the container where Blazy is clueless. The supported plugins can add [data-blazy] attribute into its container containing $settings['blazy_data'] converted into [data-blazy] JSON.

Parameters

array $settings: The settings being modified.

array $item: The item containing settings or item keys.

File

src/BlazyManager.php, line 92

Class

BlazyManager
Implements a public facing blazy manager.

Namespace

Drupal\blazy

Code

public function isBlazy(array &$settings, array $item = []) {

  // Retrieves Blazy formatter related settings from within Views style.
  $content = !empty($settings['item_id']) && isset($item[$settings['item_id']]) ? $item[$settings['item_id']] : $item;

  // 1. Blazy formatter within Views fields by supported modules.
  if (isset($item['settings'])) {

    // Prevents edge case with unexpected flattened Views results which is
    // normally triggered by checking "Use field template" option.
    $blazy = is_array($content) && isset($content['#build']['settings']) ? $content['#build']['settings'] : [];

    // Allows breakpoints overrides such as multi-styled images by GridStack.
    if (empty($settings['breakpoints']) && isset($blazy['breakpoints'])) {
      $settings['breakpoints'] = $blazy['breakpoints'];
    }
    $cherries = [
      'blazy',
      'box_style',
      'image_style',
      'lazy',
      'media_switch',
      'ratio',
      'uri',
    ];
    foreach ($cherries as $key) {
      $fallback = isset($settings[$key]) ? $settings[$key] : '';
      $settings[$key] = isset($blazy[$key]) && empty($fallback) ? $blazy[$key] : $fallback;
    }
  }

  // 2. Blazy Views fields by supported modules.
  if (is_array($content) && isset($content['#view']) && ($view = $content['#view'])) {
    if ($blazy_field = BlazyViews::viewsField($view)) {
      $settings = array_merge(array_filter($blazy_field
        ->mergedViewsSettings()), array_filter($settings));
    }
  }

  // Provides data for the [data-blazy] attribute at the containing element.
  $this
    ->cleanUpBreakpoints($settings);
  if (!empty($settings['breakpoints'])) {
    $image = isset($item['item']) ? $item['item'] : NULL;
    $this
      ->buildDataBlazy($settings, $image);
  }
  unset($settings['uri']);
}