Verified Commit 7406530a authored by quietone's avatar quietone
Browse files

Issue #3368857 by ultimike, nico059, mattlc, akulsaxena, anand48, jacobupal,...

Issue #3368857 by ultimike, nico059, mattlc, akulsaxena, anand48, jacobupal, smustgrave, kim.pepper, ankitv18, drubolbol: Remove duplication from FileTestForm and FileTestSaveUploadFromForm

(cherry picked from commit dd5c7b57)
parent 4a28b967
Loading
Loading
Loading
Loading
Loading
+4 −43
Original line number Diff line number Diff line
@@ -8,12 +8,12 @@
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * File test form class.
 */
class FileTestForm implements FormInterface {
  use FileTestFormTrait;

  /**
   * {@inheritdoc}
@@ -26,53 +26,14 @@ public function getFormId() {
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form = $this->baseForm($form, $form_state);

    $form['file_test_upload'] = [
      '#type' => 'file',
      '#title' => t('Upload a file'),
    ];
    $form['file_test_replace'] = [
      '#type' => 'select',
      '#title' => t('Replace existing image'),
      '#options' => [
        FileExists::Rename->name => new TranslatableMarkup('Appends number until name is unique'),
        FileExists::Replace->name => new TranslatableMarkup('Replace the existing file'),
        FileExists::Error->name => new TranslatableMarkup('Fail with an error'),
      ],
      '#default_value' => FileExists::Rename->name,
    ];
    $form['file_subdir'] = [
      '#type' => 'textfield',
      '#title' => t('Subdirectory for test file'),
      '#default_value' => '',
    ];

    $form['extensions'] = [
      '#type' => 'textfield',
      '#title' => t('Allowed extensions.'),
      '#default_value' => '',
    ];

    $form['allow_all_extensions'] = [
      '#title' => t('Allow all extensions?'),
      '#type' => 'radios',
      '#options' => [
        'false' => 'No',
        'empty_array' => 'Empty array',
        'empty_string' => 'Empty string',
      ],
      '#default_value' => 'false',
    ];

    $form['is_image_file'] = [
      '#type' => 'checkbox',
      '#title' => t('Is this an image file?'),
      '#default_value' => TRUE,
    ];

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => t('Submit'),
    ];
    return $form;
  }

+76 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\file_test\Form;

use Drupal\Core\File\FileExists;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * This trait provides common code common for Forms.
 */
trait FileTestFormTrait {

  /**
   * Adds common form elements to the form.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The form structure.
   */
  public function baseForm(array $form, FormStateInterface $form_state): array {

    $form['file_test_replace'] = [
      '#type' => 'select',
      '#title' => t('Replace existing image'),
      '#options' => [
        FileExists::Rename->name => new TranslatableMarkup('Appends number until name is unique'),
        FileExists::Replace->name => new TranslatableMarkup('Replace the existing file'),
        FileExists::Error->name => new TranslatableMarkup('Fail with an error'),
      ],
      '#default_value' => FileExists::Rename->name,
    ];
    $form['file_subdir'] = [
      '#type' => 'textfield',
      '#title' => t('Subdirectory for test file'),
      '#default_value' => '',
    ];

    $form['extensions'] = [
      '#type' => 'textfield',
      '#title' => t('Allowed extensions.'),
      '#default_value' => '',
    ];

    $form['allow_all_extensions'] = [
      '#title' => t('Allow all extensions?'),
      '#type' => 'radios',
      '#options' => [
        'false' => 'No',
        'empty_array' => 'Empty array',
        'empty_string' => 'Empty string',
      ],
      '#default_value' => 'false',
    ];

    $form['is_image_file'] = [
      '#type' => 'checkbox',
      '#title' => t('Is this an image file?'),
      '#default_value' => TRUE,
    ];

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => t('Submit'),
    ];

    return $form;
  }

}
+4 −43
Original line number Diff line number Diff line
@@ -10,13 +10,13 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * File test form class.
 */
class FileTestSaveUploadFromForm extends FormBase {
  use FileTestFormTrait;

  /**
   * Stores the state storage service.
@@ -66,49 +66,14 @@ public function getFormId() {
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form = $this->baseForm($form, $form_state);

    $form['file_test_upload'] = [
      '#type' => 'file',
      '#multiple' => TRUE,
      '#title' => $this->t('Upload a file'),
    ];
    $form['file_test_replace'] = [
      '#type' => 'select',
      '#title' => $this->t('Replace existing image'),
      '#options' => [
        FileExists::Rename->name => new TranslatableMarkup('Appends number until name is unique'),
        FileExists::Replace->name => new TranslatableMarkup('Replace the existing file'),
        FileExists::Error->name => new TranslatableMarkup('Fail with an error'),
      ],
      '#default_value' => FileExists::Rename->name,
    ];
    $form['file_subdir'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Subdirectory for test file'),
      '#default_value' => '',
    ];

    $form['extensions'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Allowed extensions.'),
      '#default_value' => '',
    ];

    $form['allow_all_extensions'] = [
      '#title' => t('Allow all extensions?'),
      '#type' => 'radios',
      '#options' => [
        'false' => 'No',
        'empty_array' => 'Empty array',
        'empty_string' => 'Empty string',
      ],
      '#default_value' => 'false',
    ];

    $form['is_image_file'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Is this an image file?'),
      '#default_value' => TRUE,
    ];

    $form['error_message'] = [
      '#type' => 'textfield',
@@ -116,10 +81,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#default_value' => '',
    ];

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