FormatterInterface.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Form\FormStateInterface;
  4. /**
  5. * Interface definition for field formatter plugins.
  6. *
  7. * @ingroup field_formatter
  8. */
  9. interface FormatterInterface extends PluginSettingsInterface {
  10. /**
  11. * Returns a form to configure settings for the formatter.
  12. *
  13. * Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow
  14. * administrators to configure the formatter. The field_ui module takes care
  15. * of handling submitted form values.
  16. *
  17. * @param array $form
  18. * The form where the settings form is being included in.
  19. * @param \Drupal\Core\Form\FormStateInterface $form_state
  20. * The current state of the form.
  21. *
  22. * @return array
  23. * The form elements for the formatter settings.
  24. */
  25. public function settingsForm(array $form, FormStateInterface $form_state);
  26. /**
  27. * Returns a short summary for the current formatter settings.
  28. *
  29. * If an empty result is returned, a UI can still be provided to display
  30. * a settings form in case the formatter has configurable settings.
  31. *
  32. * @return string[]
  33. * A short summary of the formatter settings.
  34. */
  35. public function settingsSummary();
  36. /**
  37. * Allows formatters to load information for field values being displayed.
  38. *
  39. * This should be used when a formatter needs to load additional information
  40. * from the database in order to render a field, for example a reference
  41. * field that displays properties of the referenced entities such as name or
  42. * type.
  43. *
  44. * This method operates on multiple entities. The $entities_items parameter
  45. * is an array keyed by entity ID. For performance reasons, information for
  46. * all involved entities should be loaded in a single query where possible.
  47. *
  48. * Changes or additions to field values are done by directly altering the
  49. * items.
  50. *
  51. * @param \Drupal\Core\Field\FieldItemListInterface[] $entities_items
  52. * An array with the field values from the multiple entities being rendered.
  53. */
  54. public function prepareView(array $entities_items);
  55. /**
  56. * Builds a renderable array for a fully themed field.
  57. *
  58. * @param \Drupal\Core\Field\FieldItemListInterface $items
  59. * The field values to be rendered.
  60. * @param string $langcode
  61. * (optional) The language that should be used to render the field. Defaults
  62. * to the current content language.
  63. *
  64. * @return array
  65. * A renderable array for a themed field with its label and all its values.
  66. */
  67. public function view(FieldItemListInterface $items, $langcode = NULL);
  68. /**
  69. * Builds a renderable array for a field value.
  70. *
  71. * @param \Drupal\Core\Field\FieldItemListInterface $items
  72. * The field values to be rendered.
  73. * @param string $langcode
  74. * The language that should be used to render the field.
  75. *
  76. * @return array
  77. * A renderable array for $items, as an array of child elements keyed by
  78. * consecutive numeric indexes starting from 0.
  79. */
  80. public function viewElements(FieldItemListInterface $items, $langcode);
  81. /**
  82. * Returns if the formatter can be used for the provided field.
  83. *
  84. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  85. * The field definition that should be checked.
  86. *
  87. * @return bool
  88. * TRUE if the formatter can be used, FALSE otherwise.
  89. */
  90. public static function isApplicable(FieldDefinitionInterface $field_definition);
  91. }