Loading core/modules/file/tests/file_test/src/Form/FileTestForm.php +4 −43 Original line number Diff line number Diff line Loading @@ -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} Loading @@ -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; } Loading core/modules/file/tests/file_test/src/Form/FileTestFormTrait.php 0 → 100644 +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; } } core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php +4 −43 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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', Loading @@ -116,10 +81,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => '', ]; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Submit'), ]; return $form; } Loading Loading
core/modules/file/tests/file_test/src/Form/FileTestForm.php +4 −43 Original line number Diff line number Diff line Loading @@ -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} Loading @@ -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; } Loading
core/modules/file/tests/file_test/src/Form/FileTestFormTrait.php 0 → 100644 +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; } }
core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php +4 −43 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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', Loading @@ -116,10 +81,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => '', ]; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Submit'), ]; return $form; } Loading