lory-8.x-1.x-dev/src/Plugin/Field/FieldFormatter/LoryTextFormatter.php
src/Plugin/Field/FieldFormatter/LoryTextFormatter.php
<?php
namespace Drupal\lory\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\lory\LoryDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'Lory Text' formatter.
*
* @FieldFormatter(
* id = "lory_text",
* label = @Translation("Lory Text"),
* field_types = {
* "text",
* "text_long",
* "text_with_summary",
* },
* quickedit = {"editor" = "disabled"}
* )
*/
class LoryTextFormatter extends FormatterBase {
use LoryFormatterViewTrait;
use LoryFormatterTrait {
buildSettings as traitBuildSettings;
}
/**
* {@inheritdoc}
*/
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, 'text');
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return LoryDefault::baseSettings() + LoryDefault::gridSettings();
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
return $this->commonViewElements($items, $langcode);
}
/**
* Build the lory carousel elements.
*/
public function buildElements(array &$build, $items) {
// The ProcessedText element already handles cache context & tag bubbling.
// @see \Drupal\filter\Element\ProcessedText::preRenderText()
foreach ($items as $key => $item) {
$element = [
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $item->format,
'#langcode' => $item->getLangcode(),
];
$build['items'][$key] = $element;
unset($element);
}
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$definition = $this->getScopedFormElements();
$this->admin()->buildSettingsForm($element, $definition);
return $element;
}
/**
* Builds the settings.
*/
public function buildSettings() {
return ['vanilla' => TRUE] + $this->traitBuildSettings();
}
/**
* Defines the scope for the form elements.
*/
public function getScopedFormElements() {
return [
'no_image_style' => TRUE,
'no_layouts' => TRUE,
'responsive_image' => FALSE,
] + $this->getCommonScopedFormElements();
}
}
