Skip to content

Compare: Contact form integration

42 changes: 25 additions & 17 deletions Contact form integration.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This tutorial explains how you can pass the product configuration to a contact form when the "request product" button is clicked.
This tutorial explains how you can pass the product or plan configuration to a contact form when the "request product"/"request plan" button is clicked.

For this tutorial we will use the [Uniform plugin](https://github.com/mzur/kirby-uniform) by Martin Zurowietz. If you already use a different plugin or prefer to use Kirby's core methods, you can adapt the code accordingly. If you want to follow the example, please first install the Uniform plugin as [explained in its docs](https://kirby-uniform.readthedocs.io/).

Expand Down Expand Up @@ -35,8 +35,9 @@ return function ($kirby) {
htmlspecialchars_decode($form->old('roomle-configuration'))
);

// turn the raw configuration string into a nice object structure
$configuration = $configurationData ? roomleConfiguration($configurationData) : null;
// turn the raw configuration string into a nice object structure;
// the plugin automatically handles both plan and product configurations
$plan = $configurationData ? roomlePlan($configurationData) : null;

// only submit the form if the `submit` value is set;
// just listening for POST requests won't work because the
Expand All @@ -52,18 +53,18 @@ return function ($kirby) {
]);
}

return compact('configuration', 'form');
return compact('form', 'plan');
};
```

## 2. Display the form and configuration in the template

The controller passes two variables to our form template, `$configuration` with all the Roomle configuration data and `$form` from Uniform.
The controller passes two variables to our form template, `$plan` with all the Roomle configuration data and `$form` from Uniform.

We can now use these variables in `site/templates/form.php` to display both the contact form and the product configuration:

```php
<?php if ($configuration && $url = $configuration->configuratorUrl()): ?>
<?php if ($plan && $url = $plan->configuratorUrl()): ?>
<a href="<?= esc($url, 'attr') ?>">Back to the configurator</a>
<?php endif ?>

Expand All @@ -77,8 +78,8 @@ We can now use these variables in `site/templates/form.php` to display both the
<?= csrf_field() ?>
<?= honeypot_field() ?>

<?php if ($configuration): ?>
<input name="roomle-configuration" type="hidden" value="<?= esc($configuration->toJson(), 'attr') ?>">
<?php if ($plan): ?>
<input name="roomle-configuration" type="hidden" value="<?= esc($plan->toJson(), 'attr') ?>">
<?php endif ?>

<input type="submit" name="submit" value="Submit">
Expand All @@ -90,12 +91,18 @@ Success!
<?php snippet('uniform/errors', ['form' => $form]) ?>
<?php endif ?>

<?php if ($configuration): ?>
<?php if ($plan): ?>
<h2>Configuration data</h2>
<h3><?= esc($configuration->label()) ?></h3>
<p><?= esc($configuration->size()) ?></p>

<?= $configuration->perspectiveImage()->html(['class' => 'configuration-image']) ?>
<?php if ($plan->hasId() === true): ?>
<?= $plan->thumbnail()->html(['class' => 'configuration-image']) ?>
<?php endif ?>

<?php foreach ($plan->items() as $item): ?>
<h3><?= esc($item->label()) ?></h3>
<p><?= esc($item->size()) ?></p>

<?= $item->perspectiveImage()->html(['class' => 'configuration-image']) ?>

<table>
<tr>
Expand All @@ -104,7 +111,7 @@ Success!
<th>Name</th>
</tr>

<?php foreach ($configuration->parts() as $part): ?>
<?php foreach ($item->parts() as $part): ?>
<tr>
<td><?= esc($part->count()) ?></td>
<td><?= esc($part->articleNr()) ?></td>
Expand All @@ -120,6 +127,7 @@ Success!
</tr>
<?php endforeach ?>
</table>
<?php endforeach ?>
<?php endif ?>

<style>
Expand All @@ -143,7 +151,7 @@ For a more user-friendly output, we need a custom email template in `site/templa
-------------------
Configuration data:

<?= roomleConfiguration($configuration) ?>
<?= roomlePlan($configuration) ?>
<?php endif ?>
```

Expand All @@ -153,7 +161,7 @@ This email template is already referenced in the controller from step 1.

## 4. Point the plugin to the contact form

Now you need to tell the Roomle plugin that it should submit the configuration data to your contact form whenever the "request product" button is clicked.
Now you need to tell the Roomle plugin that it should submit the configuration data to your contact form whenever the "request product"/"request plan" button is clicked.

You can either do this by selecting the contact form page in the block settings or by setting a global default in `site/config/config.php`:

Expand All @@ -171,11 +179,11 @@ return [

### More fields in the page template

The `$configuration` object and its children also have more fields and methods you can use for output on the form page (e.g. the part price). To see what is possible, please take a look at the [plugin classes](https://github.com/lukasbestle/kirby-roomle/tree/main/src/classes), particularly `Configuration`, `Parameter` and `Part`.
The `$plan` object and its children also have more fields and methods you can use for output on the form page (e.g. the part price). To see what is possible, please take a look at the [plugin classes](https://github.com/lukasbestle/kirby-roomle/tree/main/src/classes), particularly `Configuration`, `Parameter`, `Part` and `Plan`.

### Custom configuration rendering in the email

The email template from step 3 prints the whole `$configuration` object. Internally, this will render three snippets: `roomle/configuration.php`, `roomle/part.php` and `roomle/parameter.php`.
The email template from step 3 prints the whole `$plan` object. Internally, this will render four snippets: `roomle/plan.php`, `roomle/configuration.php`, `roomle/part.php` and `roomle/parameter.php`.

If you want to customize the rendering in the email, you have two options:

Expand Down