1 {% extends "base/class.php.twig" %}
4 \Drupal\{{ module }}\Plugin\Field\FieldFormatter\{{ class_name }}.
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Plugin\Field\FieldFormatter;
12 use Drupal\Component\Utility\Html;
13 use Drupal\Core\Field\FieldItemInterface;
14 use Drupal\Core\Field\FieldItemListInterface;
15 use Drupal\Core\Field\FormatterBase;
16 use Drupal\Core\Form\FormStateInterface;
19 {% block class_declaration %}
21 * Plugin implementation of the '{{ plugin_id }}' formatter.
24 * id = "{{ plugin_id }}",
25 * label = @Translation("{{ label }}"){% if field_type %},
31 * At least one field_types annotation array entry is necessary to display this formatter in the UI.
32 * ex. field_types = { "field_type" }
36 class {{ class_name }} extends FormatterBase {% endblock %}
37 {% block class_methods %}
41 public static function defaultSettings() {
43 // Implement default settings.
44 ] + parent::defaultSettings();
50 public function settingsForm(array $form, FormStateInterface $form_state) {
52 // Implement settings form.
53 ] + parent::settingsForm($form, $form_state);
59 public function settingsSummary() {
61 // Implement settings summary.
69 public function viewElements(FieldItemListInterface $items, $langcode) {
72 foreach ($items as $delta => $item) {
73 $elements[$delta] = ['#markup' => $this->viewValue($item)];
80 * Generate the output appropriate for one field item.
82 * @param \Drupal\Core\Field\FieldItemInterface $item
86 * The textual output generated.
88 protected function viewValue(FieldItemInterface $item) {
89 // The text value has no text format assigned to it, so the user input
90 // should equal the output, including newlines.
91 return nl2br(Html::escape($item->value));