TestFieldEmptyFormatter.php in Zircon Profile 8.0
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 array(
'test_empty_string' => '**EMPTY FIELD**',
) + parent::defaultSettings();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
if ($items
->isEmpty()) {
$elements[0] = array(
'#markup' => $this
->getSetting('test_empty_string'),
);
}
else {
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#markup' => $item->value,
);
}
}
return $elements;
}
}