1

I'm currently facing an issue:

By following the contrib module's(menu_link_attributes) functionality, I altered the menu item's form to have a visibility by path option(almost the same as in the block config form).

I am saving the input data from the form in the menu_link_content's options column, then unsetting the menu item in hook_preprocess_HOOK() if the conditions are met.

The thing is, we have cached pages which are basically nodes(only on dev environment, the local one works as expected because caching is disabled) and the menu item is unset only after a CC.

I'm trying to add a cache tag to the node so that it's invalidated after I'm done with menu items.

/**
 * Implements hook_preprocess_HOOK().
 */
function prl_menu_item_visibility_preprocess_node(&$variables) {
  // Since all the pages are nodes and all of them are using footer menu, we add the menu_link_content cache tag and invalidate it
  // when a link is saved and supposed to be hidden on specific pages.
  $variables['#cache']['tags'][] = 'menu_link_content';
}

Unfortunately, no success.

1 Answer 1

2

The tag you add doesn't exists. You probably want to add the list tag from the menu link content, which is invalidated in all cases when a link item is added, updated or deleted:

$variables['#cache']['tags'][] = 'menu_link_content_list';

But there might be another problem. As I understand your idea is, if you have a cache tag somewhere on the page, the complete page gets invalidated. This is true, but it will not help you. The rebuilt page will contain the cached menu block, if the block has not the correct cache tag on its own. So if you add a cache tag from the menu link to the node you have to do the same vice versa, add a node tag to the menu.

And probably you also need a cache context in the menu block for the visibility.

2
  • thanks mate, your answer led me to a solution, I ended up validating the pages and negate option using the conditional plugin and adding the 'url.path' context in hook_preprocess_menu solved my issue. It's a dull approach, but it's okay for now. Commented Feb 2, 2017 at 20:26
  • yes, this is not so nice for caching. If you get performance problems you can try a solution in javascript, see how core handle active links in menus.
    – 4uk4
    Commented Feb 2, 2017 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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