EntityViewBuilderInterface.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Field\FieldItemInterface;
  4. use Drupal\Core\Field\FieldItemListInterface;
  5. /**
  6. * Defines an interface for entity view builders.
  7. *
  8. * @ingroup entity_api
  9. */
  10. interface EntityViewBuilderInterface {
  11. /**
  12. * Builds the component fields and properties of a set of entities.
  13. *
  14. * @param &$build
  15. * The renderable array representing the entity content.
  16. * @param \Drupal\Core\Entity\EntityInterface[] $entities
  17. * The entities whose content is being built.
  18. * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] $displays
  19. * The array of entity view displays holding the display options
  20. * configured for the entity components, keyed by bundle name.
  21. * @param string $view_mode
  22. * The view mode in which the entity is being viewed.
  23. */
  24. public function buildComponents(array &$build, array $entities, array $displays, $view_mode);
  25. /**
  26. * Builds the render array for the provided entity.
  27. *
  28. * @param \Drupal\Core\Entity\EntityInterface $entity
  29. * The entity to render.
  30. * @param string $view_mode
  31. * (optional) The view mode that should be used to render the entity.
  32. * @param string $langcode
  33. * (optional) For which language the entity should be rendered, defaults to
  34. * the current content language.
  35. *
  36. * @return array
  37. * A render array for the entity.
  38. *
  39. * @throws \InvalidArgumentException
  40. * Can be thrown when the set of parameters is inconsistent, like when
  41. * trying to view a Comment and passing a Node which is not the one the
  42. * comment belongs to, or not passing one, and having the comment node not
  43. * be available for loading.
  44. */
  45. public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL);
  46. /**
  47. * Builds the render array for the provided entities.
  48. *
  49. * @param array $entities
  50. * An array of entities implementing EntityInterface to view.
  51. * @param string $view_mode
  52. * (optional) The view mode that should be used to render the entity.
  53. * @param string $langcode
  54. * (optional) For which language the entity should be rendered, defaults to
  55. * the current content language.
  56. *
  57. * @return
  58. * A render array for the entities, indexed by the same keys as the
  59. * entities array passed in $entities.
  60. *
  61. * @throws \InvalidArgumentException
  62. * Can be thrown when the set of parameters is inconsistent, like when
  63. * trying to view Comments and passing a Node which is not the one the
  64. * comments belongs to, or not passing one, and having the comments node not
  65. * be available for loading.
  66. */
  67. public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL);
  68. /**
  69. * Resets the entity render cache.
  70. *
  71. * @param \Drupal\Core\Entity\EntityInterface[] $entities
  72. * (optional) If specified, the cache is reset for the given entities only.
  73. */
  74. public function resetCache(array $entities = NULL);
  75. /**
  76. * Builds a renderable array for the value of a single field in an entity.
  77. *
  78. * The resulting output is a fully themed field with label and multiple
  79. * values.
  80. *
  81. * This function can be used by third-party modules that need to output an
  82. * isolated field.
  83. * - Do not use inside node (or any other entity) templates; use
  84. * render($content[FIELD_NAME]) instead.
  85. * - The FieldItemInterface::view() method can be used to output a single
  86. * formatted field value, without label or wrapping field markup.
  87. *
  88. * The function takes care of invoking the prepare_view steps. It also
  89. * respects field access permissions.
  90. *
  91. * @param \Drupal\Core\Field\FieldItemListInterface $items
  92. * FieldItemList containing the values to be displayed.
  93. * @param string|array $display_options
  94. * Can be either:
  95. * - The name of a view mode. The field will be displayed according to the
  96. * display settings specified for this view mode in the $field
  97. * definition for the field in the entity's bundle. If no display settings
  98. * are found for the view mode, the settings for the 'default' view mode
  99. * will be used.
  100. * - An array of display options. The following key/value pairs are allowed:
  101. * - label: (string) Position of the label. The default 'field' theme
  102. * implementation supports the values 'inline', 'above' and 'hidden'.
  103. * Defaults to 'above'.
  104. * - type: (string) The formatter to use. Defaults to the
  105. * 'default_formatter' for the field type. The default formatter will
  106. * also be used if the requested formatter is not available.
  107. * - settings: (array) Settings specific to the formatter. Defaults to the
  108. * formatter's default settings.
  109. * - weight: (float) The weight to assign to the renderable element.
  110. * Defaults to 0.
  111. *
  112. * @return array
  113. * A renderable array for the field values.
  114. *
  115. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewFieldItem()
  116. */
  117. public function viewField(FieldItemListInterface $items, $display_options = []);
  118. /**
  119. * Builds a renderable array for a single field item.
  120. *
  121. * @param \Drupal\Core\Field\FieldItemInterface $item
  122. * FieldItem to be displayed.
  123. * @param string|array $display_options
  124. * Can be either the name of a view mode, or an array of display settings.
  125. * See EntityViewBuilderInterface::viewField() for more information.
  126. *
  127. * @return array
  128. * A renderable array for the field item.
  129. *
  130. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewField()
  131. */
  132. public function viewFieldItem(FieldItemInterface $item, $display_options = []);
  133. /**
  134. * The cache tag associated with this entity view builder.
  135. *
  136. * An entity view builder is instantiated on a per-entity type basis, so the
  137. * cache tags are also per-entity type.
  138. *
  139. * @return array
  140. * An array of cache tags.
  141. */
  142. public function getCacheTags();
  143. }