Filter view list items by user property and item property I have a view where I want to output a list of content node teasers. At the moment all content nodes are used, but I want to filter the output to only contain nodes that have a certain value in a taxonomy field property they have, check if the current user has a certain role that is similar to the property and only then show them in the output.

So far I've tried to programmatically filter the output in the hook mytheme_preprocess_views_view_unformatted of my theme with the following code

function mytheme_preprocess_views_view_unformatted(&amp;$variables) {
    $result = $variables['view']-&gt;result;
    $entity = $result-&gt;entity;
}


but that crashes the site and doesn't give me the results to filter.

Is there a way to filter the content nodes programmatically or view the view settings and if so how?
I think I understand what you're describing, and it is theoretically possible to handle in the hook as you've started, with something like this:

/**
 * Implements hook_preprocess_views_view_unformatted().
 */
function mymodule_preprocess_views_view_unformatted(&amp;$variables) {
  if (isset($variables['view'])) {
    $view = $variables['view'];
    // You'll want to check the ID &amp;&amp; possibly the current_display so as not to fire on every View!
    // In this example, my View is called "test", and I have a page display called "page_1".
    if ($view-&gt;id() == "test" &amp;&amp; $view-&gt;current_display == "page_1") {
      $result = $view-&gt;result;
      // You mentioned doing something regarding the user, so you could grab that here.
      $current_user = $variables['user']-&gt;account;
      // The $result will be an array of ResultRow entities.
      foreach ($result as $key =&gt; $resultRowEntity) {
        // Do some customized logic on each row.
        // You can access the entire entity here if needed like this:
        $row_entity = $resultRowEntity-&gt;_entity;
        // In my case this is a Node, so can do something to get a term field from it.
        // In this example, getting the "some_field" and the first delta of it as a string value.
        $some_field_on_the_node = $row_entity-&gt;get('field_some_field')-&gt;getValue();
        // You could do some sort of logic here to determine whether or not to unset this row.
        // In this example, I'll say if the field doesn't equal 2, OR the user ID is not equal to 1, unset the row.
        // This would remove it from the result.
        if ($some_field_on_the_node != "2" || $current_user-&gt;uid != 1) {
          // You'll want to unset it from 'rows' to not display it.
          unset($variables['rows'][$key]);
        }
      }
    }
  }
}


I think the takeaway is to use "isset" to avoid those nasty PHP errors, and I highly recommend using XDebug, or at the least the devel &amp;&amp; kint modules, so that in your code, you can put things like ksm($variables['view']) to get some insight into what variables you have available to use. PHP can be very finicky when you try and reference things that don't exist!

Even though this might technically work, it's worth noting that a better approach would be to implement a relationship in Views to bring in the taxonomy term entity being referenced on your node, and that would allow you to add View filters in the UI to massage out the data that way. You could also add a role that could be associated with viewing your particular content type, and this would automatically exclude it from Views results regarding the user if they didn't have this role. 

Of course, I may me misunderstanding the complexity of your use case. Another "better practice" approach would be to implement a custom Views filter, which could then be added via the Views UI and maintained separately in a custom module, and would possibly allow for a cleaner implementation - It's somewhat of a broad topic, but I think this is a great intro to the topic: https://www.webomelette.com/creating-custom-views-filter-drupal-8.

I hope this helps point you in the right direction!