Completed
Pull Request — 8.x-1.x (#117)
by
unknown
04:17
created

Upload::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
1
<?php
2
3
/**
4
 * Contains \Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload.
5
 */
6
7
namespace Drupal\entity_browser\Plugin\EntityBrowser\Widget;
8
9
use Drupal\Component\Utility\NestedArray;
10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\Core\TypedData\DataDefinition;
12
use Drupal\Core\TypedData\TypedData;
13
use Drupal\entity_browser\WidgetBase;
14
use Drupal\file\Plugin\Field\FieldType\FileItem;
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
 * )
24
 */
25
class Upload extends WidgetBase {
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public function defaultConfiguration() {
31
    return [
32
      'upload_location' => 'public://',
33
    ] + parent::defaultConfiguration();
34
  }
35
36
  /**
37
   * {@inheritdoc}
38
   */
39
  public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) {
40
    $form = [];
41
    $form['upload'] = [
42
      '#type' => 'managed_file',
43
      '#title' => t('Choose a file'),
44
      '#title_display' => 'invisible',
45
      '#upload_location' => $this->configuration['upload_location'],
46
      '#multiple' => TRUE,
47
    ];
48
49
    return $form;
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function prepareEntities(FormStateInterface $form_state) {
56
    $files = [];
57
    foreach ($form_state->getValue(['upload'], []) as $fid) {
58
      $files[] = $this->entityManager->getStorage('file')->load($fid);
59
    }
60
    return $files;
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public function submit(array &$element, array &$form, FormStateInterface $form_state) {
67
    $files = $this->extractFiles($form_state);
68
    $this->selectEntities($files, $form_state);
69
    $this->clearFormValues($element, $form_state);
70
  }
71
72
  /**
73
   * Clear values from upload form element.
74
   *
75
   * @param array $element
76
   *   Upload form element.
77
   * @param \Drupal\Core\Form\FormStateInterface $form_state
78
   *   Form state object.
79
   */
80
  protected function clearFormValues(array &$element, FormStateInterface $form_state) {
81
    // We propagated entities to the other parts of the system. We can now remove
82
    // them from our values.
83
    $form_state->setValueForElement($element['upload']['fids'], '');
84
    NestedArray::setValue($form_state->getUserInput(), $element['upload']['fids']['#parents'], '');
85
  }
86
87
  /**
88
   * @param FormStateInterface $form_state
89
   *   Form state object.
90
   *
91
   * @return \Drupal\file\FileInterface[]
92
   *   Array of files.
93
   */
94
  protected function extractFiles(FormStateInterface $form_state) {
95
    $files = [];
96
    foreach ($form_state->getValue(['upload'], []) as $fid) {
97
      /** @var \Drupal\file\FileInterface $file */
98
      $file = $this->entityManager->getStorage('file')->load($fid);
99
      $file->setPermanent();
100
      $file->save();
101
      $files[] = $file;
102
    }
103
104
    return $files;
105
  }
106
107
  /**
108
   * {@inheritdoc}
109
   */
110
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
111
    $form['upload_location'] = [
112
      '#type' => 'textfield',
113
      '#title' => $this->t('Upload location'),
114
      '#default_value' => $this->configuration['upload_location'],
115
    ];
116
117
    return $form;
118
  }
119
120
}
121