expo qr code to get visitors email

This commit is contained in:
2021-09-07 00:50:12 +02:00
parent 31790f5ab7
commit 4ea79bca79
28 changed files with 327 additions and 7 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace Drupal\materio_expo\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
/**
* Class QRController.
*/
class QRController extends ControllerBase {
/**
* 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
//* 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"),
// ];
}
}

View File

@@ -0,0 +1,98 @@
<?php
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;
/**
* Class GetEmail.
*/
class GetEmail extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'get_email';
}
/**
* {@inheritdoc}
*/
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');
if($alreadysetemail){
// https://x-team.com/blog/drupal-goto/
$url = Url::fromRoute('materio_expo.qr_controller_getfile', ['ref'=>$ref], ['absolute' => TRUE]);
return new RedirectResponse($url->toString());
}else{
$form['email'] = [
'#type' => 'email',
// '#title' => $this->t('Email'),
'#attributes' => [
'placeholder' => $this->t('Email')
],
'#weight' => '0',
];
$form['ref'] = [
'#type' => 'hidden',
'#value' => $ref
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t("Submit"),
];
return $form;
}
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
//* send form submission by email
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'materio_expo';
$key = 'expo_getmail_submitted';
$to = \Drupal::config('system.site')->get('mail');
$params['mail'] = $values['mail'];
$params['message'] = $values['mail'];
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$send = true;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($result['result'] !== true) {
$logger = \Drupal::logger('flag_lists');
$logger->error('There was a problem sending your message.' . $values['mail']);
}
//* record email as session variable
// https://drupal.stackexchange.com/questions/197576/storing-data-session-for-anonymous-user
$session = \Drupal::request()->getSession();
$session->set('materio_expo_email', true);
//* redirect to getfile controller
$ref = $values['ref'];
$form_state->setRedirect('materio_expo.qr_controller_getfile', ['ref'=>$ref]);
}
}