I found a couple of ways to achieve it, posting here, but I'm open to better/cleaner ways. I'm especially unhappy in my answers with the way to access the tag name - seems so overly complicated trying to get the raw name?!
Both code blocks live in a mytheme_preprocess_node()
:
Answer 1: create a new variable with all the tags in it:
<?php
$allTags = [];
$addTags = function($fieldName) use (&$allTags, $variables) {
if (isset($variables['content'][$fieldName])) {
foreach (\Drupal\Core\Render\Element::children($variables['content'][$fieldName]) as $i) {
$allTags[]= $variables['content'][$fieldName][$i];
}
}
};
$addTags('field_colours');
$addTags('field_tags');
usort($allTags, function ($a, $b) {
$tagA = $a['#taxonomy_term']->name->getValue()[0]['value'];
$tagB = $b['#taxonomy_term']->name->getValue()[0]['value'];
return strcasecmp($tagA, $tagB);
});
$variables['content']['allTags'] = $allTags;
Then in Twig for the node template: {{ allTags }}
.
Answer 2: mix the tags from the colour field into the tags field in the render array
<?php
$allTagsTemp = [];
foreach (\Drupal\Core\Render\Element::children($variables['content']['field_tags']) as $i) {
$tag = $variables['content']['field_tags'][$i];
unset($tag['#weight']);
unset($variables['content']['field_tags'][$i]);
$allTagsTemp[] = $tag;
}
foreach (\Drupal\Core\Render\Element::children($variables['content']['field_colours']) as $i) {
$tag = $variables['content']['field_colours'][$i];
unset($tag['#weight']);
unset($variables['content']['field_colours'][$i]);
$allTagsTemp[] = $tag;
}
usort($allTagsTemp, function ($a, $b) {
$tagA = $a['#taxonomy_term']->name->getValue()[0]['value'];
$tagB = $b['#taxonomy_term']->name->getValue()[0]['value'];
return strcasecmp($tagA, $tagB);
});
$variables['content']['field_tags'] += $allTagsTemp;
Then in Twig {{ field_tags }}
.