class BlazyFormatterBlazy in Blazy 7
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/BlazyFormatterBlazy.php \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterBlazy
Plugin implementation of the `Blazy File` or `Blazy Image` for Blazy only.
Hierarchy
- class \Drupal\blazy\Plugin\Field\FieldFormatter\FormatterBase implements FormatterInterface
- class \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterBase
- class \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterBlazy
- class \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterBase
Expanded class hierarchy of BlazyFormatterBlazy
See also
\Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFileFormatter
\Drupal\blazy\Plugin\Field\FieldFormatter\BlazyImageFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ BlazyFormatterBlazy.php, line 14
Namespace
Drupal\blazy\Plugin\Field\FieldFormatterView source
class BlazyFormatterBlazy extends BlazyFormatterBase {
/**
* Constructs a BlazyFormatter instance.
*/
public function __construct($plugin_id, $field, $instance, BlazyManagerInterface $formatter) {
parent::__construct($plugin_id, $field, $instance);
$this->formatter = $formatter;
}
/**
* {@inheritdoc}
*/
public function admin() {
if (!isset($this->admin)) {
$this->admin = new BlazyAdminFormatter($this->formatter);
}
return $this->admin;
}
/**
* {@inheritdoc}
*/
public function viewElements($items, $entity) {
$build = [];
$files = $this
->getEntitiesToView($items);
// Early opt-out if the field is empty.
if (empty($files)) {
return $build;
}
// Collects specific settings to this formatter.
$settings = $this
->buildSettings();
$settings['blazy'] = TRUE;
$settings['namespace'] = $settings['item_id'] = $settings['lazy'] = 'blazy';
// Build the settings.
$build = [
'settings' => $settings,
];
// Modifies settings before building elements.
$this->formatter
->preBuildElements($build, $files, $entity);
// Build the elements.
$this
->buildElements($build, $files);
// Modifies settings post building elements.
$this->formatter
->postBuildElements($build, $files, $entity);
// Pass to manager for easy updates to all Blazy formatters.
return $this->formatter
->build($build);
}
/**
* Build the Blazy elements for image and file entity/ media.
*/
public function buildElements(array &$build, $files) {
$settings = $build['settings'];
foreach ($files as $delta => $item) {
// Some settings need to be available before arriving at theme_blazy() so
// that lightboxes and media switcher can get context to work with.
// The trouble is they are not always available depending on file entity
// and other supported modules availability which is not always there.
$settings['delta'] = $delta;
$settings['type'] = isset($item->type) ? $item->type : 'image';
$settings['uri'] = isset($item->uri) ? $item->uri : '';
$box['item'] = $item;
$box['settings'] = $settings;
$this
->buildElement($box, $item, $delta);
// Build caption if so configured.
if (!empty($settings['caption'])) {
foreach ($settings['caption'] as $caption) {
if (isset($item->{$caption}) && ($caption_content = array_filter($this
->getCaption($item, $caption, $settings)))) {
$box['captions'][$caption] = $caption_content;
}
}
}
// Image with grid, responsive image, lazyLoad, and lightbox supports.
$build[$delta] = $this
->formatter()
->getBlazy($box);
unset($box);
}
}
/**
* {@inheritdoc}
*/
public function buildElement(array &$element, $entity, $delta = 0) {
// Do nothing.
}
/**
* Returns the captions.
*/
protected function getCaption($entity, $field_name, $settings) {
return [];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlazyFormatterBase:: |
public static | function |
Returns default settings. Overrides FormatterBase:: |
1 |
BlazyFormatterBase:: |
protected | function | Converts $items array to object for easy D8 -> D7 backports. | 1 |
BlazyFormatterBase:: |
public | function |
Returns required form elements for the current formatter. Overrides FormatterBase:: |
2 |
BlazyFormatterBase:: |
public | function |
Defines the scope for the form elements. Overrides FormatterBase:: |
2 |
BlazyFormatterBase:: |
public | function |
Implements hook_field_formatter_settings_form(). Overrides FormatterBase:: |
1 |
BlazyFormatterBase:: |
public | function |
Implements hook_field_formatter_settings_summary(). Overrides FormatterBase:: |
|
BlazyFormatterBlazy:: |
public | function |
Returns the blazy admin service for blazy-related module. Overrides BlazyFormatterBase:: |
|
BlazyFormatterBlazy:: |
public | function |
Build individual item if so configured such as for file entity goodness. Overrides BlazyFormatterBase:: |
1 |
BlazyFormatterBlazy:: |
public | function | Build the Blazy elements for image and file entity/ media. | |
BlazyFormatterBlazy:: |
protected | function | Returns the captions. | 2 |
BlazyFormatterBlazy:: |
public | function |
Implements hook_field_formatter_view(). Overrides FormatterBase:: |
|
BlazyFormatterBlazy:: |
public | function |
Constructs a BlazyFormatter instance. Overrides FormatterBase:: |
|
FormatterBase:: |
protected | property | The blazy-related admin formatter service. | |
FormatterBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The field display. | |
FormatterBase:: |
protected | property | The field instance. | |
FormatterBase:: |
protected | property | The form settings. | |
FormatterBase:: |
protected | property | True if the field cardinality equals -1. | |
FormatterBase:: |
protected | property | The known hard-coded entities. | |
FormatterBase:: |
protected | property | The langcode. | |
FormatterBase:: |
protected | property | The blazy formatter plugin id. | |
FormatterBase:: |
protected | property | The formatter settings. | |
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
public | function | Builds the settings. | 1 |
FormatterBase:: |
public | function | Implements hook_field_formatter_settings_form(). | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Gets html settings. | |
FormatterBase:: |
public | function | Gets formatter plugin id. | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Gets formatter settings. | |
FormatterBase:: |
protected | function | Merges default settings values into $settings. | |
FormatterBase:: |
public | function | Sets html settings. | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Sets formatter settings. | |
FormatterBase:: |
protected | function | Setup common variables across different hooks. | |
FormatterBase:: |
public | function | Implements hook_field_formatter_view(). |