Skip to content

Instantly share code, notes, and snippets.

@shaal
Created March 2, 2018 18:15
Show Gist options
  • Select an option

  • Save shaal/3ead3804ad5e9af3311419d90eaaf538 to your computer and use it in GitHub Desktop.

Select an option

Save shaal/3ead3804ad5e9af3311419d90eaaf538 to your computer and use it in GitHub Desktop.
Drupal8 preprocess menu - Hide "About" Menu Item (from Main Menu), if current node title is "Who are we?"
/**
* Implements hook_preprocess_menu() for menu.html.twig.
*/
use Drupal\node\Entity\Node;
function sassrosenberg_preprocess_menu(&$variables, $hook)
{
if ($hook == 'menu__main') {
function _access_a_nodes_field($fieldName)
{
if (\Drupal::routeMatch()->getParameter('node')) {
$nid = \Drupal::routeMatch()->getParameter('node');
$node = \Drupal\node\Entity\Node::load($nid->id());
return $node->get($fieldName)->getValue();
}
return NULL;
}
$myNodeTitle = _access_a_nodes_field('title');
$myNodeTitleValue =$myNodeTitle[0]['value'];
$myField = _access_a_nodes_field('field_spag_body');
$myFieldValue =$myField[0]['value'];
dpm($myNodeTitleValue);
dpm($myFieldValue);
foreach ($variables['items'] as $key => $item) {
if ($myNodeTitleValue == 'Who Are We?') {
if ($item['title'] == 'About') {
unset($variables['items'][$key]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment