From 6cdb6a56bcfb529b39e5e353d3b0d6abb16ad484 Mon Sep 17 00:00:00 2001 From: Fabio Mateus Date: Sun, 11 Aug 2024 16:54:45 -0300 Subject: [PATCH] feat: implement Zoocha Simple Form module --- .../src/Form/ZoochaSimpleForm.php | 145 ++++++++++++++++++ .../zoocha_simple_form.info.yml | 7 + .../zoocha_simple_form.install | 34 ++++ .../zoocha_simple_form.permissions.yml | 4 + .../zoocha_simple_form.routing.yml | 8 + 5 files changed, 198 insertions(+) create mode 100644 web/modules/custom/zoocha_simple_form/src/Form/ZoochaSimpleForm.php create mode 100644 web/modules/custom/zoocha_simple_form/zoocha_simple_form.info.yml create mode 100644 web/modules/custom/zoocha_simple_form/zoocha_simple_form.install create mode 100644 web/modules/custom/zoocha_simple_form/zoocha_simple_form.permissions.yml create mode 100644 web/modules/custom/zoocha_simple_form/zoocha_simple_form.routing.yml diff --git a/web/modules/custom/zoocha_simple_form/src/Form/ZoochaSimpleForm.php b/web/modules/custom/zoocha_simple_form/src/Form/ZoochaSimpleForm.php new file mode 100644 index 0000000..2cc2405 --- /dev/null +++ b/web/modules/custom/zoocha_simple_form/src/Form/ZoochaSimpleForm.php @@ -0,0 +1,145 @@ +database = $database; + $this->logger = $logger; + } + + /** + * {@inheritdoc} + * + * Creates an instance of the form using dependency injection. + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('database'), + $container->get('logger.factory')->get('zoocha_simple_form') + ); + } + + /** + * {@inheritdoc} + * + * Returns a unique string identifying the form. + */ + public function getFormId() { + return 'zoocha_simple_form'; + } + + /** + * {@inheritdoc} + * + * Builds the form elements. + * + * @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 buildForm(array $form, FormStateInterface $form_state) { + // Add a text field to the form. + $form['text_field'] = [ + '#type' => 'textfield', + '#title' => $this->t('Text Field'), // Title of the text field. + '#required' => TRUE, // Marks the field as required. + '#maxlength' => 255, // Limit the maximum length for security. + ]; + + // Add a select list to the form. + $form['select_field'] = [ + '#type' => 'select', + '#title' => $this->t('Select Field'), // Title of the select field. + '#options' => [ + 'option_1' => $this->t('Option 1'), + 'option_2' => $this->t('Option 2'), + ], + '#required' => TRUE, // Marks the field as required. + ]; + + // Add a submit button to the form. + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Submit'), // Text on the submit button. + ]; + + return $form; // Returns the form array. + } + + /** + * {@inheritdoc} + * + * Handles form submission and stores the submitted data in the database. + * + * @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. + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // Retrieve and sanitize values from the form submission. + $text = Html::escape($form_state->getValue('text_field')); + $select = Html::escape($form_state->getValue('select_field')); + + // Attempt to insert the submitted data into the custom database table. + try { + // Use Drupal's Database API to perform a safe database insert. + $this->database->insert('zoocha_simple_form_data') + ->fields([ + 'text_field' => $text, + 'select_field' => $select, + ]) + ->execute(); + + // Display a success message to the user. + $this->messenger()->addMessage($this->t('Form submitted successfully.')); + } + catch (\Exception $e) { + // Log the exception with a descriptive message. + $this->logger->error('Database error: @message', ['@message' => $e->getMessage()]); + // Display a generic error message to the user. + $this->messenger()->addError($this->t('An error occurred while submitting the form. Please try again later.')); + } + } + +} diff --git a/web/modules/custom/zoocha_simple_form/zoocha_simple_form.info.yml b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.info.yml new file mode 100644 index 0000000..8481f9b --- /dev/null +++ b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.info.yml @@ -0,0 +1,7 @@ +name: 'Zoocha Simple Form' +type: module +description: 'Provides a simple form with text and select fields.' +core_version_requirement: ^10 +package: Custom +dependencies: + - drupal:system diff --git a/web/modules/custom/zoocha_simple_form/zoocha_simple_form.install b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.install new file mode 100644 index 0000000..6903bd2 --- /dev/null +++ b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.install @@ -0,0 +1,34 @@ + [ + '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'], + ]; + return $schema; +} diff --git a/web/modules/custom/zoocha_simple_form/zoocha_simple_form.permissions.yml b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.permissions.yml new file mode 100644 index 0000000..011b7df --- /dev/null +++ b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.permissions.yml @@ -0,0 +1,4 @@ +# Permission for accessing the Zoocha Simple Form. +access zoocha simple form: + title: 'Access Zoocha Simple Form' + description: 'Permission to access the Zoocha simple form.' diff --git a/web/modules/custom/zoocha_simple_form/zoocha_simple_form.routing.yml b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.routing.yml new file mode 100644 index 0000000..8dbec33 --- /dev/null +++ b/web/modules/custom/zoocha_simple_form/zoocha_simple_form.routing.yml @@ -0,0 +1,8 @@ +# Route definition for accessing the Zoocha Simple Form. +zoocha_simple_form.form: + path: '/zoocha-simple-form' # URL path to access the form. + defaults: + _form: '\Drupal\zoocha_simple_form\Form\ZoochaSimpleForm' # The form class. + _title: 'Zoocha Simple Form' # Title of the page displaying the form. + requirements: + _permission: 'access zoocha simple form' # Permission required to access the form.