You are here

function faq_ask_form_faq_node_form_alter in FAQ_Ask 7

Implements hook_form_FORM_ID_alter().

This is how we build the "ask question" form. @TODO: Make sure this is called after the taxonomy is added, so that we may delete or modify the taxonomy part of the form if we want to.

Parameters

array $form: The edit form to modify.

array $form_state: Form state information

Return value

array $form Modified form as called by reference

File

./faq_ask.module, line 234
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_faq_node_form_alter(&$form, &$form_state) {
  global $user;

  // Issue #1280446 by deck7uk
  // If this form is reached with a user that can ask question but should not answer
  if (user_access('ask question') && !user_access('answer question')) {
    $_GET['ask'] = 1;

    // make sure the ask query is set
  }
  if (!isset($_GET['ask']) || $_GET['ask'] != 1 && $_GET['ask'] != 'TRUE') {
    return;

    // Do not modify form if ask query is not set
  }
  $language = $form['body']['#language'];
  if (!user_access('view own unpublished content') || $user->uid == 0) {
    $form['#redirect'] = 'faq-page';
  }
  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;

  // Add default text to body field.
  $form['body']['#default_value'] = variable_get('faq_ask_unanswered', t('Not answered yet.'));

  // Hide the body elements (we'll dummy one later) and the menu elements.
  hide($form['body']);
  hide($form['menu']);
  hide($form['options']);
  hide($form['upload']);
  $form['additional_settings']['#access'] = FALSE;
  $form['upload']['#access'] = FALSE;

  // Check if only experts can categorize the question.
  if (variable_get('faq_ask_categorize', FALSE)) {

    // Hide all taxonomy fields
    $fields = field_info_instances('node', 'faq');
    foreach ($fields as $name => $properties) {
      if ($properties['display']['default']['module'] == 'taxonomy' && isset($form[$name])) {
        hide($form[$name]);

        // Hide form if it is a taxonomy field
        $form[$name][$language]['#required'] = FALSE;

        // If hidden, then do not expect it to be required
      }
    }
  }

  // if we're supposed to notify asker on answer, add form item for this
  if (variable_get('faq_ask_notify_asker', FALSE)) {

    // 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)'),
        '#default_value' => '',
        '#weight' => 10,
        '#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)'),
        '#default_value' => FALSE,
        '#weight' => 10,
        '#description' => t('Check this box if you would like to be notified when the question is answered.'),
      );
    }
  }

  // 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['#submit'][] = 'faq_ask_submit';
  $form['actions']['submit']['#submit'][] = 'faq_ask_submit';

  // Handle special cases if this is a block form
  if (isset($_GET['block'])) {
    if ($_GET['block']) {

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

      // Make sure it is not set to 60 as default
      // Shorter description on detailed question field
      $form['detailed_question']['#description'] = t('Longer question text.');
      $form['detailed_question']['#size'] = '';

      // Make sure it is not set to 60 as default
      // Make sure the category field does not expand too wide
      $fields = field_info_instances('node', 'faq');
      foreach ($fields as $name => $properties) {
        if (isset($properties['display']['default']['module']) && $properties['display']['default']['module'] != 'taxonomy' && isset($form[$name]) && $properties['field_name'] == 'field_tags') {
          $form[$name][$form[$name]['#language']]['#cols'] = '';
          $form[$name][$form[$name]['#language']]['#size'] = '';
        }
      }

      // Email field
      if (isset($form['faq_email'])) {
        $form['faq_email']['#size'] = '';

        // Make sure it is not set to 60 as default
      }
    }
  }
}