-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetstock_webforms.module
43 lines (31 loc) · 1.15 KB
/
setstock_webforms.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
* @file
* CNCEx Webform Submissions
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
function setstock_webforms_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// REQUEST A QUOTE
// SETS PRODUCT TITLE
if(isset($form['#webform_id']) &&
$form['#webform_id'] == 'request_a_quote') {
// GET PATH OF NODE THAT LOADED THE FORM
$request = \Drupal::request();
$referer = $request->headers->get('referer');
$base_url = Request::createFromGlobals()->getSchemeAndHttpHost();
// Getting the alias or the relative path.
$alias = substr($referer, strlen($base_url));
// Getting the node.
$params = Url::fromUri("internal:" . $alias)->getRouteParameters();
$entity_type = key($params);
$node = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]);
// EXTRACT VALUE OF FIELD
if (isset($node) && is_object($node)) {
$form['elements']['product_title']['#default_value'] = $node->get('title')->value;
}
}
}