From 31a43c1890ef7bead64734a3d9048828a6bbd154 Mon Sep 17 00:00:00 2001 From: Keegan Rankin Date: Mon, 3 Jul 2023 19:05:56 -0700 Subject: [PATCH] Refactor addition of authors and date to use proper formatting Ref #239 --- web/themes/custom/agarica/agarica.theme | 31 +++++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/web/themes/custom/agarica/agarica.theme b/web/themes/custom/agarica/agarica.theme index f6e62b8..d3b1480 100644 --- a/web/themes/custom/agarica/agarica.theme +++ b/web/themes/custom/agarica/agarica.theme @@ -19,6 +19,22 @@ function agarica_preprocess_node(&$variables) { $allowed_regions = ['secondary_menu']; agarica_add_regions_to_node($allowed_regions, $variables); } + $field_body_paragraph = $variables['elements']['field_body_paragraph']; + if (isset($field_body_paragraph)) { + $max_key = max(array_map('intval', array_keys($field_body_paragraph))); + for ($i = 0; $i <= $max_key; $i++) { + if ($field_body_paragraph[$i]['#paragraph']->getType() === 'title') { + + $renderer = \Drupal::service('renderer'); + + $authors_html = $renderer->render($variables['content']['field_authors']); + $date_html = $variables['date']; + + $field_body_paragraph[$i]['#paragraph']->authors_html = $authors_html; + $field_body_paragraph[$i]['#paragraph']->date_html = $date_html; + } + } + } } /** @@ -124,20 +140,15 @@ function agarica_languague_switch_link() { } /** -* Implements hook_preprocess_HOOK() for node templates. +* Implements hook_preprocess_HOOK() for field__field_title template. */ function agarica_preprocess_field__field_title(&$variables) { $parentEntity = $variables['element']['#object']->getParentEntity(); if ($parentEntity instanceof EntityInterface && $parentEntity->getEntityTypeId() === 'node') { if (isset($parentEntity->getFields()['field_authors'])) { - $field_authors = $parentEntity->getFields()['field_authors']; - $authors = ''; - $date = $parentEntity->getCreatedTime(); - foreach ($field_authors as $author) { - $authors .= Node::load($author->getValue()['target_id'])->label() . ', '; - } - $variables['authors'] = $authors; - $variables['date'] = \Drupal::service('date.formatter')->format($date, 'custom', 'Y F j'); + + $variables['authors'] = $variables['element']['#object']->authors_html; + $variables['date'] = $variables['element']['#object']->date_html; } } -} \ No newline at end of file +}