3b3abcc61999fe7cad99797a5f86dd1265a4dd34
[yaffs-website] / web / core / lib / Drupal / Core / Field / FormatterBase.php
1 <?php
2
3 namespace Drupal\Core\Field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Render\Element;
8
9 /**
10  * Base class for 'Field formatter' plugin implementations.
11  *
12  * @ingroup field_formatter
13  */
14 abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface {
15
16   /**
17    * The field definition.
18    *
19    * @var \Drupal\Core\Field\FieldDefinitionInterface
20    */
21   protected $fieldDefinition;
22
23   /**
24    * The formatter settings.
25    *
26    * @var array
27    */
28   protected $settings;
29
30   /**
31    * The label display setting.
32    *
33    * @var string
34    */
35   protected $label;
36
37   /**
38    * The view mode.
39    *
40    * @var string
41    */
42   protected $viewMode;
43
44   /**
45    * Constructs a FormatterBase object.
46    *
47    * @param string $plugin_id
48    *   The plugin_id for the formatter.
49    * @param mixed $plugin_definition
50    *   The plugin implementation definition.
51    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
52    *   The definition of the field to which the formatter is associated.
53    * @param array $settings
54    *   The formatter settings.
55    * @param string $label
56    *   The formatter label display setting.
57    * @param string $view_mode
58    *   The view mode.
59    * @param array $third_party_settings
60    *   Any third party settings.
61    */
62   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) {
63     parent::__construct([], $plugin_id, $plugin_definition);
64
65     $this->fieldDefinition = $field_definition;
66     $this->settings = $settings;
67     $this->label = $label;
68     $this->viewMode = $view_mode;
69     $this->thirdPartySettings = $third_party_settings;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function view(FieldItemListInterface $items, $langcode = NULL) {
76     // Default the language to the current content language.
77     if (empty($langcode)) {
78       $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
79     }
80     $elements = $this->viewElements($items, $langcode);
81
82     // If there are actual renderable children, use #theme => field, otherwise,
83     // let access cacheability metadata pass through for correct bubbling.
84     if (Element::children($elements)) {
85       $entity = $items->getEntity();
86       $entity_type = $entity->getEntityTypeId();
87       $field_name = $this->fieldDefinition->getName();
88       $info = [
89         '#theme' => 'field',
90         '#title' => $this->fieldDefinition->getLabel(),
91         '#label_display' => $this->label,
92         '#view_mode' => $this->viewMode,
93         '#language' => $items->getLangcode(),
94         '#field_name' => $field_name,
95         '#field_type' => $this->fieldDefinition->getType(),
96         '#field_translatable' => $this->fieldDefinition->isTranslatable(),
97         '#entity_type' => $entity_type,
98         '#bundle' => $entity->bundle(),
99         '#object' => $entity,
100         '#items' => $items,
101         '#formatter' => $this->getPluginId(),
102         '#is_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(),
103       ];
104
105       $elements = array_merge($info, $elements);
106     }
107
108     return $elements;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function settingsForm(array $form, FormStateInterface $form_state) {
115     return [];
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function settingsSummary() {
122     return [];
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function prepareView(array $entities_items) { }
129
130   /**
131    * Returns the array of field settings.
132    *
133    * @return array
134    *   The array of settings.
135    */
136   protected function getFieldSettings() {
137     return $this->fieldDefinition->getSettings();
138   }
139
140   /**
141    * Returns the value of a field setting.
142    *
143    * @param string $setting_name
144    *   The setting name.
145    *
146    * @return mixed
147    *   The setting value.
148    */
149   protected function getFieldSetting($setting_name) {
150     return $this->fieldDefinition->getSetting($setting_name);
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public static function isApplicable(FieldDefinitionInterface $field_definition) {
157     // By default, formatters are available for all fields.
158     return TRUE;
159   }
160
161 }