You are here

function node_authlink_batch_generate in Node authorize link 8

Same name and namespace in other branches
  1. 7 node_authlink.module \node_authlink_batch_generate()

Generate authkeys for all nodes in node type.

1 string reference to 'node_authlink_batch_generate'
node_authlink_form_node_type_form_alter in ./node_authlink.module
Alter of node_type_form.

File

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

Code

function node_authlink_batch_generate(&$form, FormStateInterface &$form_state) {

  // Load NIDs that are not in the authkeys table
  $query = \Drupal::database()
    ->select('node', 'n');
  $query
    ->leftJoin('node_authlink_nodes', 'a', 'n.nid = a.nid');
  $query
    ->fields('n', [
    'nid',
  ])
    ->condition('type', $form_state
    ->getValue('type'))
    ->isNull('authkey');
  $nids = $query
    ->execute()
    ->fetchCol();

  // Create keys
  foreach ($nids as $nid) {
    node_authlink_create($nid);
  }
  \Drupal::messenger()
    ->addMessage(t('%num authkeys has been generated.', [
    '%num' => count($nids),
  ]));
}