Browse Source

check in controller if form was submited in this session

bach 2 years ago
parent
commit
e6f0cbb190
1 changed files with 26 additions and 20 deletions
  1. 26 20
      web/modules/custom/materio_expo/src/Controller/QRController.php

+ 26 - 20
web/modules/custom/materio_expo/src/Controller/QRController.php

@@ -20,26 +20,32 @@ class QRController extends ControllerBase {
   public function getfile($ref) {
     //? https://drupal.stackexchange.com/questions/255216/how-to-do-a-redirectresponse-with-a-destination-query-parameter
 
-    //* get node from ref
-    $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
-    $query = $entity_storage->getQuery()
-      ->condition('type', 'materiau')
-      ->condition('status', '1')
-      ->condition('field_reference', $ref);
-    $results = $query->execute();
-    $nid = array_pop($results);
-    $node = $entity_storage->load($nid);
-
-    //* get file from node
-    $file = $node->get('field_fiche_expo')->referencedEntities()[0];
-
-    //* redirect to file
-    return new RedirectResponse($file->createFileUrl());
-
-    // return [
-    //   '#type' => 'markup',
-    //   '#markup' => $this->t("Implement method: getfile with parameter(s): $ref"),
-    // ];
+    //* check if email cookie is present (form submitted)
+    $session = \Drupal::request()->getSession();
+    $alreadysetemail = $session->get('materio_expo_email');
+    if($alreadysetemail){
+      //* get node from ref
+      $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
+      $query = $entity_storage->getQuery()
+        ->condition('type', 'materiau')
+        ->condition('status', '1')
+        ->condition('field_reference', $ref);
+      $results = $query->execute();
+      $nid = array_pop($results);
+      $node = $entity_storage->load($nid);
+
+      //* get file from node
+      $file = $node->get('field_fiche_expo')->referencedEntities()[0];
+
+      //* redirect to file
+      return new RedirectResponse($file->createFileUrl());
+
+    } else {
+      return [
+        '#type' => 'markup',
+        '#markup' => $this->t("Error, email not provided"),
+      ];
+    }
   }
 
 }