Version 1
[yaffs-website] / web / core / modules / field / tests / modules / field_test / src / Plugin / Field / FieldFormatter / TestFieldMultipleFormatter.php
1 <?php
2
3 namespace Drupal\field_test\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FormatterBase;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the 'field_test_multiple' formatter.
11  *
12  * @FieldFormatter(
13  *   id = "field_test_multiple",
14  *   label = @Translation("Multiple"),
15  *   description = @Translation("Multiple formatter"),
16  *   field_types = {
17  *     "test_field",
18  *     "test_field_with_preconfigured_options"
19  *   },
20  *   weight = 5
21  * )
22  */
23 class TestFieldMultipleFormatter extends FormatterBase {
24
25   /**
26    * {@inheritdoc}
27    */
28   public static function defaultSettings() {
29     return [
30       'test_formatter_setting_multiple' => 'dummy test string',
31       'alter' => FALSE,
32     ] + parent::defaultSettings();
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function settingsForm(array $form, FormStateInterface $form_state) {
39     $element['test_formatter_setting_multiple'] = [
40       '#title' => t('Setting'),
41       '#type' => 'textfield',
42       '#size' => 20,
43       '#default_value' => $this->getSetting('test_formatter_setting_multiple'),
44       '#required' => TRUE,
45     ];
46     return $element;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function settingsSummary() {
53     $summary = [];
54     $summary[] = t('@setting: @value', ['@setting' => 'test_formatter_setting_multiple', '@value' => $this->getSetting('test_formatter_setting_multiple')]);
55     return $summary;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function viewElements(FieldItemListInterface $items, $langcode) {
62     $elements = [];
63
64     if (!empty($items)) {
65       $array = [];
66       foreach ($items as $delta => $item) {
67         $array[] = $delta . ':' . $item->value;
68       }
69       $elements[0] = ['#markup' => $this->getSetting('test_formatter_setting_multiple') . '|' . implode('|', $array)];
70     }
71
72     return $elements;
73   }
74
75 }