I can easily check if a user is logged in in most templates by simply using a twig variable, or making a custom one myself. My issue is that I would like to be able to check if a user is logged in inside a custom text field in a view.
I used
{% if logged_in %}
Test
{% endif %}
But it didn't seem to work. Also
{{ dump(logged_in) }}
came up null. Also I looked in the available options for context filters and relationships but nothing seemed to have anything to do with the current user. Only authors of the content. Is there a way to know if the user is logged in from a view?
I suspect that it might be possible with some preprocess hook for views but if that can be avoided by using an option in views that would be preferable.
For clarification this is a view that displays a list of articles. Each with an image, title, and a snippet of body text. The article node has a field deciding whether a user login is required. If the user login is required then the body text is replaced for anyone that isn't logged in.
UPDATE: I attempted adding the variable in a preprocess hook but that didn't seem to do anything either.
function mymodule_preprocess_views_view_field(&$variables){
$variables['logged_in'] = (\Drupal::currentUser()->isAnonymous())?false:true;
}
But {{ dump(logged_in) }} still came up null.