0

I'm trying to check if the current node (of a specific content type) has been added to a menu (using the "Provide a menu link" in the node edit GUI).

if it has, I need to set a variable in .theme.inc, using THEME_preprocess_page.

I've been digging through drupal.org docs and i found those two questions find Menu Settings of node, given URL alias (or node ID) and Get menu item of a node seem relevant but not quite right.

1

To set if the node has menu link you can use menu_ui_get_menu_link_defaults function from menu_ui.module something like:

/**
 * Implements hook_preprocess_HOOK().
 */
function YOUMODULE_preprocess_page(&$vars) {
  /** @var \Drupal\node\Entity\Node $node */
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    // Get the default menu link.
    $defaults = menu_ui_get_menu_link_defaults($node);
    if ($defaults['id']) {
      // It has a menu
     // set your variable here.
    }
    else {
      // It has no menu link
      // It mean Provide a menu link not checked
    }
  }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.