You are here

class PluploadTestForm in Plupload integration 8

Same name and namespace in other branches
  1. 2.0.x plupload_test/src/PluploadTestForm.php \Drupal\plupload_test\PluploadTestForm

Plupload test form class.

Hierarchy

Expanded class hierarchy of PluploadTestForm

1 string reference to 'PluploadTestForm'
plupload_test.routing.yml in plupload_test/plupload_test.routing.yml
plupload_test/plupload_test.routing.yml

File

plupload_test/src/PluploadTestForm.php, line 13

Namespace

Drupal\plupload_test
View source
class PluploadTestForm implements FormInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return '_plupload_test_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['plupload'] = [
      '#type' => 'plupload',
      '#title' => 'Plupload',
      '#upload_validators' => [
        'file_validate_extensions' => [
          'zip',
        ],
      ],
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    foreach ($form_state
      ->getValue('plupload') as $uploaded_file) {
      if ($uploaded_file['status'] != 'done') {
        $form_state
          ->setErrorByName('plupload', $this
          ->t("Upload of %filename failed.", [
          '%filename' => $uploaded_file['name'],
        ]));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Create target directory if necessary.
    $destination = \Drupal::config('system.file')
      ->get('default_scheme') . '://plupload-test';
    \Drupal::service('file_system')
      ->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    $saved_files = [];
    foreach ($form_state
      ->getValue('plupload') as $uploaded_file) {
      $file_uri = $this
        ->loadStreamWrapper()
        ->normalizeUri($destination . '/' . $uploaded_file['name']);

      // Move file without creating a new 'file' entity.
      \Drupal::service('file_system')
        ->move($uploaded_file['tmppath'], $file_uri);

      // @todo: When https://www.drupal.org/node/2245927 is resolved,
      // use a helper to save file to file_managed table
      $saved_files[] = $file_uri;
    }
    if (!empty($saved_files)) {
      \Drupal::messenger()
        ->addStatus('Files uploaded correctly: ' . implode(', ', $saved_files) . '.');
    }
  }

  /**
   * Returns the Drupal stream wrapper manager service.
   */
  private function loadStreamWrapper() {
    return \Drupal::service('stream_wrapper_manager');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluploadTestForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
PluploadTestForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PluploadTestForm::loadStreamWrapper private function Returns the Drupal stream wrapper manager service.
PluploadTestForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
PluploadTestForm::validateForm public function Form validation handler. Overrides FormInterface::validateForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.