You are here

function comment_notify_install in Comment Notify 8

Same name and namespace in other branches
  1. 5.2 comment_notify.install \comment_notify_install()
  2. 5 comment_notify.install \comment_notify_install()
  3. 6 comment_notify.install \comment_notify_install()
  4. 7 comment_notify.install \comment_notify_install()

Implements hook_install().

File

./comment_notify.install, line 14
Comment_notify.install.

Code

function comment_notify_install() {
  $database = \Drupal::database();

  // Create entries for existing comments.
  $comments_select = $database
    ->select('comment_field_data', 'c');
  $comments_select
    ->join('users_field_data', 'u', 'c.uid = u.uid');
  $comments_select
    ->addField('c', 'cid');
  $comments_select
    ->addExpression('0', 'notify');

  // Mix in a random string to all values.
  $salt = uniqid(mt_rand(), TRUE);
  if ($database
    ->driver() == 'pgsql') {
    $comments_select
      ->addExpression("MD5(:salt || c.mail || COALESCE(u.mail, u.init) || c.uid || c.name || c.entity_id || c.hostname || c.cid)", 'notify_hash', [
      ':salt' => $salt,
    ]);
  }
  else {
    $comments_select
      ->addExpression("MD5(CONCAT_WS('', :salt, c.mail, COALESCE(u.mail, u.init), c.uid, c.name, c.entity_id, c.hostname, c.cid))", 'notify_hash', [
      ':salt' => $salt,
    ]);
  }
  $database
    ->insert('comment_notify')
    ->from($comments_select)
    ->execute();

  // Set module weight low so that other modules act on the comment first.
  module_set_weight('comment_notify', 10);
}