3 namespace Drupal\entity_browser\Plugin\EntityBrowser\Widget;
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 Drupal\file\FileInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17 * Uses a view to provide entity listing in a browser's widget.
19 * @EntityBrowserWidget(
21 * label = @Translation("Upload"),
22 * description = @Translation("Adds an upload field browser's widget."),
26 class Upload extends WidgetBase {
29 * The module handler service.
31 * @var \Drupal\Core\Extension\ModuleHandlerInterface
33 protected $moduleHandler;
38 * @var \Drupal\Core\Utility\Token
45 * @param array $configuration
46 * A configuration array containing information about the plugin instance.
47 * @param string $plugin_id
48 * The plugin_id for the plugin instance.
49 * @param mixed $plugin_definition
50 * The plugin implementation definition.
51 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
52 * Event dispatcher service.
53 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
54 * The entity type manager service.
55 * @param \Drupal\entity_browser\WidgetValidationManager $validation_manager
56 * The Widget Validation Manager service.
57 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
59 * @param \Drupal\Core\Utility\Token $token
62 public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, ModuleHandlerInterface $module_handler, Token $token) {
63 parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager);
64 $this->moduleHandler = $module_handler;
65 $this->token = $token;
71 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
76 $container->get('event_dispatcher'),
77 $container->get('entity_type.manager'),
78 $container->get('plugin.manager.entity_browser.widget_validation'),
79 $container->get('module_handler'),
80 $container->get('token')
87 public function defaultConfiguration() {
89 'upload_location' => 'public://',
91 'submit_text' => $this->t('Select files'),
92 'extensions' => 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp',
93 ] + parent::defaultConfiguration();
99 public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
100 $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
101 $field_cardinality = $form_state->get(['entity_browser', 'validators', 'cardinality', 'cardinality']);
103 '#type' => 'managed_file',
104 '#title' => $this->t('Choose a file'),
105 '#title_display' => 'invisible',
106 '#upload_location' => $this->token->replace($this->configuration['upload_location']),
107 // Multiple uploads will only be accepted if the source field allows
108 // more than one value.
109 '#multiple' => $field_cardinality != 1 && $this->configuration['multiple'],
110 '#upload_validators' => [
111 'file_validate_extensions' => [$this->configuration['extensions']],
121 protected function prepareEntities(array $form, FormStateInterface $form_state) {
123 foreach ($form_state->getValue(['upload'], []) as $fid) {
124 $files[] = $this->entityTypeManager->getStorage('file')->load($fid);
132 public function submit(array &$element, array &$form, FormStateInterface $form_state) {
133 if (!empty($form_state->getTriggeringElement()['#eb_widget_main_submit'])) {
134 $files = $this->prepareEntities($form, $form_state);
137 function (FileInterface $file) {
138 $file->setPermanent();
142 $this->selectEntities($files, $form_state);
143 $this->clearFormValues($element, $form_state);
148 * Clear values from upload form element.
150 * @param array $element
151 * Upload form element.
152 * @param \Drupal\Core\Form\FormStateInterface $form_state
155 protected function clearFormValues(array &$element, FormStateInterface $form_state) {
156 // We propagated entities to the other parts of the system. We can now remove
157 // them from our values.
158 $form_state->setValueForElement($element['upload']['fids'], '');
159 NestedArray::setValue($form_state->getUserInput(), $element['upload']['fids']['#parents'], '');
165 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
166 $form = parent::buildConfigurationForm($form, $form_state);
168 $form['upload_location'] = [
169 '#type' => 'textfield',
170 '#title' => $this->t('Upload location'),
171 '#default_value' => $this->configuration['upload_location'],
173 $form['multiple'] = [
174 '#type' => 'checkbox',
175 '#title' => $this->t('Accept multiple files'),
176 '#default_value' => $this->configuration['multiple'],
177 '#description' => $this->t('Multiple uploads will only be accepted if the source field allows more than one value.'),
179 $form['extensions'] = [
180 '#type' => 'textfield',
181 '#title' => $this->t('Allowed file extensions'),
182 '#description' => $this->t('Separate extensions with a space or comma and do not include the leading dot.'),
183 '#default_value' => $this->configuration['extensions'],
184 '#element_validate' => [[static::class, 'validateExtensions']],
188 if ($this->moduleHandler->moduleExists('token')) {
189 $form['token_help'] = [
190 '#theme' => 'token_tree_link',
191 '#token_types' => ['file'],
193 $form['upload_location']['#description'] = $this->t('You can use tokens in the upload location.');
200 * Validates a list of file extensions.
202 * @See \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions
204 public static function validateExtensions($element, FormStateInterface $form_state) {
205 if (!empty($element['#value'])) {
206 $extensions = preg_replace('/([, ]+\.?)/', ' ', trim(strtolower($element['#value'])));
207 $extensions = array_filter(explode(' ', $extensions));
208 $extensions = implode(' ', array_unique($extensions));
209 if (!preg_match('/^([a-z0-9]+([.][a-z0-9])* ?)+$/', $extensions)) {
210 $form_state->setError($element, t('The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.'));
213 $form_state->setValueForElement($element, $extensions);