GutenbergTextFormatter.php in Gutenberg 8.2
File
src/Plugin/Field/FieldFormatter/GutenbergTextFormatter.php
View source
<?php
namespace Drupal\gutenberg\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter;
class GutenbergTextFormatter extends TextDefaultFormatter {
public static function defaultSettings() {
return [
'format' => 'gutenberg',
'content_only' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$user = \Drupal::currentUser();
$formats = filter_formats($user);
$options = [];
foreach ($formats as $format) {
$dependencies = $format
->getDependencies();
if (isset($dependencies['module']) && in_array('gutenberg', $dependencies['module'], TRUE)) {
$options[$format
->id()] = $format
->label();
}
}
$element = parent::settingsForm($form, $form_state);
$element['format'] = [
'#type' => 'select',
'#title' => $this
->t('Gutenberg format'),
'#options' => $options,
'#default_value' => $this
->getSetting('format'),
'#required' => TRUE,
];
$element['content_only'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Content only'),
'#description' => $this
->t('Render the field content without the field wrapper.'),
'#default_value' => $this
->getSetting('content_only'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Gutenberg format: @format.', [
'@format' => $this
->getSetting('format'),
]);
if ($this
->getSetting('content_only')) {
$summary[] = $this
->t('Render without field wrappers.');
}
else {
$summary[] = $this
->t('Render with field wrappers.');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $this
->getSetting('format'),
'#langcode' => $item
->getLangcode(),
];
}
return $elements;
}
public function view(FieldItemListInterface $items, $langcode = NULL) {
$elements = parent::view($items, $langcode);
if (isset($elements['#theme']) && $this
->getSetting('content_only')) {
$elements['#theme'] = 'field_gutenberg_text';
}
return $elements;
}
}