You are here

function insert_responsive_image_insert_variables in Insert 8.2

Implements hook_insert_variables().

File

modules/insert_responsive_image/insert_responsive_image.module, line 34

Code

function insert_responsive_image_insert_variables($insertType, array &$element, $styleName, array &$vars) {
  $insertStyleNameSegments = explode('__', $styleName, 2);
  $moduleName = $insertStyleNameSegments[0];
  if ($moduleName !== 'responsive_image' || count($insertStyleNameSegments) === 1) {
    return;
  }

  /** @var \Drupal\file\Entity\File $file */
  $file = $element['#insert'][$insertType]['file'];
  $styleName = $insertStyleNameSegments[1];
  $responsiveStyle = ResponsiveImageStyle::load($styleName);
  if ($responsiveStyle === NULL) {
    return;
  }
  $responsiveImageVars = [
    'item' => NULL,
    'item_attributes' => NULL,
    'responsive_image_style_id' => $styleName,
    'uri' => $file
      ->getFileUri(),
  ];
  $style = ImageStyle::load($responsiveStyle
    ->getFallbackImageStyle());
  $vars['url'] = $style === NULL ? file_create_url($file
    ->getFileUri()) : $style
    ->buildUrl($file
    ->getFileUri());
  if (!\Drupal::config('insert.config')
    ->get('absolute')) {
    $vars['url'] = file_url_transform_relative($vars['url']);
  }

  // Have Responsive Image module generate the srcset and sizes attributes:
  template_preprocess_responsive_image($responsiveImageVars);
  $vars['attributes'] = array_merge(isset($vars['attributes']) ? $vars['attributes'] : [], isset($responsiveImageVars['attributes']) ? $responsiveImageVars['attributes'] : []);
}