You are here

function faq_ask_submit in FAQ_Ask 8

Same name and namespace in other branches
  1. 6.2 faq_ask.module \faq_ask_submit()
  2. 7 faq_ask.module \faq_ask_submit()

Handles the ask form submission.

2 string references to 'faq_ask_submit'
faq_ask_form_node_faq_edit_form_alter in ./faq_ask.module
Implements hook_form_FORM_ID_alter().
faq_ask_form_node_faq_form_alter in ./faq_ask.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function faq_ask_submit($form, FormStateInterface $form_state) {
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('ask question') && !$user
    ->hasPermission('answer question')) {
    $node_id = $form_state
      ->getValue('nid');
    $node = node_load($node_id);
    if (is_object($node)) {
      $node->status->value = 0;
      $node
        ->save();
    }
  }

  // Issue #1554912 by jlea9378: Access Denied for Anonymous.
  if (!$user
    ->hasPermission('view own unpublished content') || $user
    ->id() == 0) {

    // Redirect to faq-page if the user is not allowed to view content.
    $form_state
      ->setRedirect('faq.faq-page');
  }
  $faq_email = $form_state
    ->getValue('faq_email');
  $faq_notify = $form_state
    ->getValue('notify_mail');

  // Save this is the node to be created.
  $asker_email = isset($faq_email) ? $faq_email : FALSE;

  // Handle the notification of asker.
  if (isset($asker_email) && $asker_email) {

    // If this user is not registered as a user before - check if all asking
    // anonymous users should be added to the newsletter list.
    if (\Drupal::moduleHandler()
      ->moduleExists('simplenews') && ($tid = $faq_ask_settings
      ->get('notify_asker_simplenews_tid'))) {

      // If we have selected a newsletter to add.
      if (function_exists('simplenews_subscribe_user')) {
        simplenews_subscribe_user($asker_email, $tid, $faq_ask_settings
          ->get('notify_asker_simplenews_confirm'), 'FAQ-Ask');
      }
    }
  }
  elseif (isset($faq_notify) && $faq_notify) {
    $account = user_load($user
      ->id());
    $asker_email = $account
      ->get('mail')->value;
  }
  else {
    drupal_set_message(t('Your question has been submitted. It will appear in the FAQ listing as soon as it has been answered.'), 'status');
  }
  if ($asker_email) {
    Utility::faqAskSetFaqNotification($node
      ->get('nid')->value, $asker_email);
    drupal_set_message(t('Your question has been submitted. An e-mail will be sent to <i>@mail</i> when answered.', array(
      '@mail' => $asker_email,
    )), 'status');
  }

  // Handle the notification of asker.
}