You are here

public function QuizQuestionEntityForm::buildForm in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 src/Entity/QuizQuestionEntityForm.php \Drupal\quiz\Entity\QuizQuestionEntityForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Entity/QuizQuestionEntityForm.php, line 11

Class

QuizQuestionEntityForm

Namespace

Drupal\quiz\Entity

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $entity_manager = Drupal::entityTypeManager();
  $access_handler = $entity_manager
    ->getAccessControlHandler('quiz');
  if ($qid = Drupal::request()
    ->get('qid')) {

    // Requested addition to an existing quiz.
    $vid = Drupal::request()
      ->get('vid');
    $quiz = \Drupal::entityTypeManager()
      ->getStorage('quiz')
      ->loadRevision($vid);

    // Check if the user can add a question to the requested quiz.
    if ($access_handler
      ->access($quiz, 'update')) {
      $form['quiz_id'] = [
        '#title' => t('Quiz ID'),
        '#type' => 'value',
        '#value' => $qid,
      ];
      $form['quiz_vid'] = [
        '#title' => t('Quiz revision ID'),
        '#type' => 'value',
        '#value' => $vid,
      ];
    }
  }
  if ($this->entity
    ->hasBeenAnswered()) {
    $override = \Drupal::currentUser()
      ->hasPermission('override quiz revisioning');
    if (Drupal::config('quiz.settings')
      ->get('revisioning', FALSE)) {
      $form['revision']['#required'] = !$override;
    }
    else {
      $message = $override ? t('<strong>Warning:</strong> This question has attempts. You can edit this question, but it is not recommended.<br/>Attempts in progress and reporting will be affected.<br/>You should delete all attempts on this question before editing.') : t('You must delete all attempts on this question before editing.');

      // Revisioning is disabled.
      $form['revision_information']['#access'] = FALSE;
      $form['revision']['#access'] = FALSE;
      $form['actions']['warning'] = [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => $message,
      ];
      \Drupal::messenger()
        ->addWarning($message);
      $form['actions']['#disabled'] = TRUE;
    }
    $form['revision']['#description'] = '<strong>Warning:</strong> This question has attempts.<br/>In order to update this question you must create a new revision.<br/>This will affect reporting.<br/>You must update the quizzes with the new revision of this question.';
  }
  return $form;
}