|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\entity_browser\Plugin\EntityBrowser\Widget; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\NestedArray; |
|
6
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
7
|
|
|
use Drupal\Core\Extension\ModuleHandlerInterface; |
|
8
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
9
|
|
|
use Drupal\Core\Utility\Token; |
|
10
|
|
|
use Drupal\entity_browser\WidgetBase; |
|
11
|
|
|
use Drupal\entity_browser\WidgetValidationManager; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Uses a view to provide entity listing in a browser's widget. |
|
17
|
|
|
* |
|
18
|
|
|
* @EntityBrowserWidget( |
|
19
|
|
|
* id = "upload", |
|
20
|
|
|
* label = @Translation("Upload"), |
|
21
|
|
|
* description = @Translation("Adds an upload field browser's widget.") |
|
22
|
|
|
* ) |
|
23
|
|
|
*/ |
|
24
|
|
|
class Upload extends WidgetBase { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The module handler service. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Drupal\Core\Extension\ModuleHandlerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $moduleHandler; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The token service. |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Drupal\Core\Utility\Token |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $token; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Upload constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $configuration |
|
44
|
|
|
* A configuration array containing information about the plugin instance. |
|
45
|
|
|
* @param string $plugin_id |
|
46
|
|
|
* The plugin_id for the plugin instance. |
|
47
|
|
|
* @param mixed $plugin_definition |
|
48
|
|
|
* The plugin implementation definition. |
|
49
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
|
50
|
|
|
* Event dispatcher service. |
|
51
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
|
52
|
|
|
* The entity type manager service. |
|
53
|
|
|
* @param \Drupal\entity_browser\WidgetValidationManager $validation_manager |
|
54
|
|
|
* The Widget Validation Manager service. |
|
55
|
|
|
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
|
56
|
|
|
* The module handler. |
|
57
|
|
|
* @param \Drupal\Core\Utility\Token $token |
|
58
|
|
|
* The token service. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, ModuleHandlerInterface $module_handler, Token $token) { |
|
61
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager); |
|
62
|
|
|
$this->moduleHandler = $module_handler; |
|
63
|
|
|
$this->token = $token; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
70
|
|
|
return new static( |
|
71
|
|
|
$configuration, |
|
72
|
|
|
$plugin_id, |
|
73
|
|
|
$plugin_definition, |
|
74
|
|
|
$container->get('event_dispatcher'), |
|
75
|
|
|
$container->get('entity_type.manager'), |
|
76
|
|
|
$container->get('plugin.manager.entity_browser.widget_validation'), |
|
77
|
|
|
$container->get('module_handler'), |
|
78
|
|
|
$container->get('token') |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function defaultConfiguration() { |
|
86
|
|
|
return [ |
|
87
|
|
|
'upload_location' => 'public://', |
|
88
|
|
|
] + parent::defaultConfiguration(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) { |
|
95
|
|
|
$form = []; |
|
96
|
|
|
$form['upload'] = [ |
|
97
|
|
|
'#type' => 'managed_file', |
|
98
|
|
|
'#title' => t('Choose a file'), |
|
99
|
|
|
'#title_display' => 'invisible', |
|
100
|
|
|
'#upload_location' => $this->token->replace($this->configuration['upload_location']), |
|
101
|
|
|
'#multiple' => TRUE, |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
return $form; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function prepareEntities(FormStateInterface $form_state) { |
|
111
|
|
|
$files = []; |
|
112
|
|
|
foreach ($form_state->getValue(['upload'], []) as $fid) { |
|
113
|
|
|
$files[] = $this->entityTypeManager->getStorage('file')->load($fid); |
|
114
|
|
|
} |
|
115
|
|
|
return $files; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public function submit(array &$element, array &$form, FormStateInterface $form_state) { |
|
122
|
|
|
$files = $this->extractFiles($form_state); |
|
123
|
|
|
$this->selectEntities($files, $form_state); |
|
124
|
|
|
$this->clearFormValues($element, $form_state); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Clear values from upload form element. |
|
129
|
|
|
* |
|
130
|
|
|
* @param array $element |
|
131
|
|
|
* Upload form element. |
|
132
|
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
|
133
|
|
|
* Form state object. |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function clearFormValues(array &$element, FormStateInterface $form_state) { |
|
136
|
|
|
// We propagated entities to the other parts of the system. We can now remove |
|
137
|
|
|
// them from our values. |
|
138
|
|
|
$form_state->setValueForElement($element['upload']['fids'], ''); |
|
139
|
|
|
NestedArray::setValue($form_state->getUserInput(), $element['upload']['fids']['#parents'], ''); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param FormStateInterface $form_state |
|
144
|
|
|
* Form state object. |
|
145
|
|
|
* |
|
146
|
|
|
* @return \Drupal\file\FileInterface[] |
|
147
|
|
|
* Array of files. |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function extractFiles(FormStateInterface $form_state) { |
|
150
|
|
|
$files = []; |
|
151
|
|
|
foreach ($form_state->getValue(['upload'], []) as $fid) { |
|
152
|
|
|
/** @var \Drupal\file\FileInterface $file */ |
|
153
|
|
|
$file = $this->entityTypeManager->getStorage('file')->load($fid); |
|
154
|
|
|
$file->setPermanent(); |
|
155
|
|
|
$file->save(); |
|
156
|
|
|
$files[] = $file; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $files; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
|
166
|
|
|
$form['upload_location'] = [ |
|
167
|
|
|
'#type' => 'textfield', |
|
168
|
|
|
'#title' => $this->t('Upload location'), |
|
169
|
|
|
'#default_value' => $this->configuration['upload_location'], |
|
170
|
|
|
]; |
|
171
|
|
|
|
|
172
|
|
|
if ($this->moduleHandler->moduleExists('token')) { |
|
173
|
|
|
$form['token_help'] = [ |
|
174
|
|
|
'#theme' => 'token_tree_link', |
|
175
|
|
|
'#token_types' => ['file'], |
|
176
|
|
|
]; |
|
177
|
|
|
$form['upload_location']['#description'] = $this->t('You can use tokens in the upload location.'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $form; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|