3 namespace Drupal\field_test\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\FormatterBase;
9 * Plugin implementation of the 'field_empty_test' formatter.
12 * id = "field_empty_test",
13 * label = @Translation("Field empty test"),
20 class TestFieldEmptyFormatter extends FormatterBase {
25 public static function defaultSettings() {
27 'test_empty_string' => '**EMPTY FIELD**',
28 ] + parent::defaultSettings();
34 public function viewElements(FieldItemListInterface $items, $langcode) {
37 if ($items->isEmpty()) {
38 // For fields with no value, just add the configured "empty" value.
39 $elements[0] = ['#markup' => $this->getSetting('test_empty_string')];
42 foreach ($items as $delta => $item) {
43 // This formatter only needs to output raw for testing.
44 $elements[$delta] = ['#markup' => $item->value];