I am trying to restrict node access for anonymous users based on a custom field. I got it working for accessing the node directly like this:
$country = "GB";
if ($node->hasField('field_country_hide')) {
$country_code = $node->field_country_hide->value;
if ($country_code == $country) {
// this user should not be allowed to see this article
return AccessResult::forbidden();
}
} else {
return AccessResult::neutral();
}
But this doesn't restrict access for the node in a view listing. What is the best way to go about hiding a node in a view listing?