Version 1
[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     $form['upload'] = [
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']],
112       ],
113     ];
114
115     return $form;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   protected function prepareEntities(array $form, FormStateInterface $form_state) {
122     $files = [];
123     foreach ($form_state->getValue(['upload'], []) as $fid) {
124       $files[] = $this->entityTypeManager->getStorage('file')->load($fid);
125     }
126     return $files;
127   }
128
129   /**
130    * {@inheritdoc}
131    */
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);
135       array_walk(
136         $files,
137         function (FileInterface $file) {
138           $file->setPermanent();
139           $file->save();
140         }
141       );
142       $this->selectEntities($files, $form_state);
143       $this->clearFormValues($element, $form_state);
144     }
145   }
146
147   /**
148    * Clear values from upload form element.
149    *
150    * @param array $element
151    *   Upload form element.
152    * @param \Drupal\Core\Form\FormStateInterface $form_state
153    *   Form state object.
154    */
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'], '');
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
166     $form = parent::buildConfigurationForm($form, $form_state);
167
168     $form['upload_location'] = [
169       '#type' => 'textfield',
170       '#title' => $this->t('Upload location'),
171       '#default_value' => $this->configuration['upload_location'],
172     ];
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.'),
178     ];
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']],
185       '#required' => TRUE,
186     ];
187
188     if ($this->moduleHandler->moduleExists('token')) {
189       $form['token_help'] = [
190         '#theme' => 'token_tree_link',
191         '#token_types' => ['file'],
192       ];
193       $form['upload_location']['#description'] = $this->t('You can use tokens in the upload location.');
194     }
195
196     return $form;
197   }
198
199   /**
200    * Validates a list of file extensions.
201    *
202    * @See \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions
203    */
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.'));
211       }
212       else {
213         $form_state->setValueForElement($element, $extensions);
214       }
215     }
216   }
217
218 }