_file_save_upload_from_form example

$form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Import'),
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $this->file = _file_save_upload_from_form($form['file']$form_state, 0);

    // Ensure we have the file uploaded.     if (!$this->file) {
      $form_state->setErrorByName('file', $this->t('File to import not found.'));
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    
// The test for \Drupal::service('file_system')->moveUploadedFile()     // triggering a warning is unavoidable. We're interested in what happens     // afterwards in _file_save_upload_from_form().     if ($this->state->get('file_test.disable_error_collection')) {
      define('SIMPLETEST_COLLECT_ERRORS', FALSE);
    }

    $form['file_test_upload']['#upload_validators'] = $validators;
    $form['file_test_upload']['#upload_location'] = $destination;

    $this->messenger->addStatus($this->t('Number of error messages before _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));
    $file = _file_save_upload_from_form($form['file_test_upload']$form_state, 0, $form_state->getValue('file_test_replace'));
    $this->messenger->addStatus($this->t('Number of error messages after _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));

    if ($file) {
      $form_state->setValue('file_test_upload', $file);
      $this->messenger->addStatus($this->t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()]));
      $this->messenger->addStatus($this->t('File name is @filename.', ['@filename' => $file->getFilename()]));
      $this->messenger->addStatus($this->t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()]));
      $this->messenger->addStatus($this->t('You WIN!'));
    }
    elseif ($file === FALSE) {
      $this->messenger->addError($this->t('Epic upload FAIL!'));
    }
/** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    if ($this->moduleHandler->moduleExists('file')) {

      // Check for a new uploaded logo.       if (isset($form['logo'])) {
        $file = _file_save_upload_from_form($form['logo']['settings']['logo_upload']$form_state, 0);
        if ($file) {
          // Put the temporary file in form_values so we can save it on submit.           $form_state->setValue('logo_upload', $file);
        }
      }

      // Check for a new uploaded favicon.       if (isset($form['favicon'])) {
        $file = _file_save_upload_from_form($form['favicon']['settings']['favicon_upload']$form_state, 0);
        if ($file) {
          // Put the temporary file in form_values so we can save it on submit.
Home | Imprint | This part of the site doesn't use cookies.