c8072177f16ec841d3ef304155e94fc2ebb7a795
[yaffs-website] / web / core / modules / field / tests / modules / field_test / src / Plugin / Field / FieldFormatter / TestFieldEmptyFormatter.php
1 <?php
2
3 namespace Drupal\field_test\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\FormatterBase;
7
8 /**
9  * Plugin implementation of the 'field_empty_test' formatter.
10  *
11  * @FieldFormatter(
12  *   id = "field_empty_test",
13  *   label = @Translation("Field empty test"),
14  *   field_types = {
15  *     "test_field",
16  *   },
17  *   weight = -5
18  * )
19  */
20 class TestFieldEmptyFormatter extends FormatterBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     return [
27       'test_empty_string' => '**EMPTY FIELD**',
28     ] + parent::defaultSettings();
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function viewElements(FieldItemListInterface $items, $langcode) {
35     $elements = [];
36
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')];
40     }
41     else {
42       foreach ($items as $delta => $item) {
43         // This formatter only needs to output raw for testing.
44         $elements[$delta] = ['#markup' => $item->value];
45       }
46     }
47
48     return $elements;
49   }
50
51 }