You are here

public function LearningPathManagerController::getItems in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Controller/LearningPathManagerController.php \Drupal\opigno_learning_path\Controller\LearningPathManagerController::getItems()

This method is called on learning path load.

It returns all the steps and their links in JSON format.

1 string reference to 'LearningPathManagerController::getItems'
opigno_learning_path.routing.yml in ./opigno_learning_path.routing.yml
opigno_learning_path.routing.yml

File

src/Controller/LearningPathManagerController.php, line 213

Class

LearningPathManagerController
Controller for all the actions of the Learning Path manager app.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function getItems(Group $group) {

  // Init the response and get all the contents from this learning path.
  $entities = [];
  $managed_contents = LPManagedContent::loadByProperties([
    'learning_path_id' => $group
      ->id(),
  ]);

  // TODO: Maybe extend the class LPManagedContent with LearningPathContent
  // (and use Parent::__constructor() to fill the params).
  // Convert all the LPManagedContent to
  // LearningPathContent and convert it to an array.
  foreach ($managed_contents as $managed_content) {

    // Need the content type object to get the LearningPathContent object.
    $content_type_id = $managed_content
      ->getLearningPathContentTypeId();
    $content_type = $this->content_types_manager
      ->createInstance($content_type_id);
    $lp_content = $content_type
      ->getContent($managed_content
      ->getEntityId());

    // Create the array that is ready for JSON.
    $manager_array = $lp_content
      ->toManagerArray($managed_content);
    $entities[] = $manager_array;
  }

  // Return all the contents in JSON format.
  return new JsonResponse($entities, Response::HTTP_OK);
}