tNikh

drupal 8 API route controller

Jul 15th, 2019
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use Symfony\Component\HttpFoundation\Request;
  2. use Symfony\Component\HttpFoundation\JsonResponse;
  3. use Symfony\Component\HttpFoundation\Response;
  4.  
  5. /**
  6. * Implements hook_node_presave().
  7. */
  8. function abc_node_presave($node)
  9. {
  10. if($enable_field_full_json_string && ($node->bundle() == "dynamic_api")){
  11. $json_response_arr = abc($node);
  12. $ref_link_entities = $node->get('field_api_key_value_pairs')->referencedEntities();
  13. $key_value = $ref_link_node->get('field_pair_api_key')->value;
  14. foreach ($ref_link_entities as $ref_link_node){
  15. $json_response = json_encode($json_response_arr, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
  16. $node->set('field_full_json_string', $json_response);
  17. }
  18. }
  19.  
  20. $json_value = $node->get('field_json_api_content')->value;
  21. $response = new Response();
  22. $response->setContent($json_value);
  23. $response->headers->set('Content-Type', 'application/json');
  24. return $response;
  25.  
  26. $query = \Drupal::entityQuery('node')
  27. ->condition('status', 1)
  28. ->condition('field_content_api_id.value', $node_api_id);
  29.  
  30. $nids = $query->execute();
  31. $node = \Drupal\node\Entity\Node::load(reset($nids));
  32. if(empty($node)){
  33. $response = "Not found";
  34. return new JsonResponse($response);
  35. }
  36. $response = abc($node);
  37. if ($functioncall) {
  38. return $response;
  39. } else {
  40. return new JsonResponse($response);
  41. }
RAW Paste Data