View source
<?php
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\insert\Utility\InsertUtility;
function insert_colorbox_insert_config_form(array $form) {
$config = \Drupal::config('insert_colorbox.config');
$form['insert_colorbox'] = [
'#type' => 'fieldset',
'#title' => t('Insert Colorbox'),
];
$stylesList = _insert_colorbox_styles();
$stylesList = InsertUtility::stylesListToOptions($stylesList);
$form['insert_colorbox']['insert_colorbox__style'] = [
'#type' => 'select',
'#title' => t('Image style'),
'#options' => array_merge([
'0' => t('use “Link image to” widget setting'),
], $stylesList),
'#default_value' => $config
->get('style'),
'#description' => t('Select which image style to use for viewing images in the colorbox.'),
];
$form['insert_colorbox']['insert_colorbox__gallery'] = [
'#type' => 'radios',
'#title' => t('Gallery'),
'#description' => t('Select whether images placed with Insert be grouped in one or more colorbox galleries or if images shall open individual colorboxes.'),
'#options' => [
'post' => t('Per post gallery'),
'page' => t('Per page gallery'),
'field_post' => t('Per field in post gallery'),
'field_page' => t('Per field in page gallery'),
'0' => t('No gallery'),
],
'#default_value' => $config
->get('gallery'),
];
return $form;
}
function insert_colorbox_insert_config_submit_form(FormStateInterface $form_state) {
$config = \Drupal::getContainer()
->get('config.factory')
->getEditable('insert_colorbox.config');
$config
->set('style', $form_state
->getValue('insert_colorbox__style'));
$config
->set('gallery', $form_state
->getValue('insert_colorbox__gallery'));
$config
->save();
}
function insert_colorbox_theme() {
return [
'insert_colorbox_image' => [
'template' => 'insert-colorbox-image',
'pattern' => 'insert_colorbox_image__[a-z0-9_]+',
],
];
}
function _insert_colorbox_styles() {
$stylesList = [];
foreach (ImageStyle::loadMultiple() as $style) {
$stylesList[$style
->getName()] = $style;
}
$stylesList['image'] = [
'label' => t('Original image'),
'weight' => -10,
];
return $stylesList;
}
function insert_colorbox_insert_variables($insertType, array &$element, $styleName, array &$vars) {
if (strpos($styleName, 'colorbox__') !== 0) {
return;
}
$insertSettings = $element['#insert']['settings'];
static $gallery_token = NULL;
$config = \Drupal::config('insert_colorbox.config');
switch ($config
->get('gallery')) {
case 'post':
$vars['gallery_id'] = 'gallery-' . $element['#entity_type'];
break;
case 'page':
$vars['gallery_id'] = 'gallery-all';
break;
case 'field_post':
$vars['gallery_id'] = 'gallery-' . $element['#entity_type'] . '-' . $element['#field_name'];
break;
case 'field_page':
$vars['gallery_id'] = 'gallery-' . $element['#field_name'];
break;
default:
$vars['gallery_id'] = '';
}
$colorboxConfig = \Drupal::config('colorbox.settings');
if (!empty($vars['gallery_id']) && $colorboxConfig
->get('advanced.unique_token')) {
if (is_null($gallery_token)) {
$gallery_token = Crypt::randomBytesBase64(8);
}
$vars['gallery_id'] = $vars['gallery_id'] . '-' . $gallery_token;
}
$vars['url_link'] = $vars['url_original'];
$styleName = NULL;
if ($config
->get('style') === '0') {
$styleName = $insertSettings['link_image'];
}
elseif ($config
->get('style') !== 'image') {
$styleName = $config
->get('style');
}
if ($styleName !== NULL) {
$url = InsertUtility::buildDerivativeUrl($vars['file'], $styleName, \Drupal::config('insert.config')
->get('absolute'));
if ($url !== NULL) {
$vars['url_link'] = $url;
}
}
}
function insert_colorbox_insert_styles($insertType) {
if ($insertType !== INSERT_TYPE_IMAGE) {
return [];
}
$insertStyles = [];
foreach (ImageStyle::loadMultiple() as $style) {
$insertStyles['colorbox__' . $style
->getName()] = [
'label' => t('Colorbox @style', [
'@style' => $style
->getName(),
]),
];
}
return $insertStyles;
}
function insert_colorbox_insert_render($styleName, array $vars) {
$moduleName = explode('__', $styleName, 2)[0];
if ($moduleName !== 'colorbox') {
return [];
}
$templateStyleName = str_replace('-', '_', $styleName);
$templateFieldName = str_replace('-', '_', $vars['field_name']);
return \Drupal::theme()
->render([
'insert_colorbox_image__' . $templateFieldName . '__' . $templateStyleName,
'insert_colorbox_image__' . $templateFieldName,
'insert_colorbox_image__' . $templateStyleName,
'insert_colorbox_image',
], $vars);
}
function insert_colorbox_editor_js_settings_alter(array &$settings) {
InsertUtility::addEditorExtraAllowedContent($settings, [
'a[data-colorbox-gallery]',
]);
}
function insert_colorbox_insert_allowed_html() {
return [
'tags' => [
'a' => NULL,
],
'attributes' => [
'a' => [
'data-colorbox-gallery' => NULL,
],
],
];
}
function insert_colorbox_module_implements_alter(array &$implementations, $hook) {
if ($hook === 'insert_variables') {
$group = $implementations['insert_colorbox'];
unset($implementations['insert_colorbox']);
$implementations['insert_colorbox'] = $group;
}
}