class BlazyFormatter in Blazy 8.2
Same name and namespace in other branches
- 7 src/BlazyFormatter.php \Drupal\blazy\BlazyFormatter
Provides common field formatter-related methods: Blazy, Slick.
Hierarchy
- class \Drupal\blazy\BlazyManagerBase implements BlazyManagerInterface uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\blazy\BlazyManager implements TrustedCallbackInterface
- class \Drupal\blazy\BlazyFormatter implements BlazyFormatterInterface
- class \Drupal\blazy\BlazyManager implements TrustedCallbackInterface
Expanded class hierarchy of BlazyFormatter
1 file declares its use of BlazyFormatter
- BlazyFormatterTest.php in tests/
modules/ blazy_test/ src/ BlazyFormatterTest.php
1 string reference to 'BlazyFormatter'
1 service uses BlazyFormatter
File
- src/
BlazyFormatter.php, line 8
Namespace
Drupal\blazyView source
class BlazyFormatter extends BlazyManager implements BlazyFormatterInterface {
/**
* Checks if image dimensions are set.
*
* @var array
*/
private $isImageDimensionSet;
/**
* Returns available styles with crop in the effect name.
*
* @var array
*/
protected $cropStyles;
/**
* Checks if the image style contains crop in the effect name.
*
* @var array
*/
protected $isCrop;
/**
* {@inheritdoc}
*/
public function buildSettings(array &$build, $items) {
$settings =& $build['settings'];
$this
->getCommonSettings($settings);
$count = $items
->count();
$field = $items
->getFieldDefinition();
$entity = $items
->getEntity();
$entity_type_id = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
$bundle = $entity
->bundle();
$field_name = $field
->getName();
$field_clean = str_replace("field_", '', $field_name);
$view_mode = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode'];
$namespace = $settings['namespace'];
$id = isset($settings['id']) ? $settings['id'] : '';
$gallery_id = "{$namespace}-{$entity_type_id}-{$bundle}-{$field_clean}-{$view_mode}";
$id = Blazy::getHtmlId("{$gallery_id}-{$entity_id}", $id);
$internal_path = $absolute_path = NULL;
// Deals with UndefinedLinkTemplateException such as paragraphs type.
// @see #2596385, or fetch the host entity.
if (!$entity
->isNew() && method_exists($entity, 'hasLinkTemplate')) {
if ($entity
->hasLinkTemplate('canonical')) {
// Check if multilingual is enabled (@see #3214002).
if ($entity
->hasTranslation($settings['current_language'])) {
// Load the translated url.
$url = $entity
->getTranslation($settings['current_language'])
->toUrl();
}
else {
// Otherwise keep the standard url.
$url = $entity
->toUrl();
}
$internal_path = $url
->getInternalPath();
$absolute_path = $url
->setAbsolute()
->toString();
}
}
$settings['bundle'] = $bundle;
$settings['cache_metadata'] = [
'keys' => [
$id,
$count,
],
];
$settings['cache_tags'][] = $entity_type_id . ':' . $entity_id;
$settings['caption'] = empty($settings['caption']) ? [] : array_filter($settings['caption']);
$settings['content_url'] = $settings['absolute_path'] = $absolute_path;
$settings['count'] = $count;
$settings['entity_id'] = $entity_id;
$settings['entity_type_id'] = $entity_type_id;
$settings['gallery_id'] = str_replace('_', '-', $gallery_id . '-' . $settings['media_switch']);
$settings['id'] = $id;
$settings['internal_path'] = $internal_path;
$settings['use_field'] = !$settings['lightbox'] && isset($settings['third_party'], $settings['third_party']['linked_field']) && !empty($settings['third_party']['linked_field']['linked']);
// Bail out if Vanilla mode is requested.
if (!empty($settings['vanilla'])) {
$settings = array_filter($settings);
return;
}
// Lazy load types: blazy, and slick: ondemand, anticipated, progressive.
$settings['blazy'] = !empty($settings['blazy']) || !empty($settings['background']) || $settings['resimage'];
$settings['lazy'] = $settings['blazy'] ? 'blazy' : (isset($settings['lazy']) ? $settings['lazy'] : '');
$settings['lazy'] = empty($settings['is_preview']) ? $settings['lazy'] : '';
}
/**
* {@inheritdoc}
*/
public function preBuildElements(array &$build, $items, array $entities = []) {
$this
->buildSettings($build, $items);
$settings =& $build['settings'];
// Pass first item to optimize sizes this time.
if (isset($items[0]) && ($item = $items[0])) {
$this
->extractFirstItem($settings, $item, reset($entities));
}
// Sets dimensions once, if cropped, to reduce costs with ton of images.
// This is less expensive than re-defining dimensions per image.
if (!empty($settings['_uri'])) {
if (empty($settings['resimage'])) {
$this
->setImageDimensions($settings);
}
elseif (!empty($settings['resimage']) && !empty($settings['ratio']) && $settings['ratio'] == 'fluid') {
$this
->setResponsiveImageDimensions($settings);
}
}
// Allows altering the settings.
$this
->getModuleHandler()
->alter('blazy_settings', $build, $items);
}
/**
* {@inheritdoc}
*/
public function postBuildElements(array &$build, $items, array $entities = []) {
// Do nothing.
}
/**
* {@inheritdoc}
*/
public function extractFirstItem(array &$settings, $item, $entity = NULL) {
if ($settings['field_type'] == 'image') {
$settings['_item'] = $item;
$settings['_uri'] = ($file = $item->entity) && empty($item->uri) ? $file
->getFileUri() : $item->uri;
}
elseif ($entity && $entity
->hasField('thumbnail') && ($image = $entity
->get('thumbnail')
->first())) {
if (isset($image->entity) && ($file = $image->entity)) {
$settings['_item'] = $image;
$settings['_uri'] = $file
->getFileUri();
}
}
// The first image dimensions to differ from individual item dimensions.
if (!empty($settings['_item'])) {
BlazyUtil::imageDimensions($settings, $settings['_item'], TRUE);
}
}
/**
* Sets dimensions once to reduce method calls, if image style contains crop.
*
* @param array $settings
* The settings being modified.
*/
protected function setImageDimensions(array &$settings = []) {
if (!isset($this->isImageDimensionSet[md5($settings['id'])])) {
// If image style contains crop, sets dimension once, and let all inherit.
if (!empty($settings['image_style']) && ($style = $this
->isCrop($settings['image_style']))) {
$settings = array_merge($settings, BlazyUtil::transformDimensions($style, $settings, TRUE));
// Informs individual images that dimensions are already set once.
$settings['_dimensions'] = TRUE;
}
$this->isImageDimensionSet[md5($settings['id'])] = TRUE;
}
}
/**
* Returns available image styles with crop in the name.
*/
private function cropStyles() {
if (!isset($this->cropStyles)) {
$this->cropStyles = [];
foreach ($this
->entityLoadMultiple('image_style') as $style) {
foreach ($style
->getEffects() as $effect) {
if (strpos($effect
->getPluginId(), 'crop') !== FALSE) {
$this->cropStyles[$style
->getName()] = $style;
break;
}
}
}
}
return $this->cropStyles;
}
/**
* {@inheritdoc}
*/
public function isCrop($style) {
if (!isset($this->isCrop[$style])) {
$this->isCrop[$style] = $this
->cropStyles() && isset($this
->cropStyles()[$style]) ? $this
->cropStyles()[$style] : FALSE;
}
return $this->isCrop[$style];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlazyFormatter:: |
protected | property | Returns available styles with crop in the effect name. | |
BlazyFormatter:: |
protected | property | Checks if the image style contains crop in the effect name. | |
BlazyFormatter:: |
private | property | Checks if image dimensions are set. | |
BlazyFormatter:: |
public | function |
Modifies the field formatter settings inherited by child elements. Overrides BlazyFormatterInterface:: |
|
BlazyFormatter:: |
private | function | Returns available image styles with crop in the name. | |
BlazyFormatter:: |
public | function |
Extract the first image item to build colorbox/zoom-like gallery. Overrides BlazyFormatterInterface:: |
|
BlazyFormatter:: |
public | function |
Checks if an image style contains crop effect. Overrides BlazyFormatterInterface:: |
|
BlazyFormatter:: |
public | function |
Modifies the field formatter settings not inherited by child elements. Overrides BlazyFormatterInterface:: |
|
BlazyFormatter:: |
public | function |
Modifies the field formatter settings inherited by child elements. Overrides BlazyFormatterInterface:: |
|
BlazyFormatter:: |
protected | function | Sets dimensions once to reduce method calls, if image style contains crop. | |
BlazyManager:: |
private | function | Prepares CSS background image. | |
BlazyManager:: |
public | function | Returns the contents using theme_field(), or theme_item_list(). | |
BlazyManager:: |
public | function | Build captions for both old image, or media entity. | |
BlazyManager:: |
protected | function | Build out (rich media) content. | |
BlazyManager:: |
private | function | Build out image, or anything related, including cache, CSS background, etc. | |
BlazyManager:: |
private | function | Build out (Responsive) image. | |
BlazyManager:: |
private | function | Build out Responsive image. | |
BlazyManager:: |
protected | function | Build thumbnails, also to provide placeholder for blur effect. | |
BlazyManager:: |
public | function | Returns the enforced rich media content, or media using theme_blazy(). | |
BlazyManager:: |
public | function | Deprecated method. | |
BlazyManager:: |
protected | function | Prepares Blazy settings. | |
BlazyManager:: |
protected | function | Prepares the Blazy output as a structured array ready for ::renderer(). | |
BlazyManager:: |
protected | function | Prepares Blazy outputs, extract items as indices. | |
BlazyManager:: |
public | function | Builds the Blazy image as a structured array ready for ::renderer(). | |
BlazyManager:: |
public | function | Builds the Blazy outputs as a structured array ready for ::renderer(). | |
BlazyManager:: |
protected | function | Build thumbnails, also to provide placeholder for blur effect. | |
BlazyManager:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
|
BlazyManagerBase:: |
protected | property | The cache backend. | |
BlazyManagerBase:: |
protected | property | The config factory. | |
BlazyManagerBase:: |
protected | property | The entity repository service. | |
BlazyManagerBase:: |
protected | property | The entity type manager service. | |
BlazyManagerBase:: |
protected | property | The language manager. | |
BlazyManagerBase:: |
protected | property | The module handler service. | |
BlazyManagerBase:: |
protected | property | The renderer. | |
BlazyManagerBase:: |
protected | property | The app root. | |
BlazyManagerBase:: |
public | function |
Returns array of needed assets suitable for #attached property. Overrides BlazyManagerInterface:: |
|
BlazyManagerBase:: |
public | function | Collects defined skins as registered via hook_MODULE_NAME_skins_info(). | |
BlazyManagerBase:: |
public | function | Returns any config, or keyed by the $setting_name. | |
BlazyManagerBase:: |
public static | function | ||
BlazyManagerBase:: |
public | function | Returns a shortcut for loading a config entity: image_style, slick, etc. | |
BlazyManagerBase:: |
public | function | Returns a shortcut for loading multiple configuration entities. | |
BlazyManagerBase:: |
public | function | Returns the cache. | |
BlazyManagerBase:: |
public | function | Return the cache metadata common for all blazy-related modules. | |
BlazyManagerBase:: |
public | function | Returns the common UI settings inherited down to each item. | |
BlazyManagerBase:: |
public | function | Returns the config factory. | |
BlazyManagerBase:: |
public | function | Returns the entity repository service. | |
BlazyManagerBase:: |
public | function | Returns the entity type manager. | |
BlazyManagerBase:: |
public | function |
Returns the supported image effects. Overrides BlazyManagerInterface:: |
|
BlazyManagerBase:: |
public | function |
Returns drupalSettings for IO. Overrides BlazyManagerInterface:: |
|
BlazyManagerBase:: |
public | function |
Gets the supported lightboxes. Overrides BlazyManagerInterface:: |
|
BlazyManagerBase:: |
public | function | Returns the module handler. | |
BlazyManagerBase:: |
public | function | Returns the renderer. | |
BlazyManagerBase:: |
public | function | Returns the Responsive image styles and caches tags. | |
BlazyManagerBase:: |
public | function |
Checks for Blazy formatter such as from within a Views style plugin. Overrides BlazyManagerInterface:: |
|
BlazyManagerBase:: |
protected | function | Collects the first found Blazy formatter settings within Views fields. | |
BlazyManagerBase:: |
public | function | Returns the language manager service. | |
BlazyManagerBase:: |
public | function | Returns the app root. | |
BlazyManagerBase:: |
protected | function | Provides attachments and cache common for all blazy-related modules. | |
BlazyManagerBase:: |
public | function | Sets the language manager service. | |
BlazyManagerBase:: |
public | function | Sets dimensions once to reduce method calls for Responsive image. | |
BlazyManagerBase:: |
public | function | Sets app root service. | |
BlazyManagerBase:: |
public | function | Constructs a BlazyManager object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. |