0

I'm using Drupal 8 with the nodeaccess module.

I'm able to set the node grants with the form.

I also have a custom module which imports new nodes. These new nodes should be shown only to certain users.

I know I can insert the corresponding lines into the database tables nodeaccess and node_access. But this might be not consistent.

Is there a way to set the nodeaccess programmatically?

  • 2
    Does this answer your question? How can I restrict user access to view nodes based on field value? – leymannx Apr 20 at 19:03
  • Nodes created through the API should have any system grants applied automatically. How are you importing nodfes into the system? – Jaypan Apr 20 at 19:30
  • I'm importing an Excel file and create a node. After creating the node I use a function to build the database rows in node_access and nodeaccess. – schulle877 Apr 23 at 17:23
0

I use a function to build the database rows in node_access and nodeaccess:

  private function set_node_access($nid, $id, $realm='nodeaccess_uid', $view=1, $update=0, $delete=0)
  {
    // ksm("nid: $nid uid: $uid");
    $connection = \Drupal::database();
    $sql = "INSERT INTO nodeaccess (nid, gid, realm, grant_view, grant_update, grant_delete) 
            VALUES($nid,$id,'$realm',$view,$update,$delete) 
            ON DUPLICATE KEY UPDATE grant_view=VALUES(grant_view), grant_update=VALUES(grant_update), grant_delete=VALUES(grant_delete)";
    $connection->query($sql);
    $sql = "INSERT INTO node_access(nid, langcode, fallback, gid, realm, grant_view, grant_update, grant_delete) 
            VALUES($nid,'de', 1, $id,'$realm',$view,$update,$delete) 
            ON DUPLICATE KEY UPDATE grant_view=VALUES(grant_view), grant_update=VALUES(grant_update), grant_delete=VALUES(grant_delete);";
    $connection->query($sql);
  }

That's not the nice way, but it works for me.

| improve this answer | |

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.