You are here

public function QuizQuestionEntityTrait::getNodeForm in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 src/Entity/QuizQuestionEntityTrait.php \Drupal\quiz\Entity\QuizQuestionEntityTrait::getNodeForm()

Returns a node form to quiz_question_form.

Adds default form elements, and fetches question type specific elements from their implementation of getCreationForm.

Parameters

array $form_state:

Return value

array An renderable FAPI array.

File

src/Entity/QuizQuestionEntityTrait.php, line 48

Class

QuizQuestionEntityTrait
A trait all Quiz question strongly typed entity bundles must use.

Namespace

Drupal\quiz\Entity

Code

public function getNodeForm(array &$form_state = NULL) {
  $user = \Drupal::currentUser();
  $form = array();

  // Mark this form to be processed by quiz_form_alter. quiz_form_alter will
  // among other things hide the revision fieldset if the user don't have
  // permission to control the revisioning manually.
  $form['#quiz_check_revision_access'] = TRUE;

  // Allow user to set title?
  if (user_access_test_user_access('edit question titles')) {
    $form['helper']['#theme'] = 'quiz_question_creation_form';
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#maxlength' => 255,
      '#default_value' => $this->node->title,
      '#required' => TRUE,
      '#description' => t('Add a title that will help distinguish this question from other questions. This will not be seen during the @quiz.', array(
        '@quiz' => _quiz_get_quiz_name(),
      )),
    );
  }
  else {
    $form['title'] = array(
      '#type' => 'value',
      '#value' => $this->node->title,
    );
  }

  // Store quiz id in the form.
  $form['quiz_nid'] = array(
    '#type' => 'hidden',
  );
  $form['quiz_vid'] = array(
    '#type' => 'hidden',
  );
  if (isset($_GET['quiz_nid']) && isset($_GET['quiz_vid'])) {
    $form['quiz_nid']['#value'] = intval($_GET['quiz_nid']);
    $form['quiz_vid']['#value'] = intval($_GET['quiz_vid']);
  }

  // Identify this node as a quiz question type so that it can be recognized
  // by other modules effectively.
  $form['is_quiz_question'] = array(
    '#type' => 'value',
    '#value' => TRUE,
  );
  if (!empty($this->node->nid)) {
    if ($properties = entity_load('quiz_question', FALSE, array(
      'nid' => $this->node->nid,
      'vid' => $this->node->vid,
    ))) {
      $quiz_question = reset($properties);
    }
  }
  $form['feedback'] = array(
    '#type' => 'text_format',
    '#title' => t('Question feedback'),
    '#default_value' => !empty($quiz_question->feedback) ? $quiz_question->feedback : '',
    '#format' => !empty($quiz_question->feedback_format) ? $quiz_question->feedback_format : filter_default_format(),
    '#description' => t('This feedback will show when configured and the user answers a question, regardless of correctness.'),
  );

  // Add question type specific content.
  $form = array_merge($form, $this
    ->getCreationForm($form_state));
  if (\Drupal::config('quiz.settings')
    ->get('auto_revisioning', 1) && $this
    ->hasBeenAnswered()) {
    $log = t('The current revision has been answered. We create a new revision so that the reports from the existing answers stays correct.');
    $this->node->revision = 1;
    $this->node->log = $log;
  }
  return $form;
}