FormatterBase.php 4.1 KB

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