From a4d7940f8580450874e490d6134ceadc76b7dd33 Mon Sep 17 00:00:00 2001 From: Wesley Alves Date: Tue, 13 Aug 2024 23:09:15 -0300 Subject: [PATCH 1/3] =?UTF-8?q?feat(m=C3=B3dulo=20Simple=20Form):=20adicio?= =?UTF-8?q?nar=20formul=C3=A1rio=20simples=20com=20dois=20campos=20e=20ins?= =?UTF-8?q?er=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.')); + } +} From ceed6f30be77f931c259009e325fd3b95ffee78e Mon Sep 17 00:00:00 2001 From: Wesley Alves Date: Wed, 14 Aug 2024 05:34:02 -0300 Subject: [PATCH 2/3] =?UTF-8?q?fix(media):=20corrigir=20bug=20no=20campo?= =?UTF-8?q?=20de=20v=C3=ADdeo=20ao=20salvar=20quando=20o=20node=20atual=20?= =?UTF-8?q?est=C3=A1=20em=20`draft`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Adiciona o media type ao workflow, garantindo que o campo `moderation_state` seja corretamente incluido no media type. - Implementa lógica para verificar se o node sendo editado está em rascunho (`draft`), e se o estado atual do media é diferente de `draft`, realiza as devidas validações. - Corrige possíveis erros e quebras no código ao salvar a entity, garantindo que os novos valores sejam setados corretamente e retornem os callbacks apropriados. - Inclui logs e mensagens de erro para facilitar o rastreamento de problemas durante o processo de salvamento da entity. --- config/sync/core.extension.yml | 1 + config/sync/system.site.yml | 2 +- config/sync/user.role.anonymous.yml | 3 ++ config/sync/user.role.authenticated.yml | 3 ++ config/sync/workflows.workflow.editorial.yml | 5 ++ ...ite.info.yml => drupal_site_core.info.yml} | 2 +- .../drupal_site_core/drupal_site_core.module | 51 +++++++++++++++++-- 7 files changed, 61 insertions(+), 6 deletions(-) rename web/modules/custom/drupal_site_core/{drupal_site.info.yml => drupal_site_core.info.yml} (88%) diff --git a/config/sync/core.extension.yml b/config/sync/core.extension.yml index 64fa694..df50331 100644 --- a/config/sync/core.extension.yml +++ b/config/sync/core.extension.yml @@ -18,6 +18,7 @@ module: contextual: 0 datetime: 0 dblog: 0 + drupal_site_core: 0 dynamic_page_cache: 0 editor: 0 entity_browser: 0 diff --git a/config/sync/system.site.yml b/config/sync/system.site.yml index 34ff6ff..ef49192 100644 --- a/config/sync/system.site.yml +++ b/config/sync/system.site.yml @@ -8,7 +8,7 @@ slogan: '' page: 403: '' 404: '' - front: /node/1 + front: /node admin_compact_mode: false weight_select_max: 100 default_langcode: en diff --git a/config/sync/user.role.anonymous.yml b/config/sync/user.role.anonymous.yml index b62cc7d..88e9b27 100644 --- a/config/sync/user.role.anonymous.yml +++ b/config/sync/user.role.anonymous.yml @@ -7,6 +7,7 @@ dependencies: module: - comment - contact + - content_moderation - filter - media - search @@ -23,4 +24,6 @@ permissions: - 'access site-wide contact form' - 'search content' - 'use text format restricted_html' + - 'view any unpublished content' + - 'view latest version' - 'view media' diff --git a/config/sync/user.role.authenticated.yml b/config/sync/user.role.authenticated.yml index ad9009b..3cbd2ee 100644 --- a/config/sync/user.role.authenticated.yml +++ b/config/sync/user.role.authenticated.yml @@ -7,6 +7,7 @@ dependencies: module: - comment - contact + - content_moderation - file - filter - media @@ -29,4 +30,6 @@ permissions: - 'search content' - 'skip comment approval' - 'use text format basic_html' + - 'view any unpublished content' + - 'view latest version' - 'view media' diff --git a/config/sync/workflows.workflow.editorial.yml b/config/sync/workflows.workflow.editorial.yml index 8e7f00e..c761e95 100644 --- a/config/sync/workflows.workflow.editorial.yml +++ b/config/sync/workflows.workflow.editorial.yml @@ -3,6 +3,8 @@ langcode: en status: true dependencies: config: + - media.type.remote_video + - media.type.video - node.type.landing_page module: - content_moderation @@ -62,6 +64,9 @@ type_settings: to: published weight: 1 entity_types: + media: + - remote_video + - video node: - landing_page default_moderation_state: draft diff --git a/web/modules/custom/drupal_site_core/drupal_site.info.yml b/web/modules/custom/drupal_site_core/drupal_site_core.info.yml similarity index 88% rename from web/modules/custom/drupal_site_core/drupal_site.info.yml rename to web/modules/custom/drupal_site_core/drupal_site_core.info.yml index eb262c0..3f67a62 100644 --- a/web/modules/custom/drupal_site_core/drupal_site.info.yml +++ b/web/modules/custom/drupal_site_core/drupal_site_core.info.yml @@ -2,4 +2,4 @@ name: 'Custom core' type: module description: "Base module (default config, general tasks/updates, etc.)." core_version_requirement: '^9.4 || ^10' -package: Custom +package: Assessment diff --git a/web/modules/custom/drupal_site_core/drupal_site_core.module b/web/modules/custom/drupal_site_core/drupal_site_core.module index 3c78d09..afeaa3f 100644 --- a/web/modules/custom/drupal_site_core/drupal_site_core.module +++ b/web/modules/custom/drupal_site_core/drupal_site_core.module @@ -1,6 +1,49 @@ getEntityTypeId() == 'media' && $entity->bundle() == 'remote_video') { + $node = \Drupal::routeMatch()->getParameter('node'); + + if ($node && !$node->isDefaultRevision()) { + + if ($entity->hasField('moderation_state') && $entity->get('moderation_state')->value !== 'draft') { + + $violations = $entity->validate(); + if (count($violations) > 0) { + foreach ($violations as $violation) { + \Drupal::messenger()->addError($violation->getMessage()); + } + return; + } + + if (!\Drupal::currentUser()->hasPermission('edit media')) { + \Drupal::messenger()->addError('You do not have permission to edit this media entity.'); + return; + } + + $entity->set('moderation_state', 'draft'); + + $entity->setNewRevision(TRUE); + + $entity->setRevisionUserId(\Drupal::currentUser()->id()); + + $entity->setRevisionLogMessage('Set moderation state to draft programmatically.'); + + try { + if ($entity->save()) { + \Drupal::messenger()->addMessage('Set moderation state to draft programmatically.'); + } else { + \Drupal::messenger()->addError('Entity save returned false.'); + } + } catch (\Exception $e) { + \Drupal::messenger()->addError('An error occurred during entity save: ' . $e->getMessage()); + } + } + } + } +} \ No newline at end of file From 2c8ee6ea5a80ada3039f332cfa7be725a6b15a9a Mon Sep 17 00:00:00 2001 From: Wesley Alves Date: Thu, 15 Aug 2024 08:16:04 -0300 Subject: [PATCH 3/3] Infinite loop fix --- .../custom/drupal_site_core/drupal_site_core.module | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/web/modules/custom/drupal_site_core/drupal_site_core.module b/web/modules/custom/drupal_site_core/drupal_site_core.module index afeaa3f..bdefb85 100644 --- a/web/modules/custom/drupal_site_core/drupal_site_core.module +++ b/web/modules/custom/drupal_site_core/drupal_site_core.module @@ -6,13 +6,17 @@ // */ function drupal_site_core_entity_update(Drupal\Core\Entity\EntityInterface $entity) { + static $is_updating = FALSE; + + if ($is_updating) { + return; + } + if ($entity->getEntityTypeId() == 'media' && $entity->bundle() == 'remote_video') { $node = \Drupal::routeMatch()->getParameter('node'); if ($node && !$node->isDefaultRevision()) { - if ($entity->hasField('moderation_state') && $entity->get('moderation_state')->value !== 'draft') { - $violations = $entity->validate(); if (count($violations) > 0) { foreach ($violations as $violation) { @@ -26,6 +30,8 @@ function drupal_site_core_entity_update(Drupal\Core\Entity\EntityInterface $enti return; } + $is_updating = TRUE; + $entity->set('moderation_state', 'draft'); $entity->setNewRevision(TRUE); @@ -43,6 +49,8 @@ function drupal_site_core_entity_update(Drupal\Core\Entity\EntityInterface $enti } catch (\Exception $e) { \Drupal::messenger()->addError('An error occurred during entity save: ' . $e->getMessage()); } + + $is_updating = FALSE; } } }