Forms - userid in hidden field?

Hi! I juste discovered Grav and so far I am intrigued.

I want to let logged in users fill in a form on the frontend and I want that form to collect their username and/or email via a hidden field.

I suppose this is possible in Grav but I cant figure out how.


Two ways you can choose:

You could add a hidden field to the form:

- name: hidden_email
  type: hidden
  default: ''

and edit the default hidden field to interpret the Twig, because by default it does not do that.

So in your theme add a templates/forms/fields/hidden/hidden.html.twig with

{% set value = (value is null ? evaluate(field.default) : value) %}

<input data-grav-field="hidden" data-grav-disabled="false" type="hidden" class="input" name="{{ (scope ~|fieldName }}" value="{{ value|join(', ') }}" />

What’s changed from the default hidden field is the evaluate() call on top, on the default value.

Of course this is not 100% ideal as a user could edit the HTML prior to sending the form.

Another way is to provide a process action for the form.

For example, the Form plugin uses these lines to save the form content and provide the save action. You can write your own plugin to provide an alternative action for the form.

You can check for example how the Email plugin adds its email processing to the form and use the same procedure.

Thanks for taking your time to explain things to me.

Do I replace the existing hidden.html.twig with the modified one or do I let them live side by side somehow?

I made the field visible just to see if would translate into an actual emailaddress or not. It did not. :frowning:

If you add the twig to your theme, it will override (gets precedence over) the one from the plugin.

I tried with a sample form and it worked, you can also debug grav.user in your Twig.

Got it! Thanks!