From a4d7940f8580450874e490d6134ceadc76b7dd33 Mon Sep 17 00:00:00 2001 From: Wesley Alves Date: Tue, 13 Aug 2024 23:09:15 -0300 Subject: [PATCH] =?UTF-8?q?feat(m=C3=B3dulo=20Simple=20Form):=20adicionar?= =?UTF-8?q?=20formul=C3=A1rio=20simples=20com=20dois=20campos=20e=20inser?= =?UTF-8?q?=C3=A7=C3=A3o=20de=20dados=20em=20tabela=20customizada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Implementa um novo módulo Drupal chamado "Simple Form". - O módulo disponibiliza um formulário simples com um campo de texto e uma lista de seleção. - Os dados submetidos pelo formulário são salvos em uma tabela customizada no banco de dados. - Exibe uma mensagem de confirmação após o envio bem-sucedido do formulário. - Inclui suporte para exibição dos valores previamente inseridos como valores padrão nos campos do formulário. --- .gitignore | 1 + config/sync/core.extension.yml | 1 + config/sync/system.site.yml | 2 +- .../custom/simple_form/simple_form.info.yml | 12 +++ .../simple_form/simple_form.links.menu.yml | 6 ++ .../custom/simple_form/simple_form.module | 38 +++++++++ .../simple_form/simple_form.routing.yml | 7 ++ .../simple_form/src/Form/SimpleForm.php | 85 +++++++++++++++++++ 8 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 web/modules/custom/simple_form/simple_form.info.yml create mode 100644 web/modules/custom/simple_form/simple_form.links.menu.yml create mode 100644 web/modules/custom/simple_form/simple_form.module create mode 100644 web/modules/custom/simple_form/simple_form.routing.yml create mode 100644 web/modules/custom/simple_form/src/Form/SimpleForm.php diff --git a/.gitignore b/.gitignore index 5c0ee76..b71fcde 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ # Ignore IDE files /.idea +.ddev \ No newline at end of file diff --git a/config/sync/core.extension.yml b/config/sync/core.extension.yml index c5a35e6..64fa694 100644 --- a/config/sync/core.extension.yml +++ b/config/sync/core.extension.yml @@ -46,6 +46,7 @@ module: path_alias: 0 search: 0 shortcut: 0 + simple_form: 0 system: 0 taxonomy: 0 text: 0 diff --git a/config/sync/system.site.yml b/config/sync/system.site.yml index ef49192..34ff6ff 100644 --- a/config/sync/system.site.yml +++ b/config/sync/system.site.yml @@ -8,7 +8,7 @@ slogan: '' page: 403: '' 404: '' - front: /node + front: /node/1 admin_compact_mode: false weight_select_max: 100 default_langcode: en diff --git a/web/modules/custom/simple_form/simple_form.info.yml b/web/modules/custom/simple_form/simple_form.info.yml new file mode 100644 index 0000000..9321cf6 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.info.yml @@ -0,0 +1,12 @@ +name: 'Practical Coding Challenges' +description: 'Provides a simple form with two fields: a text +field and a select list. Upon submission, the form should save the data to a +custom table in the database and display a confirmation message.' +package: Assessment +core_version_requirement: ^10 +type: module + +version: '1.0' +datestamp: 1630242476 +dependencies: + - drupal:field \ No newline at end of file diff --git a/web/modules/custom/simple_form/simple_form.links.menu.yml b/web/modules/custom/simple_form/simple_form.links.menu.yml new file mode 100644 index 0000000..5272a85 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.links.menu.yml @@ -0,0 +1,6 @@ +simple_form.admin_config_form: + title: 'Simple Form' + description: 'Configure the Simple Form.' + parent: system.admin_config_system + route_name: simple_form.form + weight: 100 \ No newline at end of file diff --git a/web/modules/custom/simple_form/simple_form.module b/web/modules/custom/simple_form/simple_form.module new file mode 100644 index 0000000..1aa0d56 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.module @@ -0,0 +1,38 @@ +schema()->createTable('simple_form_data', [ + 'fields' => [ + 'id' => [ + 'type' => 'serial', + 'not null' => TRUE, + ], + 'text_field' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ], + 'select_field' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ], + ], + 'primary key' => ['id'], + ]); +} + +/** + * Implements hook_uninstall(). + */ +function simple_form_uninstall() { + \Drupal::database()->schema()->dropTable('simple_form_data'); +} \ No newline at end of file diff --git a/web/modules/custom/simple_form/simple_form.routing.yml b/web/modules/custom/simple_form/simple_form.routing.yml new file mode 100644 index 0000000..8582217 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.routing.yml @@ -0,0 +1,7 @@ +simple_form.form: + path: '/admin/simple-form' + defaults: + _form: '\Drupal\simple_form\Form\SimpleForm' + _title: 'Simple Form' + requirements: + _permission: 'access content' diff --git a/web/modules/custom/simple_form/src/Form/SimpleForm.php b/web/modules/custom/simple_form/src/Form/SimpleForm.php new file mode 100644 index 0000000..7c4f714 --- /dev/null +++ b/web/modules/custom/simple_form/src/Form/SimpleForm.php @@ -0,0 +1,85 @@ +select('simple_form_data', 'sfd') + ->fields('sfd', ['text_field', 'select_field']) + ->orderBy('id', 'DESC') + ->range(0, 1); + $result = $query->execute()->fetchAssoc(); + + $default_text = isset($result['text_field']) ? $result['text_field'] : ''; + $default_select = isset($result['select_field']) ? $result['select_field'] : ''; + + $form['text_field'] = [ + '#type' => 'textfield', + '#title' => $this->t('Text Field'), + '#default_value' => $default_text, + '#required' => TRUE, + ]; + + $form['select_field'] = [ + '#type' => 'select', + '#title' => $this->t('Select Field'), + '#options' => [ + 'option_1' => $this->t('Option 1'), + 'option_2' => $this->t('Option 2'), + 'option_3' => $this->t('Option 3'), + ], + '#default_value' => $default_select, + '#required' => TRUE, + ]; + + $form['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Submit'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $text = $form_state->getValue('text_field'); + $select = $form_state->getValue('select_field'); + + $connection = Database::getConnection(); + $connection->insert('simple_form_data') + ->fields([ + 'text_field' => $text, + 'select_field' => $select, + ]) + ->execute(); + + $this->messenger()->addMessage($this->t('The form has been submitted successfully.')); + } +}