forked from OS2Forms/os2forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttachmentElement.php
More file actions
138 lines (125 loc) · 4.59 KB
/
AttachmentElement.php
File metadata and controls
138 lines (125 loc) · 4.59 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Drupal\os2forms_attachment\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Twig\WebformTwigExtension;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform_attachment\Plugin\WebformElement\WebformAttachmentBase;
/**
* Provides a 'os2forms_attachment' element.
*
* @WebformElement(
* id = "os2forms_attachment",
* label = @Translation("OS2Forms Attachment"),
* description = @Translation("Provides a customet OS2forms attachment element."),
* category = @Translation("OS2Forms")
* )
*/
class AttachmentElement extends WebformAttachmentBase {
/**
* {@inheritdoc}
*/
protected function defineDefaultProperties() {
$properties = [
'view_mode' => 'html',
'template' => '',
'export_type' => '',
'digital_signature' => '',
'exclude_empty' => '',
'exclude_empty_checkbox' => '',
'excluded_elements' => '',
] + parent::defineDefaultProperties();
// PDF documents should never be trimmed.
unset($properties['trim']);
return $properties;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
// Require export type file extension.
$file_extension = 'pdf or html';
$file_extension_pattern = "(pdf|html)";
$t_args = ['@extension' => $file_extension];
$form['attachment']['filename']['#description'] .= '<br/><br/>' . $this->t('File name must include *.@extension file extension.', $t_args);
$form['attachment']['filename']['#pattern'] = '^.*\.' . $file_extension_pattern . '$';
$form['attachment']['filename']['#pattern_error'] = $this->t('File name must include *.@extension file extension.', $t_args);
WebformElementHelper::process($form['attachment']['filename']);
// View mode.
$form['attachment']['view_mode'] = [
'#type' => 'select',
'#title' => $this->t('View mode'),
'#options' => [
'html' => $this->t('HTML'),
'table' => $this->t('Table'),
'twig' => $this->t('Twig template…'),
],
];
$form['attachment']['template'] = [
'#type' => 'webform_codemirror',
'#title' => $this->t('Twig template'),
'#title_display' => 'invisible',
'#mode' => 'twig',
'#states' => [
'visible' => [
':input[name="properties[view_mode]"]' => ['value' => 'twig'],
],
],
];
$form['attachment']['help'] = WebformTwigExtension::buildTwigHelp() + [
'#states' => [
'visible' => [
':input[name="properties[view_mode]"]' => ['value' => 'twig'],
],
],
];
$form['attachment']['export_type'] = [
'#type' => 'select',
'#title' => $this->t('Export type'),
'#options' => [
'pdf' => $this->t('PDF'),
'html' => $this->t('HTML'),
],
];
$form['attachment']['digital_signature'] = [
'#type' => 'checkbox',
'#title' => $this->t('Digital signature'),
];
// Set #access so that help is always visible.
WebformElementHelper::setPropertyRecursive($form['attachment']['help'], '#access', TRUE);
// Elements.
$form['elements'] = [
'#type' => 'details',
'#title' => $this->t('Included elements'),
'#description' => $this->t('The selected elements will be included in the [webform_submission:values] token. Individual values may still be printed if explicitly specified as a [webform_submission:values:?] in the email body template.'),
'#open' => $this->configuration['excluded_elements'] ? TRUE : FALSE,
];
$form['elements']['excluded_elements'] = [
'#type' => 'webform_excluded_elements',
'#exclude_markup' => FALSE,
'#webform_id' => $this->webform->id(),
'#default_value' => $this->configuration['excluded_elements'],
];
$form['elements']['exclude_empty'] = [
'#type' => 'checkbox',
'#title' => $this->t('Exclude empty elements'),
'#description' => $this->t('If checked, empty elements will be excluded from the email values.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['exclude_empty'],
];
$form['elements']['exclude_empty_checkbox'] = [
'#type' => 'checkbox',
'#title' => $this->t('Exclude unselected checkboxes'),
'#description' => $this->t('If checked, empty checkboxes will be excluded from the email values.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['exclude_empty_checkbox'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function getExportAttachmentsBatchLimit() {
return 10;
}
}