Cannot create quiz questions programmatically

Created on 13 May 2025, 2 days ago

Problem/Motivation

I cannot create quiz questions without errors. If I open the quiz overview page (/admin/quiz/questions) I do get the following error:

The website encountered an unexpected error. Try again later.

Error: Call to a member function label() on null in Drupal\quiz\Config\Entity\QuizQuestionListBuilder->buildRow() (line 37 of modules/contrib/quiz/src/Config/Entity/QuizQuestionListBuilder.php).
Drupal\Core\Entity\EntityListBuilder->render() (Line: 18)
Drupal\quiz\Config\Entity\QuizQuestionListBuilder->render() (Line: 23)
Drupal\Core\Entity\Controller\EntityListController->listing()
call_user_func_array() (Line: 123)

Steps to reproduce

I tried to create quiz questions like this:

      $question = QuizQuestion::create([
        'type' => 'default',
        'title' => "Sample Question",
        'label' => "Sample Question",
        'body' => [
          'value' => "What is the answer to question?",
          'format' => 'plain_text',
        ],
        'uid' => 1,
        'status' => 1,
        'feedback' => [
          'value' => "Feedback for question.",
          'format' => 'plain_text',
        ],
      ]);
      $question->save();

What am I missing? Is there any example code?

💬 Support request
Status

Active

Version

7.0

Component

Code - Import/export

Created by

🇩🇪Germany Peter Majmesku 🇩🇪Düsseldorf

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Peter Majmesku
  • 🇩🇪Germany Peter Majmesku 🇩🇪Düsseldorf

    Please notice: I would like to create a multiple-choice question programmatically.

  • 🇩🇪Germany Peter Majmesku 🇩🇪Düsseldorf

    I was able to do it like this:

          $a = Paragraph::create([
            'type' => 'multichoice',
            'multichoice_correct' => 1,
            'multichoice_answer' => 'Alternative A',
            'multichoice_feedback_chosen' => 'You chose A',
            'multichoice_feedback_not_chosen' => 'You did not choose A',
            'multichoice_score_chosen' => 1,
            'multichoice_score_not_chosen' => 0,
          ]);
          $a->save();
    
          $b = Paragraph::create([
            'type' => 'multichoice',
            'multichoice_answer' => 'Alternative B',
            'multichoice_feedback_chosen' => 'You chose B',
            'multichoice_feedback_not_chosen' => 'You did not choose B',
            'multichoice_score_chosen' => -1,
            'multichoice_score_not_chosen' => 0,
          ]);
          $b->save();
    
          $c = Paragraph::create([
            'type' => 'multichoice',
            'multichoice_answer' => 'Alternative C',
            'multichoice_feedback_chosen' => 'You chose C',
            'multichoice_feedback_not_chosen' => 'You did not choose C',
            'multichoice_score_chosen' => -1,
            'multichoice_score_not_chosen' => 0,
          ]);
          $c->save();
    
          $question = QuizQuestion::create([
              'title' => 'MCQ 1 Title',
              'type' => 'multichoice',
              'choice_multi' => 0,
              'choice_random' => 0,
              'choice_boolean' => 0,
              'body' => 'MCQ 1 body text',
            ]);
          $question->get('alternatives')->appendItem($a);
          $question->get('alternatives')->appendItem($b);
          $question->get('alternatives')->appendItem($c);
    
          $question->save();
    

    Found code examples in the automated tests. See quiz/modules/quiz_multichoice/tests/src/Functional/QuizMultichoiceTestCase.php

Production build 0.71.5 2024