- 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 }}
(wherenid
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 }}
(whereaid
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 actuallyfield_aid
.