Uniform plugin - Add current page title and url to email body?

I have added a feedback form to article pages:

In template:

<?php if ($form->success()): ?>
    <div class="uk-alert-success" uk-alert>
        <a class="uk-alert-close" uk-close></a>
        <p>Thank you for your message.</p>
    </div>
<?php elseif ($form->error()): ?>
    <div class="uk-alert-danger" uk-alert>
    <a class="uk-alert-close" uk-close></a>
    <?php snippet('uniform/errors', ['form' => $form]) ?>
    </div>
<?php endif; ?>

<div class="reply uk-margin-medium-top border-top padding-top">
    <h3 class="uk-margin-medium-bottom">Feedback</h3>
    <form class="uk-grid-small" action="<?= $page->url() ?>" method="POST" uk-grid>
        <div class="uk-width-1-1">
            <textarea<?php if ($form->error('message')): ?> class="uk-textarea error"<?php endif; ?> name="message" class="uk-textarea" rows="5" placeholder="Feedback"><?= $form->old('message') ?></textarea>
        </div>
        <?= csrf_field() ?>
        <?= honeypot_field() ?>
        <div class="uk-width-1-1">
            <button class="uk-button uk-button-primary uk-width-1-1 uk-width-auto@s" type="submit" value="Submit">Submit</button>
        </div>
    </form>
</div>

In controller:

 <?php

use Uniform\Form;

return function($site, $pages, $page) {
  $form = new Form([
      'message' => [
          'rules' => ['required'],
          'message' => 'Please enter a message',
      ],
  ]);

  if (r::is('POST')) {
      $form->emailAction([
          'to' => 'macgyver@example.com',
          'from' => 'feedback@domain.ltd',
          'subject' => 'New feedback',
      ]);
  }


  // get an array of the tags of the current page
  $tags = $page->tags()->split(',');

  // filter the pages with a filter with callback
  $relatedPages = $page->siblings(false)->visible()->filter(function($related) use($tags) {
    if(array_intersect($related->tags()->split(','), $tags)) {
      return $related;
    }
  });

  // fetch list of kits
  $kits = $page->assemblies()->toStructure();
  

  return compact('form', 'relatedPages', 'kits');

};

I need to know which page the feedback came from. How can I add the page title and url to the email body?

Thanks

You can use the snippet option to define a snippet for the email body. See the uniform repo for examples.

Edit: Please use the question category for future questions, makes it easier to keep track of unsolved questions. Thanks.

1 Like

Sorry, I will add a category to future questions.

I need a little more help with this. I have added a snippet called uniformemail.php containing:

<?php
foreach ($data as $field => $value) {
	if (is_array($value)) {
		$value = implode(', ', array_filter($value, function ($i) {
			return $i !== '';
		}));
	}
	echo ucfirst($field).': '.$value."\n";
}

And in the controller:

use Uniform\Form;

return function($site, $pages, $page) {
  $form = new Form([
      'message' => [
          'rules' => ['required'],
          'message' => 'Please enter a message',
      ],
  ]);

  if (r::is('POST')) {
      $form->emailAction([
          'to' => 'macgyver@example.com',
          'from' => 'feedback@domain.ltd',
          'subject' => 'New feedback from page: ' . $page->title()->html(),
          'snippet' => 'uniformemail',
      ]);
  }

I was able to add a dynamic page title to the subject but I have no idea how to add a link to the page in the email body (along with the submitted feedback). Can you please provide a little more guidance?

Thanks

Hm, looks like the only data that is available to the snippet is $data and $options. Maybe the easiest way would be to pass the page title and url via a hidden field. Then it will be contained in the $data array that is available to the snippet. Don’t know if it is possible to add any non-documented options to the email action.

You have several options:

  • Use the page() helper directly in the snippet.
  • Add the data to the options array (yes you can add anything and it will be available in $options in the snippet).
  • Add the data as hidden form fields. This way the data will be automatically added to the email and you are able to add it with templates to the subject but the users are able to change it if they edit the HTML which you probably don’t want.
1 Like