You are here

insert_responsive_image.module in Insert 8.2

File

modules/insert_responsive_image/insert_responsive_image.module
View source
<?php

/**
 * @file
 */
use Drupal\image\Entity\ImageStyle;
use Drupal\insert\Utility\InsertUtility;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;

/**
 * Implements hook_insert_styles().
 */
function insert_responsive_image_insert_styles($insertType) {
  if ($insertType !== INSERT_TYPE_IMAGE) {
    return [];
  }
  $insertStyles = [];

  /* @var ResponsiveImageStyle $style */
  foreach (ResponsiveImageStyle::loadMultiple() as $i => $style) {
    $insertStyles['responsive_image__' . $style
      ->id()] = [
      'label' => t('Responsive') . ': ' . $style
        ->id(),
    ];
  }
  return $insertStyles;
}

/**
 * Implements hook_insert_variables().
 */
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'] : []);
}

/**
 * Implements hook_editor_js_settings_alter().
 */
function insert_responsive_image_editor_js_settings_alter(array &$settings) {
  InsertUtility::addEditorExtraAllowedContent($settings, [
    'img[srcset,sizes]',
  ]);
}

/**
 * Implements hook_insert_allowed_html().
 */
function insert_responsive_image_insert_allowed_html() {
  return [
    'attributes' => [
      'img' => [
        'srcset' => NULL,
        'sizes' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_module_implements_alter().
 */
function insert_responsive_image_module_implements_alter(array &$implementations, $hook) {
  if ($hook === 'insert_variables') {

    // Move hook_insert_variables() implementation to the end of the execution
    // list to have it processed after insert_insert_variables().
    $group = $implementations['insert_responsive_image'];
    unset($implementations['insert_responsive_image']);
    $implementations['insert_responsive_image'] = $group;
  }
}