How to check if a user is logged in inside a view 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(&amp;$variables){
    $variables['logged_in'] = (\Drupal::currentUser()-&gt;isAnonymous())?false:true;
}


But {{ dump(logged_in) }} still came up null.
I managed to accomplish this task by creating my own custom view field plugin

in mymodule.views.inc I used

function mymodule_views_data() {

  $data['views']['table']['group'] = t('Current User');
  $data['views']['table']['join'] = [
    // #global is a special flag which allows a table to appear all the time.
    '#global' =&gt; [],
  ];

  $data['views']['logged_in'] = [
    'title' =&gt; t('Logged In'),
    'help' =&gt; t('Check if user is logged in'),
    'field' =&gt; [
      'id' =&gt; 'logged_in',
    ],
  ];

  return $data;
}


and then in /mymodule/src/Plugins/views/field/LoggedIn.php I had

&lt;?php

namespace Drupal\mymodule\Plugin\views\field;

use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;

/**
 * A handler to provide a field that is completely custom by the administrator.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("logged_in")
 */
class LoggedIn extends FieldPluginBase {

    /**
     * {@inheritdoc}
     */
    public function init(ViewExecutable $view, DisplayPluginBase $display, array &amp;$options = NULL) {
        parent::init($view, $display, $options);
        $this-&gt;field_alias = 'logged_in';
    }

    /**
     * {@inheritdoc}
     */
    public function usesGroupBy() {
        return FALSE;
    }

    /**
     * {@inheritdoc}
     */
    public function query() {
        // Do nothing -- to override the parent query.
    }

    /**
     * {@inheritdoc}
     */
    protected function defineOptions() {
        $options = parent::defineOptions();

        return $options;
    }

    /**
     * {@inheritdoc}
     */
    public function render(ResultRow $values) {

        $logged_in = \Drupal\mymodule\Modules\CurrentUser::isLoggedIn();
        return $logged_in;
    }

}


Some of the contents of LoggedIn.php might not be necessary (not 100% sure) I was using another module as an example.