I'm not able to understand the use and role of the node_access tag. I used the Node View permission module to restrict the access to the article content type for anonymous users.

Now I have created an auto-complete search box that searches node titles. This is the query I used.

$query = \Drupal::entityquery('node');
$query->condition('title', trim($search));
$query->condition('status', 1);
$nids = $query->execute();
$nodes = Node::loadMultiple($nids);

I don't want this search box to search titles that belong to the article content type, and I used the node_access tag.

Will this tag serves my purpose here or is there any other approach to achieve the same?

  • Node access checks if a certain user is allowed to see a node. It not for filtering entity types (unles the user should not see those, but you did not say that). Use a condition for the type instead. – Neograph734 Mar 25 '17 at 7:13

No, it will not serve for the purpose you have in mind. It checks the currently logged-in user has view access to the node, which means that if the currently logged-in user is the user #1, it will see every node, including the ones that aren't published, and independently from the content type.

As @Neograph734 says, you should not use that tag to filter out nodes basing on their content type. If you want to filter the result by content type, you should use a line like $query->condition('type', 'article'); to built the query.

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.