forked from VolantisDev/drupal-prepopulate-hide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepopulate_hide.module
More file actions
96 lines (71 loc) · 2.25 KB
/
prepopulate_hide.module
File metadata and controls
96 lines (71 loc) · 2.25 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_alter().
*/
function prepopulate_hide_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (prepopulate_hide_form_is_included($form_id)) {
$form['#after_build'][] = 'prepopulate_hide_after_build';
}
$config = \Drupal::config('prepopulate_hide.settings');
$newLines = '/(\r\n|\r|\n)/';
$prepopulateFields = preg_split($newLines, $config->get('prepopulate_from_current_page'));
foreach ($prepopulateFields as $prepopulateField) {
list($formId, $fieldName) = explode('|', $prepopulateField);
if ($formId == $form_id) {
$node_id = \Drupal::routeMatch()->getRawParameter('node');
if (!empty($node_id)) {
$form[$fieldName]['widget']['#default_value'] = $node_id;
}
}
}
}
function prepopulate_hide_after_build($form) {
$config = \Drupal::config('prepopulate_hide.settings');
$newLines = '/(\r\n|\r|\n)/';
$includedFields = preg_split($newLines, $config->get('included_fields'));
foreach ($includedFields as $fieldName) {
if (isset($form[$fieldName])) {
if (isset($form[$fieldName]['widget']['#value']) && !empty($form[$fieldName]['widget']['#value'])) {
$form[$fieldName]['widget']['#access'] = FALSE;
}
}
}
return $form;
}
function prepopulate_hide_form_is_included($form_id) {
$config = \Drupal::config('prepopulate_hide.settings');
$newLines = '/(\r\n|\r|\n)/';
$includedForms = preg_split($newLines, $config->get('included_forms'));
$excludedForms = preg_split($newLines, $config->get('excluded_forms'));
$include = FALSE;
foreach ($includedForms as $search) {
$search = trim($search);
if (!$search) {
continue;
}
if (strpos($search, '/') === 0) {
if (preg_match($search, $form_id)) {
$include = TRUE;
break;
}
} elseif ($form_id == $search) {
$include = TRUE;
break;
}
}
if ($include && !empty($excludedForms)) {
foreach ($excludedForms as $search) {
if (strpos($search, '/') === 0) {
if (preg_match($search, $form_id)) {
$include = FALSE;
break;
}
} elseif ($form_id == $search) {
$include = FALSE;
break;
}
}
}
return $include;
}