TestFieldEmptyFormatter.php in Drupal 10
File
core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldFormatter/TestFieldEmptyFormatter.php
View source
<?php
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
class TestFieldEmptyFormatter extends FormatterBase {
public static function defaultSettings() {
return [
'test_empty_string' => '**EMPTY FIELD**',
] + parent::defaultSettings();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
if ($items
->isEmpty()) {
$elements[0] = [
'#markup' => $this
->getSetting('test_empty_string'),
];
}
else {
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $item->value,
];
}
}
return $elements;
}
}