Skip to content
Snippets Groups Projects
Commit 6694c445 authored by Viktor Holovachek's avatar Viktor Holovachek
Browse files

Issue #3039982 - Add common parent

parent c22c696f
No related branches found
No related tags found
1 merge request!13Issue #3039982 - Add common parent
Pipeline #100510 passed
......@@ -7,6 +7,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
/**
* Prepares term objects for Twig template.
......@@ -19,15 +20,30 @@ function template_preprocess_hierarchical_term_formatter(array &$variables) {
$terms = [];
$variables['terms_objects'] = $variables['terms'];
/** @var \Drupal\taxonomy\TermInterface $term */
foreach ($variables['terms'] as $term) {
if ($variables['link']) {
$url = $term->toUrl();
$link = Link::fromTextAndUrl($term->label(), $url)->toRenderable();
$terms[] = \Drupal::service('renderer')->render($link);
/** @var \Drupal\taxonomy\TermInterface|[] $item */
foreach ($variables['terms'] as $item) {
if (is_array($item)) {
$group = [];
foreach ($item as $value) {
if ($variables['link']) {
$link = Link::fromTextAndUrl($value->label(), $value->toUrl())->toRenderable();
$group[] = \Drupal::service('renderer')->render($link);
}
else {
$group[] = $value->label();
}
}
$terms[] = Markup::create(implode('<span class="child-separator">, </span>', $group));
}
else {
$terms[] = $term->label();
if ($variables['link']) {
$link = Link::fromTextAndUrl($item->label(), $item->toUrl())->toRenderable();
$terms[] = \Drupal::service('renderer')->render($link);
}
else {
$terms[] = $item->label();
}
}
}
......
......@@ -81,6 +81,7 @@ class HierarchicalFormatter extends EntityReferenceFormatterBase {
private function displayOptions() {
return [
'all' => $this->t('The selected term and all of its parents'),
'grouping' => $this->t('Grouping terms with the same parent together'),
'parents' => $this->t('Just the parent terms'),
'root' => $this->t('Just the topmost/root term'),
'nonroot' => $this->t('Any non-topmost/root terms'),
......@@ -243,6 +244,39 @@ class HierarchicalFormatter extends EntityReferenceFormatterBase {
}
break;
case 'grouping':
$term_tree = array_reverse($this->taxonomyTermStorage->loadAllParents($tid));
$group = FALSE;
if (!empty($elements)) {
foreach ($elements as $key => $terms) {
// Add child to the common parent.
if ($term_tree[0]->id() == $terms['#terms'][0]->id()) {
if (isset($term_tree[1])) {
if (isset($elements[$key]['#terms'][1])) {
// Check if we already add a child.
if (is_array($elements[$key]['#terms'][1])) {
$elements[$key]['#terms'][1] = array_merge($elements[$key]['#terms'][1], [$term_tree[1]]);
}
else {
$elements[$key]['#terms'][1] = array_merge([$elements[$key]['#terms'][1]], [$term_tree[1]]);
}
}
else {
$elements[$key]['#terms'][1] = $term_tree[1];
}
}
$group = TRUE;
}
}
}
// Skip adding new element.
if ($group) {
continue 2;
}
break;
default:
$term_tree = array_reverse($this->taxonomyTermStorage->loadAllParents($tid));
break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment