You are here

public function CourseObjectPoll::createInstance in Course 3.x

Creates a course object.

For example, this would create the new node and return the node ID if this was a CourseObjectNode.

Do not confuse this with save(), which saves the course outline record for tracking.

Course objects should call setInstanceId() if this is a course object that creates external resources.

Overrides CourseObject::createInstance

File

modules/course_poll/src/Plugin/course/CourseObject/CourseObjectPoll.php, line 54

Class

CourseObjectPoll
Plugin annotation @CourseObject( id = "poll", label = "Poll", handlers = { "fulfillment" = "\Drupal\course_poll\Plugin\course\CourseObject\CourseObjectPollFulfillment" } )

Namespace

Drupal\course_poll\Plugin\course\CourseObject

Code

public function createInstance() {
  $poll = Poll::create();
  $poll_choice_ids = [];
  $poll_choice1 = PollChoice::create([
    'choice' => 'Yes',
  ]);
  $poll_choice1
    ->save();
  $poll_choice2 = PollChoice::create([
    'choice' => 'No',
  ]);
  $poll_choice2
    ->save();
  $poll_choice_ids[] = $poll_choice1
    ->id();
  $poll_choice_ids[] = $poll_choice2
    ->id();
  $poll
    ->set('choice', $poll_choice_ids)
    ->save();
  $this
    ->setInstanceId($poll
    ->id());
}