View source
<?php
namespace Drupal\slick\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\slick\SlickFormatterInterface;
use Drupal\slick\SlickManagerInterface;
use Drupal\slick\SlickDefault;
class SlickTextFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
use SlickFormatterTrait;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, SlickFormatterInterface $formatter, SlickManagerInterface $manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$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('slick.formatter'), $container
->get('slick.manager'));
}
public static function defaultSettings() {
return SlickDefault::baseSettings();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
if ($items
->isEmpty()) {
return [];
}
$settings = $this
->getSettings();
$settings['vanilla'] = TRUE;
$build = [
'settings' => $settings,
];
$this->formatter
->buildSettings($build, $items);
foreach ($items as $key => $item) {
$element = [
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $item->format,
'#langcode' => $item
->getLangcode(),
];
$build['items'][$key] = $element;
unset($element);
}
return $this
->manager()
->build($build);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$definition = $this
->getScopedFormElements();
$this
->admin()
->buildSettingsForm($element, $definition);
return $element;
}
public function getScopedFormElements() {
return [
'current_view_mode' => $this->viewMode,
'no_layouts' => TRUE,
'plugin_id' => $this
->getPluginId(),
'settings' => $this
->getSettings(),
];
}
}