aggregator.theme.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Preprocessors and theme functions of Aggregator module.
  5. */
  6. use Drupal\Component\Utility\UrlHelper;
  7. use Drupal\Core\Render\Element;
  8. /**
  9. * Prepares variables for aggregator item templates.
  10. *
  11. * Default template: aggregator-item.html.twig.
  12. *
  13. * By default this function performs special preprocessing to create a separate
  14. * variable for the title base field. This preprocessing is skipped if:
  15. * - a module makes the field's display configurable via the field UI by means
  16. * of BaseFieldDefinition::setDisplayConfigurable()
  17. * - AND the additional entity type property
  18. * 'enable_base_field_custom_preprocess_skipping' has been set using
  19. * hook_entity_type_build().
  20. *
  21. * @param array $variables
  22. * An associative array containing:
  23. * - elements: An array of elements to display in view mode.
  24. */
  25. function template_preprocess_aggregator_item(&$variables) {
  26. $item = $variables['elements']['#aggregator_item'];
  27. // Helpful $content variable for templates.
  28. foreach (Element::children($variables['elements']) as $key) {
  29. $variables['content'][$key] = $variables['elements'][$key];
  30. }
  31. $variables['url'] = UrlHelper::stripDangerousProtocols($item->getLink());
  32. // Make title field available separately. Skip this custom preprocessing if
  33. // the field display is configurable and skipping has been enabled.
  34. // @todo https://www.drupal.org/project/drupal/issues/3015623
  35. // Eventually delete this code and matching template lines. Using
  36. // $variables['content'] is more flexible and consistent.
  37. $skip_custom_preprocessing = $item->getEntityType()->get('enable_base_field_custom_preprocess_skipping');
  38. if (!$skip_custom_preprocessing || !$item->getFieldDefinition('title')->isDisplayConfigurable('view')) {
  39. $variables['title'] = $item->label();
  40. }
  41. }
  42. /**
  43. * Prepares variables for aggregator feed templates.
  44. *
  45. * Default template: aggregator-feed.html.twig.
  46. *
  47. * By default this function performs special preprocessing to create a separate
  48. * variable for the title base field. This preprocessing is skipped if:
  49. * - a module makes the field's display configurable via the field UI by means
  50. * of BaseFieldDefinition::setDisplayConfigurable()
  51. * - AND the additional entity type property
  52. * 'enable_base_field_custom_preprocess_skipping' has been set using
  53. * hook_entity_type_build().
  54. *
  55. * @param array $variables
  56. * An associative array containing:
  57. * - elements: An array of elements to display in view mode.
  58. */
  59. function template_preprocess_aggregator_feed(&$variables) {
  60. $feed = $variables['elements']['#aggregator_feed'];
  61. // Helpful $content variable for templates.
  62. foreach (Element::children($variables['elements']) as $key) {
  63. $variables['content'][$key] = $variables['elements'][$key];
  64. }
  65. $variables['full'] = $variables['elements']['#view_mode'] == 'full';
  66. // Make title field available separately. Skip this custom preprocessing if
  67. // the field display is configurable and skipping has been enabled.
  68. // @todo https://www.drupal.org/project/drupal/issues/3015623
  69. // Eventually delete this code and matching template lines. Using
  70. // $variables['content'] is more flexible and consistent.
  71. $skip_custom_preprocessing = $feed->getEntityType()->get('enable_base_field_custom_preprocess_skipping');
  72. if (!$skip_custom_preprocessing || !$feed->getFieldDefinition('title')->isDisplayConfigurable('view')) {
  73. $variables['title'] = $feed->label();
  74. }
  75. }