1

I have made a page that displays information about a user, much like a profile page.

Now I want users to be able to edit their information, so I added a button using twig tweak in user.html.twig:

{% if logged_in %}
<div class = "row"> 
    <a href ="{{ drupal_token('current-user:edit-url') }}" class = "edit-profile-btn col-lg-12">
      <div>Edit profile</div>
    </a> 
</div>
{% endif %}

enter image description here

This works fine, however the button is displayed on every profile page. I want to hide the button if a user is viewing another user's page.

Can this be done using an {{ if statement }} to check if the viewed page belongs to the logged in user?

1

To do this right you will need to use this combination of tests.

Is the user logged in {% if logged_in %} ?

Does the user have the permission needed to edit their profile with {% if user.hasPermission('depends on your site setup') %}?

Is the owner of the node the same as the current user? {% if node.ownerId == user.id %}

NOTE: The snippets above are not tested. You may have to tweak to get the correct info (e.g. getOwnerId SHOULD equal node.ownerId.. but twig may have a different opinion).

  • I tried any statement containing an = but it gives me errors. Could you maybe give some more explanation on how to use your answer? I tried something like {% if user.current-user %} but this didn't work also – Rubenxfd Oct 18 '17 at 7:46
  • Sorry, that was a typo on my part. Corrected the = to == in the answer, which is the correct syntax. – CG Monroe Oct 18 '17 at 13:38
  • Thanks, that did at least not give me an error. Can you help me tweak it further? As I am using the user node with a specific view (full), do I have to do anything different? I tried using {% if user--full.ownerId == current-user.uid %} but it still displays on every page. – Rubenxfd Oct 18 '17 at 14:46

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.