You are here

function faq_ask_form_node_faq_edit_form_alter in FAQ_Ask 8

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.

File

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

Code

function faq_ask_form_node_faq_edit_form_alter(&$form, FormStateInterface $form_state) {
  $node = \Drupal::routeMatch()
    ->getParameter('node');
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('answer question')) {
    $form['body']['widget'][0]['#required'] = TRUE;
    $form['actions']['submit']['#submit'][] = 'faq_ask_edit_submit';
  }
  elseif ($user
    ->hasPermission('ask question') && !$user
    ->hasPermission('answer question')) {
    if (!$node->status->value) {

      // 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['actions']['submit']['#submit'][] = 'faq_ask_submit';
    }
    elseif ($node->status->value) {
      drupal_set_message(t('Content is published, So you can not edit.'), 'warning');
      $response = new RedirectResponse(URL::fromUserInput('/node/' . $node
        ->id())
        ->toString());
      $response
        ->send();
      exit;
    }
  }
}