function faq_ask_form_node_faq_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 42 - This module is an add-on to FAQ module, allows users to 'ask question'.
Code
function faq_ask_form_node_faq_form_alter(&$form, FormStateInterface $form_state) {
$user = \Drupal::currentUser();
// Issue #1280446 by deck7uk.
// If this form is reached, user can ask question but should not answer.
if ($user
->hasPermission('ask question') && !$user
->hasPermission('answer question')) {
// Make sure the ask query is set.
$_GET['ask'] = 1;
}
if (!isset($_GET['ask']) || $_GET['ask'] != 1 && $_GET['ask'] != 'TRUE') {
// Do not modify form if ask query is not set.
return;
}
$language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if (!$user
->hasPermission('view own unpublished content') || $user
->id() == 0) {
$form_state
->setRedirect('faq-page');
}
$form['#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;
$faq_ask_settings = \Drupal::config('faq_ask.settings');
// Add default text to body field.
$form['body']['#default_value'] = $faq_ask_settings
->get('unanswered');
// 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 ($faq_ask_settings
->get('categorize')) {
// Hide all taxonomy fields.
$fields = \Drupal::entityManager()
->getFieldDefinitions('node', 'faq');
foreach ($fields as $name => $properties) {
if (!empty($properties
->getTargetBundle())) {
$fieldSettings = $properties
->getSettings();
if (array_key_exists('handler', $fieldSettings)) {
if ($fieldSettings['handler'] == 'default:taxonomy_term' && isset($form[$name])) {
// Hide form if it is a taxonomy field.
hide($form[$name]);
// If hidden, then do not expect it to be required.
$form[$name][$language]['#required'] = FALSE;
}
}
}
}
}
// If we're supposed to notify asker on answer, add form item for this.
if ($faq_ask_settings
->get('notify_asker')) {
// If asker is anonymous, add an optional e-mail field.
if ($user
->id() == 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['notify_mail'] = 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['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';
// Make sure it is not set to 60 as default.
$form['title']['#size'] = '';
// Shorter description on detailed question field.
$form['detailed_question']['#description'] = t('Longer question text.');
// Make sure it is not set to 60 as default.
$form['detailed_question']['#size'] = '';
// Make sure the category field does not expand too wide.
$fields = \Drupal::entityManager()
->getFieldDefinitions('node', 'faq');
foreach ($fields as $name => $properties) {
if ($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'])) {
// Make sure it is not set to 60 as default.
$form['faq_email']['#size'] = '';
}
}
}
}