You are here

public function OpignoGroupManagerController::removeLink in Opigno group manager 8

Same name and namespace in other branches
  1. 3.x src/Controller/OpignoGroupManagerController.php \Drupal\opigno_group_manager\Controller\OpignoGroupManagerController::removeLink()

Removes a link.

1 string reference to 'OpignoGroupManagerController::removeLink'
opigno_group_manager.routing.yml in ./opigno_group_manager.routing.yml
opigno_group_manager.routing.yml

File

src/Controller/OpignoGroupManagerController.php, line 728

Class

OpignoGroupManagerController
Controller for all the actions of the Opigno group manager app.

Namespace

Drupal\opigno_group_manager\Controller

Code

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

  // First, check that the params are okay.
  $datas = json_decode($request
    ->getContent());
  if (empty($datas->parentCid) || empty($datas->childCid)) {
    return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
  }

  // Get the params.
  $parentCid = $datas->parentCid;
  $childCid = $datas->childCid;

  // Get the links. Should be only one.
  try {
    $links = OpignoGroupManagedLink::loadByProperties([
      'group_id' => $group
        ->id(),
      'parent_content_id' => $parentCid,
      'child_content_id' => $childCid,
    ]);
  } catch (InvalidPluginDefinitionException $e) {
    return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
  }

  // Delete the links and return OK.
  foreach ($links as $link) {
    $link
      ->delete();
  }
  return new JsonResponse(NULL, Response::HTTP_OK);
}