3 namespace Drupal\Core\Field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Render\Element;
10 * Base class for 'Field formatter' plugin implementations.
12 * @ingroup field_formatter
14 abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface {
17 * The field definition.
19 * @var \Drupal\Core\Field\FieldDefinitionInterface
21 protected $fieldDefinition;
24 * The formatter settings.
31 * The label display setting.
45 * Constructs a FormatterBase object.
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
59 * @param array $third_party_settings
60 * Any third party settings.
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);
65 $this->fieldDefinition = $field_definition;
66 $this->settings = $settings;
67 $this->label = $label;
68 $this->viewMode = $view_mode;
69 $this->thirdPartySettings = $third_party_settings;
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();
80 $elements = $this->viewElements($items, $langcode);
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();
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(),
101 '#formatter' => $this->getPluginId(),
102 '#is_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(),
105 $elements = array_merge($info, $elements);
114 public function settingsForm(array $form, FormStateInterface $form_state) {
121 public function settingsSummary() {
128 public function prepareView(array $entities_items) {}
131 * Returns the array of field settings.
134 * The array of settings.
136 protected function getFieldSettings() {
137 return $this->fieldDefinition->getSettings();
141 * Returns the value of a field setting.
143 * @param string $setting_name
149 protected function getFieldSetting($setting_name) {
150 return $this->fieldDefinition->getSetting($setting_name);
156 public static function isApplicable(FieldDefinitionInterface $field_definition) {
157 // By default, formatters are available for all fields.