You are here

function faq_ask_form_alter in FAQ_Ask 6.2

Implementation of hook_form_alter(). This is how we build the "ask question" form.

File

./faq_ask.module, line 178
This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.

Code

function faq_ask_form_alter(&$form, $form_state, $form_id) {
  global $user;

  // If this is question form and user is allowed to ask a question but not allowed to answer question - this must be a question form
  if ($form_id == 'faq_node_form' && user_access('ask question', $user) && !user_access('answer question', $user)) {
    $_GET['ask'] = 1;
  }

  // If this is not the question form OR question flag is not set - return from here
  if ($form_id != 'faq_node_form' || !isset($_GET['ask'])) {
    return;
  }

  // If question flag is not set - return too
  if ($_GET['ask'] != 1 && $_GET['ask'] != 'TRUE' && $_GET['ask'] != 'true') {
    return;
  }

  // ELSE: Show question form!
  drupal_set_title(t('Ask a Question'));

  // Set the published field off and make sure they can't override it.
  $form['options']['status']['#default_value'] = FALSE;
  $form['options']['status']['#disabled'] = TRUE;

  // Get rid of the body elements (we'll dummy one later) and the menu elements.
  $form['body_field']['#type'] = 'hidden';
  $form['menu']['#type'] = 'hidden';

  // Check if only experts can categorize the question.
  if (variable_get('faq_ask_categorize', FALSE)) {
    unset($form['taxonomy']);
  }
  $form['body'] = array(
    '#type' => 'value',
    '#value' => variable_get('faq_ask_unanswered', t('Not answered yet.')),
  );

  //dpm($form);

  // if we're supposed to notify asker on answer, add form item for this
  if (variable_get('faq_ask_notify_asker', FALSE)) {
    global $user;
    $mail = '';
    if (isset($form['nid']['#value'])) {
      $mail = _faq_ask_get_faq_notification_email($form['nid']['#value']);
    }

    // If asker is anonymous, add an optional e-mail field that may be used for notification when question is answered
    if ($user->uid == 0) {

      // Form field for e-mail.
      $form['faq_email'] = array(
        '#type' => 'textfield',
        '#title' => t('Notification E-mail (optional)'),
        // Issue #1471114 by jlea9378: Email address isn't retained after clicking Preview
        '#default_value' => isset($form_state['values']['faq_email']) ? $form_state['values']['faq_email'] : $mail,
        '#weight' => 0,
        '#description' => t('Write your e-mail here if you would like to be notified when the question is answered.'),
      );
    }
    else {

      // Checkbox for notification
      $form['faq_notify'] = array(
        '#type' => 'checkbox',
        '#title' => t('Notify by E-mail (optional)'),
        // Issue #1471114 by jlea9378: Email address isn't retained after clicking Preview
        '#default_value' => isset($form_state['values']['faq_notify']) ? $form_state['values']['faq_notify'] : $mail != '' ? TRUE : FALSE,
        '#weight' => 0,
        '#description' => t('Check this box if you would like to be notified when the question is answered.'),
      );
    }
  }

  // Issue #947402 by verta: PHP error on saving new question, warning: function_exists() expects parameter 1 to be string, includes\form.inc on line 769
  // Add validation of the e-mail field
  if (!isset($form['#validate'])) {
    $form['#validate'] = array();
  }
  $form['#validate'][] = 'faq_ask_form_validate';

  // Make sure we know we came from here.
  $form['faq_ask'] = array(
    '#type' => 'value',
    '#value' => TRUE,
  );
  $form['buttons']['submit']['#submit'][] = 'faq_ask_submit';

  // Sean Corales: Redirect to faq page if user is anonymous or cannot edit own faq nodes
  global $user;
  if (!user_access('edit own faq') && !user_access('edit faq') || $user->uid == 0) {
    $form['#redirect'] = 'faq';
  }

  // Issue #957414 by wvanbusk: Can shortcut select category?
  if (isset($_GET['term']) && ($t = $_GET['term'])) {
    if (is_numeric($t)) {
      $term = taxonomy_get_term($t);
      if (is_object($term)) {

        // Make sure we have loaded a proper object
        if (isset($form['taxonomy']['tags'])) {

          // Using tags
          $vid = key($form['taxonomy']['tags']);
          $form['taxonomy']['tags'][$vid]['#default_value'] = $term->name;
        }
        else {
          $vid = key($form['taxonomy']);
          $form['taxonomy'][$vid]['#default_value'] = $term->tid;
        }
      }
    }
  }

  // Handle special cases if this is a block form
  if (isset($_GET['block'])) {
    if ($_GET['block']) {
      if (!variable_get('faq_ask_categorize', FALSE)) {

        // Issue #1127744 by j.r: Categories don't show up in Ask a Question Block
        if (isset($form['taxonomy']['tags'])) {
          $tags = array_keys($form['taxonomy']['tags']);
          $form['taxonomy']['tags'][$tags[0]]['#description'] = t('A comma-separated list of terms.');
        }
      }
      else {
        unset($form['taxonomy']);
      }

      // Shorter description on Qestion field + move it higher
      $form['title']['#description'] = t('Question to be answered.');
      $form['title']['#weight'] = '-5';

      // Shorter description on detailed question field
      $form['detailed_question']['#description'] = t('Longer question text.');
    }
  }
}