diff --git a/core/modules/file/tests/file_test/src/Form/FileTestForm.php b/core/modules/file/tests/file_test/src/Form/FileTestForm.php index f77559f9a44cd1a8b3cf26c60df0f185f5f5373c..f7164474bebc6f5ba9de4fd83c8b48a9a5b11c9c 100644 --- a/core/modules/file/tests/file_test/src/Form/FileTestForm.php +++ b/core/modules/file/tests/file_test/src/Form/FileTestForm.php @@ -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; } diff --git a/core/modules/file/tests/file_test/src/Form/FileTestFormTrait.php b/core/modules/file/tests/file_test/src/Form/FileTestFormTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..ccb1ed4dddb5a22b22e18018937fd3e39c0f611b --- /dev/null +++ b/core/modules/file/tests/file_test/src/Form/FileTestFormTrait.php @@ -0,0 +1,76 @@ +<?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; + } + +} diff --git a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php index 7f87c60dd9e8ca6df3b4e0a90effab4613b0b065..7ab8c4b53625a5d86e09821b262faf9bea7a1ad0 100644 --- a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php +++ b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php @@ -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; }