forked from a64f7bb4-7358-4778-9fbe-3b882c34cc1d/v1
				
			
		
			
				
	
	
		
			239 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			239 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
<?php
 | 
						|
 | 
						|
/**
 | 
						|
 * @file
 | 
						|
 * Provides basic Blazy integration for lazy loading and multi-serving images.
 | 
						|
 */
 | 
						|
 | 
						|
use Drupal\Core\Field\FormatterInterface;
 | 
						|
use Drupal\editor\Entity\Editor;
 | 
						|
use Drupal\blazy\Blazy;
 | 
						|
use Drupal\blazy\BlazyAlter;
 | 
						|
use Drupal\blazy\BlazyDefault;
 | 
						|
use Drupal\blazy\Theme\BlazyTheme;
 | 
						|
use Drupal\blazy\Theme\BlazyViews;
 | 
						|
use Drupal\blazy\Utility\BlazyMarkdown;
 | 
						|
 | 
						|
/**
 | 
						|
 * Provides a convenient shortcut for procedural hooks, or static methods.
 | 
						|
 *
 | 
						|
 * @return class
 | 
						|
 *   The Blazy manager class instance.
 | 
						|
 */
 | 
						|
function blazy() {
 | 
						|
  static $manager;
 | 
						|
 | 
						|
  if (!isset($manager)) {
 | 
						|
    $manager = \Drupal::service('blazy.manager');
 | 
						|
  }
 | 
						|
 | 
						|
  return $manager;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_theme().
 | 
						|
 */
 | 
						|
function blazy_theme() {
 | 
						|
  return ['blazy' => ['render element' => 'element']];
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Prepares variables for blazy.html.twig templates.
 | 
						|
 */
 | 
						|
function template_preprocess_blazy(&$variables) {
 | 
						|
  BlazyTheme::blazy($variables);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Overrides variables for image.html.twig templates.
 | 
						|
 *
 | 
						|
 * @todo remove when core makes `loading` attribute optional.
 | 
						|
 */
 | 
						|
function blazy_preprocess_image(&$variables) {
 | 
						|
  $attributes = &$variables['attributes'];
 | 
						|
  if (isset($attributes['data-b-unloading'])) {
 | 
						|
    unset($attributes['loading'], $attributes['data-b-unloading']);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Overrides variables for responsive-image.html.twig templates.
 | 
						|
 */
 | 
						|
function blazy_preprocess_responsive_image(&$variables) {
 | 
						|
  if (isset($variables['attributes']['data-b-lazy'])) {
 | 
						|
    BlazyTheme::responsiveImage($variables);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Overrides variables for file-video.html.twig templates.
 | 
						|
 */
 | 
						|
function blazy_preprocess_file_video(&$variables) {
 | 
						|
  if (isset($variables['attributes']['data-b-lazy'])) {
 | 
						|
    BlazyTheme::fileVideo($variables);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Overrides template_preprocess_media_oembed_iframe().
 | 
						|
 */
 | 
						|
function blazy_preprocess_media_oembed_iframe(array &$variables) {
 | 
						|
  BlazyTheme::mediaOembedIframe($variables);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_preprocess_field().
 | 
						|
 */
 | 
						|
function blazy_preprocess_field(array &$variables) {
 | 
						|
  if (isset($variables['element']['#blazy'])
 | 
						|
    || !empty($variables['element']['#third_party_settings']['blazy']['blazy'])) {
 | 
						|
    BlazyTheme::field($variables);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_preprocess_views_view().
 | 
						|
 */
 | 
						|
function blazy_preprocess_views_view(array &$variables) {
 | 
						|
  if (!empty($variables['css_class'])
 | 
						|
    && strpos($variables['css_class'], 'blazy--') !== FALSE) {
 | 
						|
    if ($lightboxes = blazy()->getLightboxes()) {
 | 
						|
      BlazyViews::preprocessViewsView($variables, $lightboxes);
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_views_pre_render().
 | 
						|
 */
 | 
						|
function blazy_views_pre_render($view) {
 | 
						|
  if (isset($view)) {
 | 
						|
    BlazyViews::viewsPreRender($view);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_config_schema_info_alter().
 | 
						|
 */
 | 
						|
function blazy_config_schema_info_alter(array &$definitions) {
 | 
						|
  BlazyAlter::configSchemaInfoAlter($definitions, 'blazy_base');
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_library_info_alter().
 | 
						|
 */
 | 
						|
function blazy_library_info_alter(&$libraries, $extension) {
 | 
						|
  $core = $extension === 'core'
 | 
						|
    && (isset($libraries['once'])
 | 
						|
    || isset($libraries['drupal.debounce']));
 | 
						|
  if ($extension === 'blazy' || $extension === 'media' || $core) {
 | 
						|
    BlazyAlter::libraryInfoAlter($libraries, $extension);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_library_info_build().
 | 
						|
 */
 | 
						|
function blazy_library_info_build() {
 | 
						|
  return BlazyAlter::libraryInfoBuild();
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_blazy_settings_alter().
 | 
						|
 */
 | 
						|
function blazy_blazy_settings_alter(array &$build, $items) {
 | 
						|
  BlazyAlter::blazySettingsAlter($build, $items);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_field_formatter_third_party_settings_form().
 | 
						|
 */
 | 
						|
function blazy_field_formatter_third_party_settings_form(FormatterInterface $plugin) {
 | 
						|
  return BlazyAlter::fieldFormatterThirdPartySettingsForm($plugin);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_field_formatter_settings_summary_alter().
 | 
						|
 */
 | 
						|
function blazy_field_formatter_settings_summary_alter(&$summary, $context) {
 | 
						|
  BlazyAlter::fieldFormatterSettingsSummaryAlter($summary, $context);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_ckeditor_css_alter().
 | 
						|
 */
 | 
						|
function blazy_ckeditor_css_alter(array &$css, Editor $editor) {
 | 
						|
  BlazyAlter::ckeditorCssAlter($css, $editor);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Alters blazy settings here due to static FormatterBase::defaultSettings().
 | 
						|
 */
 | 
						|
function blazy_alterable_settings(array &$settings) {
 | 
						|
  return BlazyDefault::getInstance()->alterableSettings($settings);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Provides a shortcut to parse the markdown string for better hook_help().
 | 
						|
 */
 | 
						|
function blazy_parse_markdown($string) {
 | 
						|
  return BlazyMarkdown::parse($string);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Provides a wrapper to replace deprecated libraries_get_path() at ease.
 | 
						|
 */
 | 
						|
function blazy_libraries_get_path($name, $base_path = FALSE) {
 | 
						|
  return Blazy::getLibrariesPath($name, $base_path);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Cleans out unclean module filter references.
 | 
						|
 *
 | 
						|
 * @todo remove when core filter takes care of its own plugins removal cleanly.
 | 
						|
 */
 | 
						|
function blazy_filter_cleanup($module = 'blazy') {
 | 
						|
  $config_factory = \Drupal::configFactory();
 | 
						|
  $config_storage = \Drupal::service('config.storage');
 | 
						|
 | 
						|
  $filter = "filters.{$module}_filter";
 | 
						|
 | 
						|
  // Removes unclean [module]_filter references, see #3257390.
 | 
						|
  foreach ($config_storage->listAll('filter.format') as $config_name) {
 | 
						|
    $config = $config_factory->getEditable($config_name);
 | 
						|
 | 
						|
    if ($plugin = $config->get($filter)) {
 | 
						|
      $dependencies = $config->get('dependencies');
 | 
						|
      $modules = array_diff($dependencies['module'], [$module]);
 | 
						|
 | 
						|
      $config
 | 
						|
        ->clear($filter)
 | 
						|
        ->set('dependencies.module', $modules)
 | 
						|
        ->save(TRUE);
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  // Just to be safe and sure, reset filter_formats cache, etc.
 | 
						|
  // @todo re-check if core deprecated this function at or by D10.
 | 
						|
  $reset = 'drupal_static_reset';
 | 
						|
  if (is_callable('drupal_static_reset')) {
 | 
						|
    $reset('filter_formats');
 | 
						|
  }
 | 
						|
 | 
						|
  blazy()->getStorage('filter_format')->resetCache();
 | 
						|
 | 
						|
  // Clear plugin manager caches.
 | 
						|
  \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Implements hook_field_formatter_info_alter().
 | 
						|
 *
 | 
						|
 * @todo remove VEF/VEM from blazy:8.x-3.0 for core Media integration.
 | 
						|
 */
 | 
						|
function blazy_field_formatter_info_alter(array &$info) {
 | 
						|
  if (blazy()->getModuleHandler()->moduleExists('video_embed_field')) {
 | 
						|
    Blazy::fieldFormatterInfoAlter($info);
 | 
						|
  }
 | 
						|
}
 |