You are here

public function ShortAnswerQuestion::getAnsweringForm in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerQuestion.php \Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerQuestion::getAnsweringForm()
  2. 6.x question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerQuestion.php \Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerQuestion::getAnsweringForm()

Get the form through which the user will answer the question.

Question types should populate the form with selected values from the current result if possible.

Parameters

FormStateInterface $form_state: Form state.

QuizResultAnswer $quizQuestionResultAnswer: The quiz result answer.

Return value

array Form array.

Overrides QuizQuestionEntityTrait::getAnsweringForm

File

question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerQuestion.php, line 63

Class

ShortAnswerQuestion
@QuizQuestion ( id = "short_answer", label = Plugin annotation @Translation("Short answer question"), handlers = { "response" = "\Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerResponse" } )

Namespace

Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion

Code

public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
  $element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
  $element += array(
    '#type' => 'textfield',
    '#title' => t('Answer'),
    '#description' => t('Enter your answer here'),
    '#default_value' => '',
    '#size' => 60,
    '#maxlength' => 256,
    '#required' => FALSE,
    '#attributes' => array(
      'autocomplete' => 'off',
    ),
  );
  if ($quizQuestionResultAnswer
    ->isAnswered()) {
    $element['#default_value'] = $quizQuestionResultAnswer
      ->getResponse();
  }
  return $element;
}