You are here

public function ElevateZoomPlusManager::preRenderBuild in ElevateZoom Plus 8

The #pre_render callback: Provides ElevateZoomPlus related contents.

File

src/ElevateZoomPlusManager.php, line 232

Class

ElevateZoomPlusManager
Provides ElevateZoom Plus library methods mainly for hooks.

Namespace

Drupal\elevatezoomplus

Code

public function preRenderBuild($element) {
  $build = $element['#build'];
  unset($element['#build']);

  // Supported themes: Blazy (field|item_list), slick_wrapper, gridstack.
  $settings = [];
  foreach ([
    'blazy',
    'build',
    'context',
    'settings',
  ] as $key) {
    if (isset($build['#' . $key])) {
      $settings = isset($build['#' . $key]['settings']) ? $build['#' . $key]['settings'] : [];
      $settings = $key == 'build' || $key == 'context' ? $settings : $build['#' . $key];
      break;
    }
  }

  // The AJAX class was already excluded at Blazymanager line #505 and passed
  // into #container_attributes below, gone at 2021, likely changed somewhere.
  $attributes = isset($element['#container_attributes']) ? (array) $element['#container_attributes'] : [];

  // Preserves the AJAX class for the container, then removes it later for the
  // the theme. This is because product container shares the same attributes
  // array which cause unwanted double AJAX classes.
  // Not available if `Use field template` is checked.
  $ajax_class = isset($element['#ajax_replace_class']) ? $element['#ajax_replace_class'] : '';
  if (empty($ajax_class)) {
    if (isset($build['#attributes'], $build['#attributes']['class'])) {
      foreach ($build['#attributes']['class'] as $key => $class) {
        if (mb_strpos($class, 'product--variation-field--variation') !== FALSE) {
          $attributes['class'][] = $class;
          break;
        }
      }
    }
  }
  else {
    $attributes['class'][] = $ajax_class;
  }
  $settings['ajax_replace_class'] = $ajax_class;
  $element['#settings'] = $settings;
  $element['#attributes'] = $attributes;
  $element['#content'] = $build;
  unset($build);
  return $element;
}