View source
<?php
namespace Drupal\gridstack\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\blazy\Dejavu\BlazyEntityBase;
use Drupal\gridstack\GridStackFormatterInterface;
use Drupal\gridstack\GridStackManagerInterface;
use Drupal\gridstack\GridStackDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class GridStackEntityFormatterBase extends BlazyEntityBase implements ContainerFactoryPluginInterface {
use GridStackFormatterTrait;
protected $loggerFactory;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactoryInterface $logger_factory, GridStackFormatterInterface $formatter, GridStackManagerInterface $manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->loggerFactory = $logger_factory;
$this->formatter = $formatter;
$this->manager = $manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('logger.factory'), $container
->get('gridstack.formatter'), $container
->get('gridstack.manager'));
}
public function blazyManager() {
return $this->formatter;
}
public static function defaultSettings() {
$settings = GridStackDefault::baseSettings();
$settings['view_mode'] = '';
return $settings;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$entities = $this
->getEntitiesToView($items, $langcode);
if (empty($entities)) {
return [];
}
$settings = $this
->buildSettings();
$build = [
'settings' => $settings,
];
$this->formatter
->buildSettings($build, $items);
$this
->buildElements($build, $entities, $langcode);
return $this
->manager()
->build($build);
}
public function buildSettings() {
$settings = $this
->getSettings();
$settings['blazy'] = TRUE;
$settings['lazy'] = 'blazy';
$settings['namespace'] = $settings['item_id'] = 'gridstack';
$settings['plugin_id'] = $this
->getPluginId();
$settings['vanilla'] = TRUE;
return $settings;
}
public function getScopedFormElements() {
return [
'namespace' => 'gridstack',
] + parent::getScopedFormElements();
}
}