c6b2eb646bf7a4578ba637909d0ff6fac7bfd13c
[yaffs-website] / web / modules / contrib / entity_browser / src / Plugin / EntityBrowser / Widget / Upload.php
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 Drupal\file\FileInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16 /**
17  * Uses a view to provide entity listing in a browser's widget.
18  *
19  * @EntityBrowserWidget(
20  *   id = "upload",
21  *   label = @Translation("Upload"),
22  *   description = @Translation("Adds an upload field browser's widget."),
23  *   auto_select = FALSE
24  * )
25  */
26 class Upload extends WidgetBase {
27
28   /**
29    * The module handler service.
30    *
31    * @var \Drupal\Core\Extension\ModuleHandlerInterface
32    */
33   protected $moduleHandler;
34
35   /**
36    * The token service.
37    *
38    * @var \Drupal\Core\Utility\Token
39    */
40   protected $token;
41
42   /**
43    * Upload constructor.
44    *
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
58    *   The module handler.
59    * @param \Drupal\Core\Utility\Token $token
60    *   The token service.
61    */
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;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
72     return new static(
73       $configuration,
74       $plugin_id,
75       $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')
81     );
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function defaultConfiguration() {
88     return [
89       'upload_location' => 'public://',
90       'multiple' => TRUE,
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();
94   }
95
96   /**
97    * {@inheritdoc}
98    */
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']);
102     $upload_validators = $form_state->has(['entity_browser', 'widget_context', 'upload_validators']) ? $form_state->get(['entity_browser', 'widget_context', 'upload_validators']) : [];
103     $form['upload'] = [
104       '#type' => 'managed_file',
105       '#title' => $this->t('Choose a file'),
106       '#title_display' => 'invisible',
107       '#upload_location' => $this->token->replace($this->configuration['upload_location']),
108       // Multiple uploads will only be accepted if the source field allows
109       // more than one value.
110       '#multiple' => $field_cardinality != 1 && $this->configuration['multiple'],
111       '#upload_validators' => array_merge([
112         'file_validate_extensions' => [$this->configuration['extensions']],
113       ], $upload_validators),
114     ];
115
116     return $form;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   protected function prepareEntities(array $form, FormStateInterface $form_state) {
123     $files = [];
124     foreach ($form_state->getValue(['upload'], []) as $fid) {
125       $files[] = $this->entityTypeManager->getStorage('file')->load($fid);
126     }
127     return $files;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function submit(array &$element, array &$form, FormStateInterface $form_state) {
134     if (!empty($form_state->getTriggeringElement()['#eb_widget_main_submit'])) {
135       $files = $this->prepareEntities($form, $form_state);
136       array_walk(
137         $files,
138         function (FileInterface $file) {
139           $file->setPermanent();
140           $file->save();
141         }
142       );
143       $this->selectEntities($files, $form_state);
144       $this->clearFormValues($element, $form_state);
145     }
146   }
147
148   /**
149    * Clear values from upload form element.
150    *
151    * @param array $element
152    *   Upload form element.
153    * @param \Drupal\Core\Form\FormStateInterface $form_state
154    *   Form state object.
155    */
156   protected function clearFormValues(array &$element, FormStateInterface $form_state) {
157     // We propagated entities to the other parts of the system. We can now remove
158     // them from our values.
159     $form_state->setValueForElement($element['upload']['fids'], '');
160     NestedArray::setValue($form_state->getUserInput(), $element['upload']['fids']['#parents'], '');
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
167     $form = parent::buildConfigurationForm($form, $form_state);
168
169     $form['upload_location'] = [
170       '#type' => 'textfield',
171       '#title' => $this->t('Upload location'),
172       '#default_value' => $this->configuration['upload_location'],
173     ];
174     $form['multiple'] = [
175       '#type' => 'checkbox',
176       '#title' => $this->t('Accept multiple files'),
177       '#default_value' => $this->configuration['multiple'],
178       '#description' => $this->t('Multiple uploads will only be accepted if the source field allows more than one value.'),
179     ];
180     $form['extensions'] = [
181       '#type' => 'textfield',
182       '#title' => $this->t('Allowed file extensions'),
183       '#description' => $this->t('Separate extensions with a space or comma and do not include the leading dot.'),
184       '#default_value' => $this->configuration['extensions'],
185       '#element_validate' => [[static::class, 'validateExtensions']],
186       '#required' => TRUE,
187     ];
188
189     if ($this->moduleHandler->moduleExists('token')) {
190       $form['token_help'] = [
191         '#theme' => 'token_tree_link',
192         '#token_types' => ['file'],
193       ];
194       $form['upload_location']['#description'] = $this->t('You can use tokens in the upload location.');
195     }
196
197     return $form;
198   }
199
200   /**
201    * Validates a list of file extensions.
202    *
203    * @See \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions
204    */
205   public static function validateExtensions($element, FormStateInterface $form_state) {
206     if (!empty($element['#value'])) {
207       $extensions = preg_replace('/([, ]+\.?)/', ' ', trim(strtolower($element['#value'])));
208       $extensions = array_filter(explode(' ', $extensions));
209       $extensions = implode(' ', array_unique($extensions));
210       if (!preg_match('/^([a-z0-9]+([.][a-z0-9])* ?)+$/', $extensions)) {
211         $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.'));
212       }
213       else {
214         $form_state->setValueForElement($element, $extensions);
215       }
216     }
217   }
218
219 }