QuizQuestionEntityForm.php in Quiz 8.5
File
src/Entity/QuizQuestionEntityForm.php
View source
<?php
namespace Drupal\quiz\Entity;
use Drupal;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class QuizQuestionEntityForm extends ContentEntityForm {
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')) {
$vid = Drupal::request()
->get('vid');
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($vid);
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.');
$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;
}
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
if ($qid = $form_state
->getValue('quiz_id')) {
$vid = $form_state
->getValue('quiz_vid');
$quiz = Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($vid);
$quiz
->addQuestion($this->entity);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
if ($qid = $form_state
->getValue('quiz_id')) {
$form_state
->setRedirect('quiz.questions', [
'quiz' => $qid,
]);
}
}
}