3 namespace Drupal\file_module_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Form controller for file_module_test module.
13 class FileModuleTestForm extends FormBase {
18 public function getFormId() {
19 return 'file_module_test_form';
26 * An associative array containing the structure of the form.
27 * @param \Drupal\Core\Form\FormStateInterface $form_state
28 * The current state of the form.
30 * (optional) If the form should use #tree. Defaults to TRUE.
31 * @param bool $extended
32 * (optional) If the form should use #extended. Defaults to TRUE.
33 * @param bool $multiple
34 * (optional) If the form should use #multiple. Defaults to FALSE.
35 * @param array $default_fids
36 * (optional) Any default file IDs to use.
38 public function buildForm(array $form, FormStateInterface $form_state, $tree = TRUE, $extended = TRUE, $multiple = FALSE, $default_fids = NULL) {
39 $form['#tree'] = (bool) $tree;
41 $form['nested']['file'] = [
42 '#type' => 'managed_file',
43 '#title' => $this->t('Managed <em>@type</em>', ['@type' => 'file & butter']),
44 '#upload_location' => 'public://test',
45 '#progress_message' => $this->t('Please wait...'),
46 '#extended' => (bool) $extended,
48 '#multiple' => (bool) $multiple,
51 $default_fids = explode(',', $default_fids);
52 $form['nested']['file']['#default_value'] = $extended ? ['fids' => $default_fids] : $default_fids;
55 $form['textfield'] = [
56 '#type' => 'textfield',
57 '#title' => $this->t('Type a value and ensure it stays'),
62 '#value' => $this->t('Save'),
71 public function submitForm(array &$form, FormStateInterface $form_state) {
73 $uploads = $form_state->getValue(['nested', 'file']);
76 $uploads = $form_state->getValue('file');
79 if ($form['nested']['file']['#extended']) {
80 $uploads = $uploads['fids'];
84 foreach ($uploads as $fid) {
88 drupal_set_message($this->t('The file ids are %fids.', ['%fids' => implode(',', $fids)]));