0

I would like to display a custom block with some links for authenticated users like drupal.org does. drupal.org profile block

I would like to display username next to user's picture, and custom menu items. For example, "My contents" where url is something like this: "/user/{uid}/my-contents".

Do you have any idea how to create that?

Ps: I tried "menu_token" module but it didn't work for me correctly.

1 Answer 1

2

If you don't have any issue rendering a block of menu, you can just show up a menu there which I suppose you've got that covered. for the UID part there are couple of workarounds for this:

  1. Using Views
  2. Menu Token
  3. Me (Drupal 7) only
  4. Templating: Much easier approach

The Views way

Number 1, using views is a long story, I'll keep it short and I move on to number 2, my own approach. With views you can use contextual filters and build your own custom links using "Current logged in user" filter, you can randomly get a view from whatever and override it with the HTML structure of your need. in case you need a more in depth answer for this, let me know and I will update my answer

Menu Token

This module is exactly what we need, but we have to consider some bugs that it currently has, if you check the issue queue there are some major issues with it, but still, you can manage to get it working with a little bit of dirty hacking, using jQuery/JavaScript

The issue with this module is that you cannot use the token in middle of a string for instance: /user/{uid}/my-contents wouldn't work but simply /{uid} works just fine.

So in our case, we override the href attribute and hardcode the path we want to it as prefix and suffix, using the UID

This is not the best practice at all, but in case anyone wants a quick solution, this is what I suggest, in case you need help with the jQuery part, let me know and I will update the answer.

ME module - Drupal 7 only

for those who have this issue in Drupal 7, this module considers /me as the user ID if configured properly, so a link in question would become something like this: /user/me/my-contents

Templating

So let's say we edit our page.html.twig file so we can create our custom structure for our menu, then in our case, we can simply refer to the current user's UID like so:

<a href="{{ path('entity.user.canonical', {'user': user.id}) }}/my-contents">Test menu item</a>
2
  • Well, I don't know, I would prefer a "code way". Can we render links as list items?
    – asrob
    Commented Oct 31, 2018 at 10:42
  • 1
    I updated the answer and added Templating so you might want to check that out, you can simply create your list items in HTML
    – Sohail
    Commented Oct 31, 2018 at 11:07

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.