View source
<?php
namespace Drupal\paragraphs_grid\Plugin\Field\FieldFormatter;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Template\Attribute;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter;
class ParagraphsGridFormatter extends EntityReferenceRevisionsEntityFormatter implements ContainerFactoryPluginInterface {
protected $configFactory;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactoryInterface $logger_factory, EntityDisplayRepositoryInterface $entity_display_repository, ConfigFactory $config_factory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $logger_factory, $entity_display_repository);
$this->configFactory = $config_factory;
}
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('entity_display.repository'), $container
->get('config.factory'));
}
public static function defaultSettings() {
return [
'container' => 'default',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$module_config = $this->configFactory
->get('paragraphs_grid.settings');
$grid_config = $this->configFactory
->get($module_config
->get('gridtype'));
$elements['container'] = [
'#type' => 'select',
'#options' => $grid_config
->get('wrapper.grid.options'),
'#title' => $this
->t('Select grid container'),
'#default_value' => $this
->getSetting('container'),
'#required' => $grid_config
->get('wrapper.grid.required'),
];
return $elements;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$module_config = $this->configFactory
->get('paragraphs_grid.settings');
$grid_config = $this->configFactory
->get($module_config
->get('gridtype'));
$options = $grid_config
->get('wrapper.grid.options');
$container = $this
->getSetting('container');
$container = isset($options[$container]) ? $options[$container] : $container;
$summary[] = $this
->t('Grid container: @container', [
'@container' => $container,
]);
return $summary;
}
public function view(FieldItemListInterface $items, $langcode = NULL) {
$elements = parent::view($items, $langcode);
if ($this
->getSetting('container') !== 'none') {
$elements['#container_attributes'] = new Attribute([
'class' => [
$this
->getSetting('container'),
],
]);
}
return $elements;
}
}