View source
<?php
namespace Drupal\blazy;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FormatterInterface;
use Drupal\editor\Entity\Editor;
class BlazyAlter {
public static function configSchemaInfoAlter(array &$definitions, $formatter = 'blazy_base', array $settings = []) {
if (isset($definitions[$formatter])) {
$mappings =& $definitions[$formatter]['mapping'];
$settings = $settings ?: BlazyDefault::extendedSettings() + BlazyDefault::gridSettings();
foreach ($settings as $key => $value) {
$type = gettype($value);
$type = $type == 'double' ? 'float' : $type;
$mappings[$key]['type'] = $key == 'breakpoints' ? 'mapping' : (is_array($value) ? 'sequence' : $type);
if (!is_array($value)) {
$mappings[$key]['label'] = Unicode::ucfirst(str_replace('_', ' ', $key));
}
}
if (isset($mappings['breakpoints'])) {
foreach ([
'xs',
'sm',
'md',
'lg',
'xl',
] as $breakpoint) {
$mappings['breakpoints']['mapping'][$breakpoint]['type'] = 'mapping';
foreach ([
'breakpoint',
'width',
'image_style',
] as $item) {
$mappings['breakpoints']['mapping'][$breakpoint]['mapping'][$item]['type'] = 'string';
$mappings['breakpoints']['mapping'][$breakpoint]['mapping'][$item]['label'] = Unicode::ucfirst(str_replace('_', ' ', $item));
}
}
}
}
}
public static function libraryInfoAlter(&$libraries, $extension) {
if ($extension === 'blazy') {
if ($path = blazy_libraries_get_path('blazy')) {
$libraries['blazy']['js'] = [
'/' . $path . '/blazy.js' => [
'weight' => -4,
],
];
}
if (blazy()
->configLoad('io.enabled')) {
if (blazy()
->configLoad('io.unblazy')) {
$dependencies = [
'core/drupal',
'blazy/bio.media',
'blazy/loading',
];
$libraries['load']['dependencies'] = $dependencies;
}
else {
$libraries['load']['dependencies'][] = 'blazy/bio.media';
}
}
}
if ($extension === 'media' && isset($libraries['oembed.frame'])) {
$libraries['oembed.frame']['dependencies'][] = 'blazy/oembed';
}
}
public static function blazySettingsAlter(array &$build, $items) {
$settings =& $build['settings'];
if (function_exists('views_get_current_view') && ($view = views_get_current_view())) {
$settings['view_name'] = $view->storage
->id();
$settings['current_view_mode'] = $view->current_display;
$plugin_id = is_null($view->style_plugin) ? "" : $view->style_plugin
->getPluginId();
$settings['view_plugin_id'] = empty($settings['view_plugin_id']) ? $plugin_id : $settings['view_plugin_id'];
}
}
public static function isCkeditorApplicable(Editor $editor) {
foreach ([
'entity_embed',
'media_embed',
] as $filter) {
if (!$editor
->isNew() && $editor
->getFilterFormat()
->filters()
->has($filter) && $editor
->getFilterFormat()
->filters($filter)
->getConfiguration()['status']) {
return TRUE;
}
}
return FALSE;
}
public static function ckeditorCssAlter(array &$css, Editor $editor) {
if (self::isCkeditorApplicable($editor)) {
$path = base_path() . drupal_get_path('module', 'blazy');
$css[] = $path . '/css/components/blazy.media.css';
$css[] = $path . '/css/components/blazy.preview.css';
$css[] = $path . '/css/components/blazy.ratio.css';
}
}
public static function thirdPartyFormatters() {
$formatters = [
'file_video',
];
blazy()
->getModuleHandler()
->alter('blazy_third_party_formatters', $formatters);
return array_unique($formatters);
}
public static function thirdPartyPreprocessField(array &$variables) {
$element = $variables['element'];
$settings = empty($element['#blazy']) ? [] : $element['#blazy'];
$settings['third_party'] = $element['#third_party_settings'];
$is_preview = Blazy::isPreview();
foreach ($variables['items'] as &$item) {
if (empty($item['content'])) {
continue;
}
$item_attributes =& $item['content'][isset($item['content']['#attributes']) ? '#attributes' : '#item_attributes'];
$item_attributes['data-b-lazy'] = TRUE;
if ($is_preview) {
$item_attributes['data-b-preview'] = TRUE;
}
}
$attachments = blazy()
->attach($settings);
$variables['#attached'] = empty($variables['#attached']) ? $attachments : NestedArray::mergeDeep($variables['#attached'], $attachments);
}
public static function fieldFormatterThirdPartySettingsForm(FormatterInterface $plugin) {
if (in_array($plugin
->getPluginId(), self::thirdPartyFormatters())) {
return [
'blazy' => [
'#type' => 'checkbox',
'#title' => 'Blazy',
'#default_value' => $plugin
->getThirdPartySetting('blazy', 'blazy', FALSE),
],
];
}
return [];
}
public static function fieldFormatterSettingsSummaryAlter(&$summary, $context) {
$on = $context['formatter']
->getThirdPartySetting('blazy', 'blazy', FALSE);
if ($on && in_array($context['formatter']
->getPluginId(), self::thirdPartyFormatters())) {
$summary[] = 'Blazy';
}
}
public static function attachColorbox(array &$load, $attach = []) {
if (\Drupal::hasService('colorbox.attachment')) {
$dummy = [];
\Drupal::service('colorbox.attachment')
->attach($dummy);
$load = isset($dummy['#attached']) ? NestedArray::mergeDeep($load, $dummy['#attached']) : $load;
$load['library'][] = 'blazy/colorbox';
unset($dummy);
}
}
}