Refactor addition of authors and date to use proper formatting

Ref #239
This commit is contained in:
Keegan Rankin 2023-07-03 19:05:56 -07:00
parent e1092392b1
commit 31a43c1890

View file

@ -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;
}
}
}
}