@@ -41,17 +41,17 @@ marp: true
4141
4242# Using HTMX To Make Interactive Elements In Drupal
4343
44- DrupalCamp England 2026
44+ #### DrupalCamp England 2026
4545
4646<!-- Speaker notes will appear here. -->
4747
4848---
4949
5050# Philip Norton
51- - Developer at Code Enigma
51+ - Developer at < strong > Code Enigma</ strong >
5252- Involved with Drupal for 20 years
5353- Owner of ` #! code ` (www.hashbangcode.com )
54- ![ bg h:90% right:40 %] ( ../src/assets/images/lily58.png )
54+ ![ bg h:90% right:43 %] ( ../src/assets/images/lily58.png )
5555
5656<!--
5757- Doing Drupal for 15+ years.
@@ -437,6 +437,7 @@ This means we can set the htmx-added to be transparent and then fade it in to th
437437-->
438438
439439---
440+ <!-- _footer: "" -->
440441## Configuring HTMX
441442- It is possible to configure HTMX via a number of settings.
442443
@@ -678,41 +679,69 @@ public function action() {
678679
679680## HTMX With Forms
680681
681- - The trait ` HtmxRequestInfoTrait ` is part of the ` FormBase ` class.
682- - The ` request_stack ` service is also part of the form.
682+ - Forms work in much the same way as constructors.
683+ - You need to decorate the elements inside the ` buildForm() ` method.
684+ - There is no need to create callback functions, you pull the form elements out of the response.
685+
686+ ---
687+ <!-- _footer: "" -->
688+ ## HTMX With Forms
689+
690+ Decorate the form elements using the Htmx class.
683691
692+ ``` php
693+ public function buildForm(array $form, FormStateInterface $form_state) {
694+ $form['text'] = [
695+ '#type' => 'textfield',
696+ '#title' => $this->t('Text'),
697+ ];
698+
699+ (new Htmx())
700+ ->post()
701+ ->target('*:has(>input[name="text"])')
702+ ->select('*:has(>input[name="text"])')
703+ ->trigger('keyup delay:1s')
704+ ->applyTo($form['text']);
705+
706+ return $form;
707+ }
708+ ```
684709<!--
685- - There's no need to include the request object in the form .
710+ - This decorates the form element with the HTMX elements .
686711-->
687712---
688713
689714## HTMX With Forms
690715
691- - The HTMX response from forms is the entire form, so you _ need_ to add a ` hx-select ` (or similar) to pick out the relevnat part of the response.
716+ - ` HtmxRequestInfoTrait ` is part of the ` FormBase ` class.
717+ - The ` request_stack ` service is also part of the form so there is no need to inject this.
692718
693- - Also, forms need to be consistent. You can't just throw elements into the form markup as the elements need to exist in the form build.
719+ ``` php
720+ public function buildForm(array $form, FormStateInterface $form_state) {
721+ if ($this->isHtmxRequest()) {
722+ // React to HTMX request.
723+ }
724+ // Rest of the form.
725+ }
726+ ```
694727
695728<!--
696- If the elemnts don't exist in the form build they won't be part of the submit process.
697- Also, use post requests for HTMX in forms.
729+ - There's no need to include the request object in the form.
698730-->
699-
700731---
701732
702733## HTMX With Forms
703734
704- - Forms in the same way as constructors, you just need to decorate the elements in question .
735+ - The HTMX response from forms is the entire form, so you _ need _ to add a ` hx-select ` (or similar) to pick out the relevnat part of the response .
705736
706- ``` php
707- (new Htmx())
708- ->post()
709- ->target('*:has(>input[name="email"])')
710- ->select('*:has(>input[name="email"])')
711- ->trigger('keyup delay:1s')
712- ->applyTo($form['email']);
713- ```
737+ - Forms need to be consistent. You can't just throw elements into the form markup as the elements need to exist in the form build.
714738
715- - This decorates the form element with the HTMX elements.
739+ <!--
740+ Think about what would happen if you created the form from scratch.
741+ Form intput parameters can alter the .
742+ If the elemnts don't exist in the form build they won't be part of the submit process.
743+ Also, use post requests for HTMX in forms.
744+ -->
716745
717746---
718747
0 commit comments