function faq_ask_node_insert in FAQ_Ask 8
Same name and namespace in other branches
- 7 faq_ask.module \faq_ask_node_insert()
Implements hook_node_insert().
Handles the creation of a question node after the node is created. This ensures that the node ID is available, needed for sending e-mail notifications.
File
- ./
faq_ask.module, line 232 - This module is an add-on to FAQ module, allows users to 'ask question'.
Code
function faq_ask_node_insert($node) {
$faq_ask_settings = \Drupal::config('faq_ask.settings');
$user = \Drupal::currentUser();
// Handle only faq node types.
if ($node
->getType() == 'faq') {
$terms = Utility::faqAskGetTerms($node);
// Update the faq_ask_term_index table if node is unpublished.
if (empty($node->body->value) && (!$user
->hasPermission('answer question') && $user
->hasPermission('ask question'))) {
\Drupal::database()
->delete('faq_ask_term_index')
->condition('nid', $node
->id())
->execute();
foreach ($terms as $tid => $term) {
// If term is available.
if ($tid) {
\Drupal::database()
->insert('faq_ask_term_index')
->fields(array(
'nid' => intval($node
->id()),
'tid' => $tid,
'sticky' => intval($node
->get('sticky')->value),
'created' => intval($node
->get('created')->value),
))
->execute();
}
}
}
// Are we notifying the expert(s).
if ($faq_ask_settings
->get('notify')) {
// Use only the first term entered in the correct vocabulary.
$term = taxonomy_term_load(array_shift(array_keys($terms)));
// Find out who the experts are.
$query = \Drupal::database()
->select('faq_expert', 'fe')
->fields('fe', [
'uid',
])
->condition('fe.tid', array_keys($terms), 'IN');
$experts = $query
->execute()
->fetchAllKeyed();
foreach ($experts as $expert => $expertData) {
$account = user_load($expert);
$params = array(
'category' => is_object($term) ? $term
->id() : -1,
'question' => $node
->get('title')->value,
'question_details' => $node
->get('field_detailed_question')->value,
'nid' => $node
->id(),
'creator' => $account
->get('name')->value,
'account' => $account,
);
$mail_sent = \Drupal::service('plugin.manager.mail')
->mail('faq_ask', 'notify_expert', $account
->get('mail')->value, $account
->getPreferredLangcode(), $params, NULL, TRUE);
if ($mail_sent) {
\Drupal::logger('Faq_Ask')
->notice('Expert notification email sent to @to', array(
'@to' => $account
->get('mail')->value,
));
}
else {
\Drupal::logger('Faq_Ask')
->error('Expert notification email to @to failed for the "@cat" category.', array(
'@to' => $account
->get('mail')->value,
'@cat' => SafeMarkup::checkPlain($term
->get('name')->value),
));
drupal_set_message(t('Expert notification email failed for the "@cat" category.', array(
'@cat' => SafeMarkup::checkPlain($term
->get('name')->value),
)));
}
}
}
}
}