You are here

public function WebformManagedFileBase::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformElementBase::form

1 call to WebformManagedFileBase::form()
WebformImageFile::form in src/Plugin/WebformElement/WebformImageFile.php
Gets the actual configuration webform array to be built.
1 method overrides WebformManagedFileBase::form()
WebformImageFile::form in src/Plugin/WebformElement/WebformImageFile.php
Gets the actual configuration webform array to be built.

File

src/Plugin/WebformElement/WebformManagedFileBase.php, line 1003

Class

WebformManagedFileBase
Provides a base class webform 'managed_file' elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Remove unsupported inline title display.
  unset($form['form']['display_container']['title_display']['#options']['inline']);
  $form['file'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('File settings'),
  ];

  // Warn people about temporary files when saving of results is disabled.

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $form_state
    ->getFormObject()
    ->getWebform();
  if ($webform
    ->isResultsDisabled()) {
    $temporary_maximum_age = $this->configFactory
      ->get('system.file')
      ->get('temporary_maximum_age');
    $temporary_interval = \Drupal::service('date.formatter')
      ->formatInterval($temporary_maximum_age);
    $form['file']['file_message'] = [
      '#type' => 'webform_message',
      '#message_message' => '<strong>' . $this
        ->t('Saving of results is disabled.') . '</strong> ' . $this
        ->t('Uploaded files will be temporarily stored on the server and referenced in the database for %interval.', [
        '%interval' => $temporary_interval,
      ]) . ' ' . $this
        ->t('Uploaded files should be attached to an email and/or remote posted to an external server.'),
      '#message_type' => 'warning',
      '#access' => TRUE,
    ];
  }
  $scheme_options = static::getVisibleStreamWrappers();
  $form['file']['uri_scheme'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('File upload destination'),
    '#description' => $this
      ->t('Select where the final files should be stored. Private file storage has more overhead than public files, but allows restricted access to files within this element.'),
    '#required' => TRUE,
    '#options' => $scheme_options,
  ];

  // Public files security warning.
  if (isset($scheme_options['public'])) {
    $form['file']['uri_public_warning'] = [
      '#type' => 'webform_message',
      '#message_type' => 'warning',
      '#message_message' => $this
        ->t('Public files upload destination is dangerous for webforms that are available to anonymous and/or untrusted users.') . ' ' . $this
        ->t('For more information see: <a href="https://www.drupal.org/psa-2016-003">DRUPAL-PSA-2016-003</a>'),
      '#access' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="properties[uri_scheme]"]' => [
            'value' => 'public',
          ],
        ],
      ],
    ];
  }

  // Private files not set warning.
  if (!isset($scheme_options['private'])) {
    $form['file']['uri_private_warning'] = [
      '#type' => 'webform_message',
      '#message_type' => 'warning',
      '#message_message' => $this
        ->t('Private file system is not set. This must be changed in <a href="https://www.drupal.org/documentation/modules/file">settings.php</a>. For more information see: <a href="https://www.drupal.org/psa-2016-003">DRUPAL-PSA-2016-003</a>'),
      '#access' => TRUE,
    ];
  }
  $max_filesize = \Drupal::config('webform.settings')
    ->get('file.default_max_filesize') ?: Environment::getUploadMaxSize();
  $max_filesize = Bytes::toInt($max_filesize);
  $max_filesize = $max_filesize / 1024 / 1024;
  $form['file']['file_help'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('File upload help display'),
    '#description' => $this
      ->t('Determines the placement of the file upload help .'),
    '#options' => [
      '' => $this
        ->t('Description'),
      'help' => $this
        ->t('Help'),
      'more' => $this
        ->t('More'),
      'none' => $this
        ->t('None'),
    ],
  ];
  $form['file']['file_placeholder'] = [
    '#type' => 'webform_html_editor',
    '#title' => $this
      ->t('File upload placeholder'),
    '#description' => $this
      ->t('The placeholder will be shown before a file is uploaded.'),
  ];
  $form['file']['file_preview'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('File upload preview (Authenticated users only)'),
    '#description' => $this
      ->t('Select how the uploaded file previewed.') . '<br/><br/>' . $this
      ->t('Allowing anonymous users to preview files is dangerous.') . '<br/>' . $this
      ->t('For more information see: <a href="https://www.drupal.org/psa-2016-003">DRUPAL-PSA-2016-003</a>'),
    '#options' => WebformOptionsHelper::appendValueToText($this
      ->getItemFormats()),
    '#empty_option' => '<' . $this
      ->t('no preview') . '>',
  ];
  $form['file']['max_filesize'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum file size'),
    '#field_suffix' => $this
      ->t('MB (Max: @filesize MB)', [
      '@filesize' => $max_filesize,
    ]),
    '#placeholder' => $max_filesize,
    '#description' => $this
      ->t('Enter the max file size a user may upload.'),
    '#min' => 1,
    '#max' => $max_filesize,
    '#step' => 'any',
  ];
  $form['file']['file_extensions'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Allowed file extensions'),
    '#description' => $this
      ->t('Separate extensions with a space or comma and do not include the leading dot.') . '<br/><br/>' . $this
      ->t('Defaults to: %value', [
      '%value' => $this
        ->getDefaultFileExtensions(),
    ]),
    '#maxlength' => 255,
  ];
  $form['file']['file_name'] = [
    '#type' => 'webform_checkbox_value',
    '#title' => $this
      ->t('Rename files'),
    '#description' => $this
      ->t('Rename uploaded files to this tokenized pattern. Do not include the extension here. The actual file extension will be automatically appended to this pattern.'),
    '#element' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('File name pattern'),
      '#description' => $this
        ->t('File names combined with their full URI can not exceed 255 characters. File names that exceed this limit will be truncated.'),
      '#maxlength' => NULL,
    ],
  ];
  $form['file']['sanitize'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Sanitize file name'),
    '#description' => $this
      ->t('If checked, file name will be transliterated, lower-cased and all special characters converted to dashes (-).'),
    '#return_value' => TRUE,
  ];
  $t_args = [
    '%file_rename' => $form['file']['file_name']['#title'],
    '%sanitization' => $form['file']['sanitize']['#title'],
  ];
  $form['file']['file_name_warning'] = [
    '#type' => 'webform_message',
    '#message_type' => 'warning',
    '#message_message' => $this
      ->t('For security reasons we advise to use %file_rename together with the %sanitization option.', $t_args),
    '#access' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="properties[file_name][checkbox]"]' => [
          'checked' => TRUE,
        ],
        ':input[name="properties[sanitize]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['file']['multiple'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Multiple'),
    '#description' => $this
      ->t('Check this option if the user should be allowed to upload multiple files.'),
    '#return_value' => TRUE,
  ];

  // Button.
  // @see webform_preprocess_file_managed_file()
  $form['file']['button'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Replace file upload input with an upload button'),
    '#description' => $this
      ->t('If checked the file upload input will be replaced with click-able label styled as button.'),
    '#return_value' => TRUE,
  ];
  $form['file']['button__title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('File upload button title'),
    '#description' => $this
      ->t('Defaults to: %value', [
      '%value' => $this
        ->t('Choose file'),
    ]),
    '#states' => [
      'visible' => [
        ':input[name="properties[button]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['file']['button__attributes'] = [
    '#type' => 'webform_element_attributes',
    '#title' => $this
      ->t('File upload button'),
    '#classes' => $this->configFactory
      ->get('webform.settings')
      ->get('settings.button_classes'),
    '#class__description' => $this
      ->t("Apply classes to the button. Button classes default to 'button button-primary'."),
    '#states' => [
      'visible' => [
        ':input[name="properties[button]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Hide default value, which is not applicable for file uploads.
  $form['default']['#access'] = FALSE;
  return $form;
}