2

I'm trying to build a front end to display entries collected through Sprout Forms. I want to create a list of all entry titles that can be clicked through to see the full entry.

I can list a single entry, or all entries:

{% for formEntry in craft.sproutForms.entries.formHandle('entry') %}
  {{ formEntry.title }}
{% endfor %}

But when I try to create a link, {{ formEntry.url }} doesn't work. I'm not entirely sure if this is a Sprout Forms issue or a Craft routing issue, or if I'm just missing something obvious

2

A note on Elements

Craft allows plugins to create custom Element Types. Elements have the option to support URIs, but it is not required. Out of the box, Craft currently has 7 Element Types and only two of them (Entries, Categories) support URIs. Here's a good overview of the default Elements.

Sprout Form Element Types and URIs

Sprout Forms adds two new Elements to Craft. The SproutForms_Form Element and the SproutForms_Entry Element both which do not support URIs simply because we didn't feel there was a strong use case for them to be implemented for most form submission scenarios.

While it may not make sense to support URIs for all SproutForm_Entry Elements out of the box, you can still create a way for those Elements to be linked to in your templates, you will just have to do it manually.

How to manually create URIs for Sprout Form Entry Elements

To customize Craft to handle custom URLs for an Element that doesn't support URLs (of even for an Element that supports that that you need to do something fancy with), here is a rough example of the steps needed.

Loop through your Elements and output the URL you wish them to have to link to your Entries:

/submissions/{{ formEntry.id }}

You will then need to setup a route to tell Craft which template it should load when it encounters a URL like that. You can create a route in the CP or in the craft/config/routes.php file. This route tells Craft to load the submission/_entry.html template each time a URL matches submission/ANYTHING:

return array(
    'submissions/(?P<formEntryId>.*)' => 'submissions/_entry'
);

In your submissions/_entry template, you will need to use the ID information in the URL to query for the Form Entry that you wish to display:

{% set entry = craft.sproutForms.getEntryById(formEntryId) %}

In the above example, we're able to use the formEntryId variable because we setup our route to assign the second segment of the URL to the value formEntryId. If you setup your route in the control panel, you may need to grab the ID using getSegment:

{% set formEntryId = craft.request.getSegment(2) %}
{% set entry = craft.sproutForms.getEntryById(formEntryId) %}
2
  • Thanks for such a detailed answer. I also discovered I could do it with query strings. Links: /entries?id={{ formEntry.id }} and then {% set id = craft.request.getparam('id') %} {% for entry in craft.sproutForms.entries.formHandle('entry').id(id) %}
    – artmem
    Aug 5 '16 at 13:52
  • Nice. Good catch. I overlooked the easy way that doesn't require the extra step with routes. Aug 5 '16 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.