QRController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\materio_expo\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Drupal\Core\Url;
  6. use Symfony\Component\HttpFoundation\Cookie;
  7. /**
  8. * Class QRController.
  9. */
  10. class QRController extends ControllerBase {
  11. public function setcookie($ref) {
  12. //? https://drupal.stackexchange.com/questions/278203/how-do-i-set-a-cookie-using-response-headers-setcookie-in-a-form-submission
  13. // $form_state->setRedirect('materio_expo.qr_controller_getfile', ['ref'=>$ref]);
  14. // $response = new RedirectResponse(!empty($destination) ? $destination : '/');
  15. $url = Url::fromRoute('materio_expo.qr_controller_getfile', ['ref'=>$ref]);
  16. $response = new RedirectResponse($url->toString());
  17. $response->headers->setCookie(new Cookie('materio_expo_email', true, strtotime( '+3 days' ), '/'));
  18. $response->send();
  19. return $response;
  20. }
  21. /**
  22. */
  23. public function getfile($ref) {
  24. //? https://drupal.stackexchange.com/questions/255216/how-to-do-a-redirectresponse-with-a-destination-query-parameter
  25. //* check if email cookie is present (form submitted)
  26. // $session = \Drupal::request()->getSession();
  27. // $alreadysetemail = $session->get('materio_expo_email');
  28. $alreadysetemail = \Drupal::request()->cookies->get('materio_expo_email', false);
  29. if($alreadysetemail){
  30. //* get node from ref
  31. $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
  32. $query = $entity_storage->getQuery()
  33. ->condition('type', 'materiau')
  34. ->condition('status', '1')
  35. ->accessCheck()
  36. ->condition('field_reference', $ref);
  37. $results = $query->execute();
  38. $nid = array_pop($results);
  39. $node = $entity_storage->load($nid);
  40. //* get file from node
  41. $file = $node->get('field_fiche_expo')->referencedEntities()[0];
  42. //* redirect to file
  43. return new RedirectResponse($file->createFileUrl());
  44. // return [
  45. // '#type' => 'markup',
  46. // '#markup' => $this->t("Error, email not provided"),
  47. // ];
  48. } else {
  49. // return [
  50. // '#type' => 'markup',
  51. // '#markup' => $this->t("Error, email not provided"),
  52. // ];
  53. $url = Url::fromRoute('materio_expo.get_email', ['ref'=>$ref]);
  54. return new RedirectResponse($url->toString());
  55. }
  56. }
  57. }