Last active
March 10, 2021 00:18
-
-
Save zaidaldabbagh/bf5baad56f841927ab3189e396de9d18 to your computer and use it in GitHub Desktop.
Drupal 8 - Menu preprocess hook to fetch linked nodes and access thumbnail field values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Drupal\node\Entity\Node; | |
function ncea_preprocess_menu(&$vars, $hook) { | |
// Subjects Nav | |
if ($hook == 'menu__subjects') { | |
foreach ($vars['items'] as $key => $item) { | |
if ($vars['items'][$key]['below']) { | |
foreach ($vars['items'][$key]['below'] as $learningAreaKey => $learningAreaItem) { | |
$learningAreaUrl = $learningAreaItem['url']; | |
// Add learning area thumb url | |
if ($learningAreaUrl instanceof Drupal\Core\Url) { | |
$nid = $learningAreaUrl->getRouteParameters()['node']; | |
$learningAreaNode = Node::load($nid); | |
$learningAreaNodeThumbUrl = file_create_url($learningAreaNode->field_thumbnail->entity->getFileUri()); | |
$vars['items'][$key]['below'][$learningAreaKey]['thumbnail'] = $learningAreaNodeThumbUrl; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment