Skip to content

Commit 147bad1

Browse files
committed
FAXE-332 update menu boxe to support taxonmy children
1 parent 2a446ec commit 147bad1

File tree

1 file changed

+77
-5
lines changed

1 file changed

+77
-5
lines changed

web/themes/custom/fds_faxe_theme/fds_faxe_theme.theme

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,62 @@ function node2box(&$node, $showImages, $incPromoted) {
304304
return $refNode;
305305
}
306306

307+
function tax2box(&$taxonomy, $showImages, $showRelated) {
307308

309+
$aliasManager = \Drupal::service('path_alias.manager');
310+
311+
$refNode = new stdClass();
312+
$refNode->title = (string) $taxonomy->name->value;
313+
$refNode->url = $aliasManager->getAliasByPath('/taxonomy/term/' . $taxonomy->id());
314+
$refNode->image = null;
315+
$refNode->children = array();
316+
317+
if ($showImages) {
318+
if ($taxonomy->hasField('field_primaryicon')
319+
&& $taxonomy->field_primaryicon instanceof EntityReferenceFieldItemListInterface
320+
&& $taxonomy->field_primaryicon->referencedEntities()
321+
) {
322+
$refNode->image = $taxonomy->field_primaryicon->view([
323+
'label' => 'hidden',
324+
'type' => 'image',
325+
'settings' => [
326+
'image_style' => 'thumbnail'
327+
],
328+
]);
329+
}
330+
elseif ($taxonomy->hasField('field_primaryimage')
331+
&& $taxonomy->field_primaryimage instanceof EntityReferenceFieldItemListInterface
332+
&& $taxonomy->field_primaryimage->referencedEntities()
333+
) {
334+
$refNode->image = $taxonomy->field_primaryimage->view([
335+
'label' => 'hidden',
336+
'type' => 'image',
337+
'settings' => [
338+
'image_style' => 'thumbnail'
339+
],
340+
]);
341+
}
342+
}
343+
344+
if ($showRelated && $taxonomy->hasField('field_os2web_base_field_related')) {
345+
$relatedes = $taxonomy->get('field_os2web_base_field_related')->referencedEntities();
346+
if (count($relatedes) > 0) {
347+
foreach ( $relatedes AS $related ) {
348+
if ( $related->getType() == 'os2web_page') {
349+
$relatedNode = \Drupal\node\Entity\Node::load($related->id());
350+
$refNode->children[] = (object) array(
351+
'title' => $relatedNode->getTitle(),
352+
'url' => $relatedNode->toUrl()->toString()
353+
);
354+
}
355+
}
356+
}
357+
}
358+
359+
360+
return $refNode;
361+
362+
}
308363
function fds_faxe_theme_preprocess_paragraph(&$variables) {
309364

310365
$paragraph = $variables['paragraph'];
@@ -314,6 +369,7 @@ function fds_faxe_theme_preprocess_paragraph(&$variables) {
314369
$showImages = ($paragraph->field_os2web_menu_box_vm->value == 'image');
315370
$incPromoted = ($paragraph->field_os2web_menu_links_inc_pr_p->value == 1);
316371
$onlyTaxonomy = ($paragraph->field_only_taxonomy->value == 1);
372+
$showTaxonomyRelated = ($paragraph->field_show_taxonomy_related->value == 1);
317373

318374
$showReferences = null;
319375
if ($paragraph->hasField('field_show_references')) {
@@ -323,19 +379,35 @@ function fds_faxe_theme_preprocess_paragraph(&$variables) {
323379
$variables['menubox'] = [];
324380
$node = \Drupal::routeMatch()->getParameter('node');
325381

382+
// if current page is a node
326383
if ($node instanceof \Drupal\node\NodeInterface) {
327384

385+
if ($node->hasField('field_os2web_page_section')) {
386+
$sections = $node->get('field_os2web_page_section')->referencedEntities();
387+
if (count($sections) > 0) {
388+
// there is only one but can be change in future ..
389+
foreach ($sections as $section) {
390+
$children = \Drupal::entityTypeManager()
391+
->getStorage('taxonomy_term')
392+
->loadTree('os2web_sektion', $section->tid->value, 1, true);
393+
394+
foreach( $children AS $child ) {
395+
$variables['menubox'][] = tax2box($child, $showImages, $showTaxonomyRelated);
396+
}
397+
}
398+
}
399+
}
400+
401+
// Get all in section list
328402
$aliasManager = \Drupal::service('path_alias.manager');
329403
foreach ($paragraph->field_sektion->referencedEntities() as $taxonomy) {
330-
331404
$refNode = new stdClass();
332405
$refNode->title = (string) $taxonomy->name->value;
333406
$refNode->url = $aliasManager->getAliasByPath('/taxonomy/term/' . $taxonomy->id());
334407
$refNode->image = null;
335408
$refNode->children = array();
336409

337410
if ($showImages) {
338-
339411
if ($taxonomy->hasField('field_primaryicon')
340412
&& $taxonomy->field_primaryicon instanceof EntityReferenceFieldItemListInterface
341413
&& $taxonomy->field_primaryicon->referencedEntities()
@@ -378,10 +450,11 @@ function fds_faxe_theme_preprocess_paragraph(&$variables) {
378450
}
379451

380452
if (!$onlyTaxonomy && $node->hasField('field_link_til_andet_indhold')) {
381-
382453
$refs = $node->get('field_link_til_andet_indhold');
383454
foreach ($refs->referencedEntities() as $idx => $ref) {
384-
$variables['menubox'][] = node2box($ref, $showImages, $incPromoted);;
455+
if ($ref->getType() == 'os2web_page') {
456+
$variables['menubox'][] = node2box($ref, $showImages, $incPromoted);;
457+
}
385458
}
386459
}
387460

@@ -405,7 +478,6 @@ function fds_faxe_theme_preprocess_paragraph(&$variables) {
405478
}
406479
}
407480
}
408-
409481
}
410482
}
411483

0 commit comments

Comments
 (0)