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?

Dominik Krulak
  • 1,562
  • 14
  • 27

1 Answers1

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 %}
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • 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 02 '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 02 '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. https://github.com/carlcs/craft-preparsefield – carlcs Apr 04 '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 04 '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 07:45
  • @dominik hmm.. so you are no longer using that fork of mine and updated to the latest Preparse plugin, and you have the "parse before save" option enabled in your field settings? Is this right? – carlcs Aug 24 '16 at 16:30
  • Yes @carlcs you're correct at all. I had to upgrade because there was some issue with PHP 5.3. And I didn't realise that I had your fork running up until your answer. I think during upgrade files were just merged together as "parse before save" option still live. This option isn't by default If I remember that. So I assume your code hasn't been overwritten when upgraded. – Dominik Krulak Aug 25 '16 at 06:02
  • Alright so it's working again? – carlcs Aug 25 '16 at 06:07
  • It works as always do except when a file is sent over the form. A random string isn't generated in this case. It's get generated when I resave entry from back end. – Dominik Krulak Aug 25 '16 at 06:14