You are here

function faq_ask_node_update in FAQ_Ask 8

Same name and namespace in other branches
  1. 7 faq_ask.module \faq_ask_node_update()

Implements hook_node_update().

Checks if the node being updated is a question that has been answered.

File

./faq_ask.module, line 185
This module is an add-on to FAQ module, allows users to 'ask question'.

Code

function faq_ask_node_update($node) {
  if ($node
    ->getType() == 'faq') {
    $nid = $node
      ->id();
    $faq_ask_settings = \Drupal::config('faq_ask.settings');

    // Update faq_ask_term_index on removing nid/tid pairs on node published.
    if ($node
      ->get('status')->value == 1 || !empty($node->body->value)) {
      $delete_query = \Drupal::database()
        ->delete('faq_ask_term_index');
      $delete_query
        ->condition('nid', $nid)
        ->execute();
    }

    // Return if the asker notification should be done by cron.
    if ($faq_ask_settings
      ->get('notify_by_cron')) {
      return;
    }
    $node_title = $node
      ->get('title')->value;

    // Check if the node is published and asker notified.
    $email = Utility::faqAskGetFaqNotificationEmail($nid);
    if ($node
      ->get('status')->value == 1 && $email != '') {

      // Get the asker account.
      $account = user_load_by_mail($email);
      $params['question'] = $node_title;
      $params['nid'] = $nid;

      // Send the e-mail to the asker. Drupal calls hook_mail() via this.
      $mail_sent = \Drupal::service('plugin.manager.mail')
        ->mail('faq_ask', 'notify_asker', $email, $account
        ->getPreferredLangcode(), $params, NULL, TRUE);

      // Handle sending result.
      if ($mail_sent) {
        \Drupal::logger('Faq_Ask')
          ->notice("Asker notification email sent to @to for question @quest", array(
          '@to' => $email,
          '@quest' => SafeMarkup::checkPlain($node_title),
        ));

        // If email sent, remove the notification from the queue.
        Utility::faqAskDeleteFaqNotification($nid);
      }
      else {
        \Drupal::logger('Faq_Ask')
          ->error('Asker notification email to @to failed for the "@quest" question.', array(
          '@to' => $email,
          '@quest' => SafeMarkup::checkPlain($node_title),
        ));
        drupal_set_message(t('Asker notification email to @to failed for the "@quest" question.', array(
          '@to' => $email,
          '@quest' => SafeMarkup::checkPlain($node_title),
        )));
      }
    }
  }
}