You are here

public function LearningPathManagerController::addItem 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::addItem()

This method adds an item (content) in the learning path.

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

File

src/Controller/LearningPathManagerController.php, line 289

Class

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

Namespace

Drupal\opigno_learning_path\Controller

Code

public function addItem(Group $group, Request $request) {

  // First, check if all parameters are here.
  $datas = json_decode($request
    ->getContent());
  if (empty($datas->entityId) || empty($datas->contentType)) {
    return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
  }

  // Get the params.
  $entityId = $datas->entityId;
  $contentType = $datas->contentType;
  $parentCid = empty($datas->parentCid) ? NULL : $datas->parentCid;

  // Create the added item as an LP content.
  $new_content = LPManagedContent::createWithValues($group
    ->id(), $contentType, $entityId);
  $new_content
    ->save();

  // Then, create the links to the parent content.
  if (!empty($parentCid)) {
    LPManagedLink::createWithValues($group
      ->id(), $parentCid, $new_content
      ->id())
      ->save();
  }

  /* @todo Update add functionality when Module app implementation will be done. */

  // Add created course entity as Group content.
  if ($datas->contentType == 'ContentTypeCourse') {

    // Load Course (Group) entity and save as content using specific plugin,.
    $added_entity = \Drupal::entityTypeManager()
      ->getStorage('group')
      ->load($datas->entityId);
    $group
      ->addContent($added_entity, 'subgroup:' . $added_entity
      ->bundle());
  }
  return new JsonResponse([
    'cid' => $new_content
      ->id(),
  ], Response::HTTP_OK);
}