2
  • I am using Display Suite to display Teasers of a custom content type Project in a View.
  • I want to show link on each Teaser to edit that node if the logged in user is the author of the node.
  • Our requirements do not allow us to use Contextual Links with the little hover gear.

Things I have tried:

  • I was also able to make a Display Suite field for [node:author:uid] and display that field in the Teaser.
  • I was able to create a Display Suite field to hold the link and display it in the Teaser.
  • I was able to create a custom field template for that Project Edit Button field I just made and display it in the Teaser. field--field-project-edit-button.html.twig.
  • This is where I hoped to wrap the content of that field (the link) in a conditional statement.

However

  • In the past I have been able to access fields in a different field's template using this technique. {{ element['#object'].field_name.value }}
  • I was able to do this with a Display Suite field for [node:nid] and then do {{ element['#object'].nid.value }} (where nid is the machine name of my [node:nid] Display Suite field) in the Project Button field template above.
  • It also works to use it in a conditional statement like {% if element['#object'].nid.value == 6 %}.
  • But this same technique is not working with {{ element['#object'].aid.value }} (where aid is the machine name of my Display Suite 'Author ID' field). It just comes back empty.

So

What is different about my [node:author:uid] and [node:nid] fields?

Why am I seeing the actual Author ID field properly but not {{ element['#object'].aid.value }} in the edit link's field template?

How do I make this silly button? I am completely open to other ways of doing this as long as it meets my requirements at the top of this post.

FULL SOLUTION, thanks to No Sssweat below

Display Suite token field:

<a href="[node:edit-url]" class="btn btn-sm btn-outline-info"><span class="fa fa-pencil"></span> Edit <span class="sr-only">[node:title]</span></a>

In the .theme file under mytheme_preprocess_field

/* Make the current user id available in field templates */
$vars['uid'] = \Drupal::currentUser()->id();

In that custom field template:

{% if element['#object'].getOwner.uid.value == uid %}
  {% for item in items %}
    {{ item.content }}
  {% endfor %}
{% endif %}
  • where aid is the machine name of my Display Suite 'Author ID' field. Did you manually create this Author ID field? perhaps the machine name is actually field_aid. – No Sssweat Mar 29 at 7:01
0

Assuming in your use case an Author is a user on your site, I believe you will be able to access to Author's ID using

{{ element['#object'].getOwner.uid.value }}

No need to create a custom field to hold the author's value.

  • Thank you! Going to put my snippets in the main question. – Sarah C Mar 29 at 16:54
0

First, this should be the simplest way for getting the the author's id in a twig template.

{{ node.getOwnerId }}

Second, I would use the path() function and the entity.user.canonical route with the user id to create the link to the user's page. This won't break if you choose to alias the user pages.

<a href="{{ path('entity.user.canonical', {user: node.getOwnerId}) }}">
   {{ author_pic }}
</a>

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.