You are here

function node_authlink_create in Node authorize link 8

Generate and save auth key for the new node.

3 calls to node_authlink_create()
NodeAuthlinkNodeForm::createAuthlink in src/Form/NodeAuthlinkNodeForm.php
Create authlink submit callback.
node_authlink_batch_generate in ./node_authlink.module
Generate authkeys for all nodes in node type.
node_authlink_cron in ./node_authlink.module
Implementation of hook_cron().

File

./node_authlink.module, line 349
Node Authlink hooks and alters.

Code

function node_authlink_create($node) {

  // Allow key generate without load node object
  if (is_numeric($node)) {
    $nid = $node;
  }
  else {
    $nid = $node
      ->id();
    $config = \Drupal::config('node_authlink.settings');

    // Ignore if node type is disabled
    if (!$config
      ->get('enable.' . $node
      ->bundle())) {
      return;
    }
  }

  // Generate key if not yet
  $authkey = isset($node->authkey) ? $node->authkey : hash('sha256', random_bytes(64));

  // Save to DB
  \Drupal::database()
    ->insert('node_authlink_nodes')
    ->fields([
    'nid' => $nid,
    'authkey' => $authkey,
    'created' => time(),
  ])
    ->execute();
}