GridStackEntityFormatterBase.php in GridStack 8.2
File
src/Plugin/Field/FieldFormatter/GridStackEntityFormatterBase.php
View source
<?php
namespace Drupal\gridstack\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\blazy\Dejavu\BlazyEntityBase;
use Drupal\gridstack\GridStackDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class GridStackEntityFormatterBase extends BlazyEntityBase {
use GridStackFormatterTrait {
buildSettings as traitBuildSettings;
}
use GridStackFormatterViewTrait;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return self::injectServices($instance, $container, 'entity');
}
public static function defaultSettings() {
$settings = GridStackDefault::baseSettings();
$settings['view_mode'] = '';
return $settings;
}
public function buildElement(array &$build, $entity, $langcode) {
$view_mode = empty($build['settings']['view_mode']) ? 'full' : $build['settings']['view_mode'];
$delta = $build['settings']['delta'];
$build['items'][$delta] = [
'box' => $this
->formatter()
->getEntityTypeManager()
->getViewBuilder($entity
->getEntityTypeId())
->view($entity, $view_mode, $langcode),
];
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$entities = $this
->getEntitiesToView($items, $langcode);
if (empty($entities)) {
return [];
}
return $this
->commonViewElements($items, $langcode, $entities);
}
public function buildSettings() {
return [
'vanilla' => TRUE,
] + $this
->traitBuildSettings();
}
public function getScopedFormElements() {
return $this
->getCommonScopedFormElements() + parent::getScopedFormElements();
}
}