eZ Community » Learn » eZ Publish » Create a feedback form in eZ Publish...

Create a feedback form in eZ Publish 5 using ezforms-bundle

Monday 12 May 2014 3:33:49 pm

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Send feedback data by email

Now that we're able to submit data and check that it's valid, we should do something with it. There's no use in a feedback form if we cannot get the actual feedback, is there?

Form Handler Service

Form data must be processed by a service implementing Heliopsis\eZFormsBundle\FormHandler\FormHandlerInterface. eZFormsBundle provides an abstract handler class to help you send your data by email.

The abstract class already does the following upon form submission:

  • Create a new Swift_Message
  • Set its subject
  • Set its content type MIME header
  • Render the email body from a template after passing it the data in a data parameter

We're going to extend this class and add the missing parameters and logic:

  • The email will be sent to the administrator's email address
  • We'll use the user's email as a sender
  • The subject will be set to New feedback from the website
  • We will use our own twig template

Let's create the concrete class inside our bundle:

 

What did we just do?

  1. We created a public method setRecipient() to allow the DIC to inject the administrator's email address and optionnally his name
  2. We implemented abstract method addRecipients() to pass said values to the message
  3. We implemented abstract method addSender() to load sender email and full name from the feedback data.

    NB: This is kind of quick and dirty as our handler can now only be used with the feedback model class. A better way would be to create a SenderProviderInterface that model classes would implement to provide sender email and name

  4. We implemented abstract method getTemplateParameters() to add the email subject to template parameters.
  5. We implemented abstract method addAttachments() but do not use it as we have no attachment to add.

Now, let's create a twig template for the email:

 

and add our handler service definition to our services.yml file:

 

Note that we made our service depend on yet undeclared parameters admin.email and admin.name. This is good practice to define this kind of data in your application's parameters.yml file as it may change between your staging and production environments.

 

Don't forget to add it to ezpublish/config/parameters.yml.dist as well for future reference.

Handler Provider Service

Now that we have an email handler service, we must tell eZFormsBundle when it should use it. We'll use a HandlerProvider service, very similar to the FormProvider we did earlier. Like then, we'll use a content remoteId mapping service:

 

 

  • the the heliopsis_ezforms.handler_provider.content_remoteid_map.class parameter points to the Heliopsis\eZFormsBundle\Provider\Handler\ContentRemoteIdMap class
  • This class doesn't take any constructor arguments
  • We inject a reference to our acme_forms_tutorial.form_handler.admin_email handler service within the addFormHandler() call
  • Don't forget to declare acme_forms_tutorial.handler_provider as eZFormsBundle's handler provider

That's all folks, You should now receive an email each time a user sends feedback on your site!

 

 

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)

Proudly Developed with from