3

I have redirect to a web page upon successful form submission using guest plugin. The redirected page is in announcement tone so I'd like to prevent it from direct access with exception that comes from form submission.

Is there something Craft can do?

3

Add a Preparse field to the entry type you're posting to and make it generate a random string on entry save. Then make the Guest Entries plugin redirect to a URL with that Preparse field's value in the query string.

<input type="hidden" name="redirect" value="success?id={id}&key={preparseField}">

You should now be able to get that entry model in your template and test against that Preparse field's value.

{% set query = craft.request.getQuery() %}
{% set entry = craft.entries.id(query.id).preparseField(query.preparseField).first() %}

{% if not entry %}
    {% exit 404 %}
{% endif %}
|improve this answer|||||
  • Hi @carlcs So basically this plugin pass a value set in preparse field setting page in CP to some model in front end only on entry save event? I've set some value with several characters to shuffle like this {% set chars = '0123456789'|split('') %} {% set string = shuffle(chars)|join %} {{ string|slice(0, 5) }} and saved it as preparse field with handle secterKey. On the front end everything is according to your answer in code except I changed key={secretKey} and .preparseField(query.secretKey). I got error message that key secretKey doesn't exist. – Dominik Krulak Apr 2 '16 at 12:04
  • I'm not even sure I proceed correctly because on entry form my URI looks like this ...success?id=123&key=. I assume the string variable I set doesn't get passed to element model. Or I don't understand How it works. On plugin setting page both options are enabled. I think that doesn't effect it. – Dominik Krulak Apr 2 '16 at 12:08
  • @dominik yeah, I forgot that the plugin saves Preparse fields after the element itself is saved. Have a look at my forked version of the plugin that has a "Parse before save" option added. github.com/carlcs/craft-preparsefield – carlcs Apr 4 '16 at 12:11
  • Thank you for your effort to update the plugin. That worked well. Upon successful form submission my URI looked like this ?id=422&key=097306 but template got error with following message: Key "secretKey" for array with keys "p, id, key" does not exist. So I though I'm not accessing the actual string value but preparseField itself. So I ended up with (query.string) whereas it got passed through this error message but have encountered on another one. It can't access an attribute ("attribute") on a string variable ("422") which is our entry.id. It seems to be a minor mistake – Dominik Krulak Apr 4 '16 at 19:48
  • Hi @carlcs. Would it still work if we upload a file? The Preparse field seems to be break generating a key string (remain blank) on before entry save when a file is attached to form. I think it connects's to this issue. – Dominik Krulak Aug 24 '16 at 7:45

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.