Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hierarchical_term_formatter
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
hierarchical_term_formatter
Commits
6694c445
Commit
6694c445
authored
1 year ago
by
Viktor Holovachek
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3039982
- Add common parent
parent
c22c696f
No related branches found
No related tags found
1 merge request
!13
Issue #3039982 - Add common parent
Pipeline
#100510
passed
1 year ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
hierarchical_term_formatter.theme.inc
+23
-7
23 additions, 7 deletions
hierarchical_term_formatter.theme.inc
src/Plugin/Field/FieldFormatter/HierarchicalFormatter.php
+34
-0
34 additions, 0 deletions
src/Plugin/Field/FieldFormatter/HierarchicalFormatter.php
with
57 additions
and
7 deletions
hierarchical_term_formatter.theme.inc
+
23
−
7
View file @
6694c445
...
...
@@ -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
();
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/Plugin/Field/FieldFormatter/HierarchicalFormatter.php
+
34
−
0
View file @
6694c445
...
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment