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']);
102 $upload_validators = $form_state->has(['entity_browser', 'widget_context', 'upload_validators']) ? $form_state->get(['entity_browser', 'widget_context', 'upload_validators']) : [];
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),
122 protected function prepareEntities(array $form, FormStateInterface $form_state) {
124 foreach ($form_state->getValue(['upload'], []) as $fid) {
125 $files[] = $this->entityTypeManager->getStorage('file')->load($fid);
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);
138 function (FileInterface $file) {
139 $file->setPermanent();
143 $this->selectEntities($files, $form_state);
144 $this->clearFormValues($element, $form_state);
149 * Clear values from upload form element.
151 * @param array $element
152 * Upload form element.
153 * @param \Drupal\Core\Form\FormStateInterface $form_state
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'], '');
166 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
167 $form = parent::buildConfigurationForm($form, $form_state);
169 $form['upload_location'] = [
170 '#type' => 'textfield',
171 '#title' => $this->t('Upload location'),
172 '#default_value' => $this->configuration['upload_location'],
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.'),
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']],
189 if ($this->moduleHandler->moduleExists('token')) {
190 $form['token_help'] = [
191 '#theme' => 'token_tree_link',
192 '#token_types' => ['file'],
194 $form['upload_location']['#description'] = $this->t('You can use tokens in the upload location.');
201 * Validates a list of file extensions.
203 * @See \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions
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.'));
214 $form_state->setValueForElement($element, $extensions);