0

how should I do if I need to show to a link menu based on a condition ?

This is my YML for my menu link :

double_vue_klesia.backuserswitch:
  title: 'Retour user précédent'
  menu_name: navigation-principale
  description: 'Switch back to your user account.'
  route_name: double_vue_klesia.backuser.switch
  weight: 1

I want to show this link only :

if (\Drupal::service('userswitch')->isSwitchUser())

This is in PHP, but I need this to be on YML (unless there a way to intercept it in php and hide it ?).

If my isSwtichUser is true (from my service), I want to show the link. But I can find a way to do it.

EDIT :

I'm using this module : https://www.drupal.org/project/userswitch

I "just" need to display a link to go back to the original user (link accessible from my frontend)

4
  • 1
    You show/hide menu links by allowing/denying access to the target route. So the first (now deleted) answer of @QuanLee is correct.
    – 4uk4
    Sep 5, 2018 at 15:12
  • I tried was @QuanLee says, but it wasn't working :/ Sep 5, 2018 at 15:17
  • Can be another issue. You need to add cache data. Don't know the cache dependencies of the userswitch service, so until you know you can disable caching with AccessResult::forbidden()->setCacheMaxAge(0). But better find the correct caching data for the service, with cache max age = 0 you disable a lot of caching on your page, which is bad for performance. Btw. you need to add caching also in your latest edit for a normal link if it depends on a condition.
    – 4uk4
    Sep 5, 2018 at 15:47
  • My toolbar will still be not accessible, right, even with setCacheMaxAge(0) ? It there a way otherwise to create an "temporary toolbar" where there's only my link ? Sep 5, 2018 at 15:50

1 Answer 1

2

you can add custom access on route_name: double_vue_klesia.backuser.switch

double_vue_klesia.backuser.switch:
  path: '/path-of-you'
  defaults:
    _form:  '\Drupal\my_module\Form\DemoForm'
    _title: 'title'
  requirements:
    _custom_access: '\Drupal\my_module\Controller\ApplicationController::accessCustom'

Inside \Drupal\my_module\Controller\ApplicationController:

 /**
   * Check user has permission.
   *
   * @return \Drupal\Core\Access\AccessResult
   */
  public function accessCustom() {
    if (if (\Drupal::service('userswitch')->isSwitchUser())) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }
5
  • I believe your answer is for routing.yml, I need for links.menu.yml. If i'm wrong, can you explain ? Sep 5, 2018 at 14:26
  • ou can still go for the _custom_access, but you need a event subscriber to change the requirement of the existing route. Read this for a start: drupal.org/node/2187643
    – Quan Lee
    Sep 5, 2018 at 14:32
  • My link is still appearing on my front office every time. Sep 5, 2018 at 14:37
  • @Quan this is the right approach so I’ve undeleted it
    – Clive
    Sep 5, 2018 at 15:26
  • I see (but too late...) that I've already this method in my module, but for a toolbar and I don't have access to this when I switch my user Sep 5, 2018 at 15:34

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.