insert_responsive_image.module in Insert 8.2
File
modules/insert_responsive_image/insert_responsive_image.module
View source
<?php
use Drupal\image\Entity\ImageStyle;
use Drupal\insert\Utility\InsertUtility;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
function insert_responsive_image_insert_styles($insertType) {
if ($insertType !== INSERT_TYPE_IMAGE) {
return [];
}
$insertStyles = [];
foreach (ResponsiveImageStyle::loadMultiple() as $i => $style) {
$insertStyles['responsive_image__' . $style
->id()] = [
'label' => t('Responsive') . ': ' . $style
->id(),
];
}
return $insertStyles;
}
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;
}
$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']);
}
template_preprocess_responsive_image($responsiveImageVars);
$vars['attributes'] = array_merge(isset($vars['attributes']) ? $vars['attributes'] : [], isset($responsiveImageVars['attributes']) ? $responsiveImageVars['attributes'] : []);
}
function insert_responsive_image_editor_js_settings_alter(array &$settings) {
InsertUtility::addEditorExtraAllowedContent($settings, [
'img[srcset,sizes]',
]);
}
function insert_responsive_image_insert_allowed_html() {
return [
'attributes' => [
'img' => [
'srcset' => NULL,
'sizes' => NULL,
],
],
];
}
function insert_responsive_image_module_implements_alter(array &$implementations, $hook) {
if ($hook === 'insert_variables') {
$group = $implementations['insert_responsive_image'];
unset($implementations['insert_responsive_image']);
$implementations['insert_responsive_image'] = $group;
}
}