materio expo use now cookie instead of session

This commit is contained in:
Bachir Soussi Chiadmi 2021-09-09 16:14:11 +02:00
parent 2ffda1e0b2
commit 277f3cb445
3 changed files with 33 additions and 18 deletions

View File

@ -1,14 +1,18 @@
materio_expo.qr_controller_getfile:
path: '/materio_expo/qr/file/{ref}'
defaults:
_controller: '\Drupal\materio_expo\Controller\QRController::getfile'
# _title: 'Get File'
requirements:
_permission: 'access content'
materio_expo.get_email:
path: '/materio_expo/qr/form/{ref}'
defaults:
_form: '\Drupal\materio_expo\Form\GetEmail'
# _title: ''
requirements:
_access: 'TRUE'
materio_expo.qr_controller_setcookie:
path: '/materio_expo/qr/cookie/{ref}'
defaults:
_controller: '\Drupal\materio_expo\Controller\QRController::setcookie'
requirements:
_permission: 'access content'
materio_expo.qr_controller_getfile:
path: '/materio_expo/qr/file/{ref}'
defaults:
_controller: '\Drupal\materio_expo\Controller\QRController::getfile'
requirements:
_permission: 'access content'

View File

@ -5,24 +5,35 @@ namespace Drupal\materio_expo\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Cookie;
/**
* Class QRController.
*/
class QRController extends ControllerBase {
public function setcookie($ref) {
//? https://drupal.stackexchange.com/questions/278203/how-do-i-set-a-cookie-using-response-headers-setcookie-in-a-form-submission
// $form_state->setRedirect('materio_expo.qr_controller_getfile', ['ref'=>$ref]);
// $response = new RedirectResponse(!empty($destination) ? $destination : '/');
$url = Url::fromRoute('materio_expo.qr_controller_getfile', ['ref'=>$ref]);
$response = new RedirectResponse($url->toString());
$response->headers->setCookie(new Cookie('materio_expo_email', true, strtotime( '+3 days' ), '/'));
$response->send();
return $response;
}
/**
* Hello.
*
* @return string
* Return Hello string.
*/
public function getfile($ref) {
//? https://drupal.stackexchange.com/questions/255216/how-to-do-a-redirectresponse-with-a-destination-query-parameter
//* check if email cookie is present (form submitted)
$session = \Drupal::request()->getSession();
$alreadysetemail = $session->get('materio_expo_email');
// $session = \Drupal::request()->getSession();
// $alreadysetemail = $session->get('materio_expo_email');
$alreadysetemail = \Drupal::request()->cookies->get('materio_expo_email', false);
if($alreadysetemail){
//* get node from ref

View File

@ -4,7 +4,6 @@ namespace Drupal\materio_expo\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
// use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -52,8 +51,9 @@ class GetEmail extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state, $ref = null) {
// check if email cookie is already present
$session = \Drupal::request()->getSession();
$alreadysetemail = $session->get('materio_expo_email');
// $session = \Drupal::request()->getSession();
// $alreadysetemail = $session->get('materio_expo_email');
$alreadysetemail = \Drupal::request()->cookies->get('materio_expo_email', false);
if($alreadysetemail){
// https://x-team.com/blog/drupal-goto/
$url = Url::fromRoute('materio_expo.qr_controller_getfile', ['ref'=>$ref], ['absolute' => TRUE]);
@ -145,7 +145,7 @@ class GetEmail extends FormBase {
//* redirect to getfile controller
$ref = $values['ref'];
$form_state->setRedirect('materio_expo.qr_controller_getfile', ['ref'=>$ref]);
$form_state->setRedirect('materio_expo.qr_controller_setcookie', ['ref'=>$ref]);
}