1

I have a multilanguage site. Now i'm running in the issue that some pages don't have translations. So i would like to hide that from the navigation.

I already tried to manipulate it with that hook:

mytheme_preprocess_menu_local_task

But it seems that it does not have an affect. I also don't find any other hook which i could use for that.

Any suggestion?

2

Use hook_node_access() to deny access to the not translated nodes and this will automatically hide the corresponding menu items:

mymodule.module:

use Drupal\Core\Access\AccessResult;
use Drupal\node\NodeInterface;
use Drupal\Core\Session\AccountInterface;

  /**
   * Implements hook_node_access().
   */
  function mymodule_node_access(NodeInterface $node, $op, AccountInterface $account) {
    $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
    if (!$node->hasTranslation($langcode)) {
      $access = AccessResult::forbidden();
    }
    else {
      $access = AccessResult::neutral();
    }
    $access->addCacheableDependency($node);
    return $access;
  }

You can be more specific, hide not translated nodes of specific content types. Or for specific user roles, which you probably need to do if other users than the admin wants to edit the nodes. Or you have to be specific about the operation $op. And test this with different user roles, as admin you won't see a difference, because this account passes all access checks.

  • Thanks. But it seems that it does not have an effect. Do i need active or install a module for node access? Cause even when i make dump inside the function is it not executed. – mastercheef85 Oct 11 '17 at 11:44
  • Yes, you need a custom module and install it. – 4k4 Oct 11 '17 at 11:50
  • yes that i've done but it does not work. i ment another core module or something. but ok i will check why it does not work – mastercheef85 Oct 11 '17 at 11:52
  • great it works!! Thanks a lot!! sorry i overread your "as admin you won't see a difference" – mastercheef85 Oct 11 '17 at 15:10
  • now i have another thing. i redirected previously user to startpage of selected language if they switch language on an non translated node. now that doesn't work anymore cause it does not go into my mytheme_preprocess_node function and inside the new function i'm not able to do unless to get too many redirects. Do you have any hints? – mastercheef85 Oct 11 '17 at 16:00

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.