PlaceholderGeneratorInterface.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Drupal\Core\Render;
  3. /**
  4. * Defines an interface for turning a render array into a placeholder.
  5. *
  6. * This encapsulates logic related to generating placeholders.
  7. *
  8. * Makes it possible to determine whether a render array can be placeholdered
  9. * (it can be reconstructed independently of the request context), whether a
  10. * render array should be placeholdered (its cacheability meets the conditions),
  11. * and to create a placeholder.
  12. *
  13. * @see \Drupal\Core\Render\RendererInterface
  14. */
  15. interface PlaceholderGeneratorInterface {
  16. /**
  17. * Analyzes whether the given render array can be placeholdered.
  18. *
  19. * @param array $element
  20. * A render array. Its #lazy_builder and #create_placeholder properties are
  21. * analyzed.
  22. *
  23. * @return bool
  24. */
  25. public function canCreatePlaceholder(array $element);
  26. /**
  27. * Whether the given render array should be automatically placeholdered.
  28. *
  29. * The render array should be placeholdered if its cacheability either has a
  30. * cache context with too high cardinality, a cache tag with a too high
  31. * invalidation rate, or a max-age that is too low. Either of these would make
  32. * caching ineffective, and thus we choose to placeholder instead.
  33. *
  34. * @param array $element
  35. * The render array whose cacheability to analyze.
  36. *
  37. * @return bool
  38. * Whether the given render array's cacheability meets the placeholdering
  39. * conditions.
  40. */
  41. public function shouldAutomaticallyPlaceholder(array $element);
  42. /**
  43. * Turns the given element into a placeholder.
  44. *
  45. * Placeholdering allows us to avoid "poor cacheability contamination": this
  46. * maps the current render array to one that only has #markup and #attached,
  47. * and #attached contains a placeholder with this element's prior cacheability
  48. * metadata. In other words: this placeholder is perfectly cacheable, the
  49. * placeholder replacement logic effectively cordons off poor cacheability.
  50. *
  51. * @param array $element
  52. * The render array to create a placeholder for.
  53. *
  54. * @return array
  55. * Render array with placeholder markup and the attached placeholder
  56. * replacement metadata.
  57. */
  58. public function createPlaceholder(array $element);
  59. }