I have a custom content entity listed with Views.
I want to conditionally display some information depending on the fact that the row is related to the current user.
I tried {{ user:uid }} with no success.
Any idea?

With help from @4k4, I could use 'Custom text' to add on each row of my Views output two 'buttons' (Edit, Delete) depending on the current user:

{% if user_id_1=="[current-user:uid]"  %}
<a href="/sel/service/{{ id }}/edit"><img src="/sites/default/files/images/document-edit.gif" alt="" title="Modifier" width="11" height="13"></a>
<a href="/sel/service/{{ id }}/delete"><img src="/sites/default/files/images/trash.gif" alt="" title="Supprimer" width="11" height="13"></a>
{% endif %}

enter image description here As you can see, there is also a button in the header (Add).
I'd like to have this button displayed only if the user is not anonymous.
I tried (in either Text area or Unfiltered text):

{% if "0"=="[current-user:uid]"  %}
<a href="/sel/service/add" class="form-submit hiddenforuser[current-user:uid]">Ajouter</a>
{% endif %}

but it doesn't work:
enter image description here
Any idea why?


The twig variables in {{ }} are replacement patterns for fields. Globally available tokens are accessed by their usual square brackets syntax:


Custom text field

This token works in a global text area, but not a custom text field, because this plugin Drupal\views\Plugin\views\field\Custom doesn't have global token replacement. You need to extend it and override renderAltered():



namespace Drupal\mymodule\Plugin\views\field;

use Drupal\views\Plugin\views\field\Custom;

 * A handler to provide a field extending custom by global token replacement.
 * @ingroup views_field_handlers
 * @ViewsField("custom")
class CustomToken extends Custom {

   * {@inheritdoc}
protected function renderAltered($alter, $tokens) {
    return $this->viewsTokenReplace($this->globalTokenReplace($alter['text']), $tokens);

and tell the plugin manager about the new class:


function mymodule_views_plugins_field_alter(array &$plugins) {
  $plugins['custom']['class'] = 'Drupal\mymodule\Plugin\views\field\CustomToken';

This applies the function globalTokenReplace() before the twig patterns are replaced.

Then you can use

{% if anotherfield_id == '[current-user:uid]' %} YES {% else %} NO {% endif %}

You have to enclose the token in single quotes so that you get a valid twig syntax after the global tokens are replaced.

Global text area

A global text area in header/footer is exactly the opposite, it supports global tokens but not Twig.

You could do the same as before and add Twig support in a custom plugin. But there is an easier solution. Install the module Twig input filter and go to "Text formats and editors" in Configuration and enable the Twig filter for the text format you use in the text area.

  • I added a global custom text field in my view. When I put [current-user:uid] in the text of this field, I get "[current-user:uid]" and not the value (should be 1). What am I doing wrong? – gbmapo Apr 10 '18 at 9:36
  • I've just tested it in a global text area, it works there. Do you have the Token module installed? – 4k4 Apr 10 '18 at 10:07
  • I just installed Token module. I have now a nice (and quite impressive) list of tokens. But my global text area with [current-user:uid] in it still displays "[current-user:uid]". Is there anything else to do before using these tokens in Views? – gbmapo Apr 10 '18 at 10:35
  • It works when I use it in a global text area in the header of my View. Still doesn't when used in a global custom text field in each row. And I need that as I want to display/hide depending on something like {% if anotherfield_id == [current-user:uid] %} YES {% else %} NO {% endif %} – gbmapo Apr 10 '18 at 11:12
  • The custom field plugin has indeed no global token replacement, so you would need to extend it, see the edited answer. – 4k4 Apr 10 '18 at 12:22

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.