BlazyFormatterTrait.php in Blazy 8.2
File
src/Plugin/Field/FieldFormatter/BlazyFormatterTrait.php
View source
<?php
namespace Drupal\blazy\Plugin\Field\FieldFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;
trait BlazyFormatterTrait {
protected $formatter;
protected $blazyManager;
public function formatter() {
return $this->formatter;
}
public function blazyManager() {
return $this->blazyManager;
}
public function admin() {
return \Drupal::service('blazy.admin.formatter');
}
protected static function injectServices($instance, ContainerInterface $container, $type = '') {
$instance->formatter = $instance->blazyManager = $container
->get('blazy.formatter');
if ($type == 'image' || $type == 'entity') {
$instance->imageFactory = isset($instance->imageFactory) ? $instance->imageFactory : $container
->get('image.factory');
if ($type == 'entity') {
$instance->loggerFactory = isset($instance->loggerFactory) ? $instance->loggerFactory : $container
->get('logger.factory');
$instance->blazyEntity = isset($instance->blazyEntity) ? $instance->blazyEntity : $container
->get('blazy.entity');
$instance->blazyOembed = isset($instance->blazyOembed) ? $instance->blazyOembed : $instance->blazyEntity
->oembed();
}
}
return $instance;
}
public function settingsSummary() {
return $this
->admin()
->getSettingsSummary($this
->getScopedFormElements());
}
public function buildSettings() {
$settings = array_merge($this
->getCommonFieldDefinition(), $this
->getSettings());
$settings['blazy'] = TRUE;
$settings['item_id'] = $settings['lazy'] = 'blazy';
$settings['_grid'] = !empty($settings['style']) && !empty($settings['grid']);
$settings['third_party'] = $this
->getThirdPartySettings();
$settings['label'] = $this->fieldDefinition
->getLabel();
$settings['label_display'] = $this->label;
return $settings;
}
public function getCommonFieldDefinition() {
$field = $this->fieldDefinition;
return [
'namespace' => 'blazy',
'current_view_mode' => $this->viewMode,
'field_name' => $field
->getName(),
'field_type' => $field
->getType(),
'entity_type' => $field
->getTargetEntityTypeId(),
'plugin_id' => $this
->getPluginId(),
'target_type' => $this
->getFieldSetting('target_type'),
];
}
public function getCommonScopedFormElements() {
return [
'settings' => $this
->getSettings(),
] + $this
->getCommonFieldDefinition();
}
}