You are here

public function BlazyManagerBase::attach in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/BlazyManagerBase.php \Drupal\blazy\BlazyManagerBase::attach()
  2. 7 src/BlazyManagerBase.php \Drupal\blazy\BlazyManagerBase::attach()

Returns array of needed assets suitable for #attached property.

File

src/BlazyManagerBase.php, line 140

Class

BlazyManagerBase
Implements BlazyManagerInterface.

Namespace

Drupal\blazy

Code

public function attach($attach = []) {
  $load = [];
  $switch = empty($attach['media_switch']) ? '' : $attach['media_switch'];
  if ($switch && $switch != 'content') {
    $attach[$switch] = $switch;
    if (in_array($switch, $this
      ->getLightboxes())) {
      $load['library'][] = 'blazy/lightbox';
    }
  }

  // Allow both variants of grid or column to co-exist for different fields.
  if (!empty($attach['style'])) {
    foreach ([
      'column',
      'grid',
    ] as $grid) {
      $attach[$grid] = $attach['style'];
    }
  }
  foreach ([
    'column',
    'grid',
    'media',
    'photobox',
    'ratio',
  ] as $component) {
    if (!empty($attach[$component])) {
      $load['library'][] = 'blazy/' . $component;
    }
  }

  // Core Blazy libraries.
  if (!empty($attach['blazy'])) {
    $load['library'][] = 'blazy/load';
    $load['drupalSettings']['blazy'] = $this
      ->configLoad()['blazy'];
  }
  $this->moduleHandler
    ->alter('blazy_attach', $load, $attach);
  return $load;
}