I have a frontend form with an email field, and I would like to prefill it with the user’s email address. {{ dump(grav.user.email) }}
gives me the address, but putting it in the form doesn’t return anything. My form field definition looks like this:
formular-service:
fields:
email:
type: email
config-default@: grav.user.email
label: 'Wahlbestätigung an folgende E-Mailadresse schicken:'
validate: true
What’s wrong with that? I got it from the docs?
@Netzhexe,
Maybe you should try site.author.email
instead of grav.user.email
?
Yeah no since I want to show the logged in user’s email. I tried though, and site.author.email
does work as expected. So why not with grav.user.email
? user.email
doesn’t work either…
From the docs I understand config-*@
takes “value from Grav configuration”. I think you should use data-*@
, but I’m not sure if it works at all No matter how I tried to get user data it wouldn’t work:
data-default@: '\Grav\Common\User\User::getProperty("email")'
data-default@: '\Grav\Common\User\User::get("email")'
data-default@: \Grav\Common\User\User::get("email")
And even straight from the docs
data-default@: '\Grav\Plugin\Admin::route'
It just won’t work. Maybe there’s a bug
@Netzhexe,
I don’t know if it’s the best solution, but it seems to work:
Note:
- I guess, you could also add the static function to your theme. And adapt the call…
'\Grav\Theme\MyTheme::getEmail'
@anon76427325, any idea why wouldn’t it work with any of my examples? Or even an example from docs?
Thank you both for your time and ideas! I also thought in the data-default@ direction and tried it almost @anon76427325’s way, but I missed the $grav = Grav::instance();
bit so I got an error about using $this
out of an object context. But with that bit it works fantastically!
Since I was in a hurry… I solved it in a SUPER HACKY way, but for the sake of maybe helping someone else in a pinch some day, here it is:
public function onOutputGenerated()
{
$content = $this->grav->output;
$tmp = substr($content, 0, strpos($content, 'class="usermail "'));
// I added the class to the field in the frontmatter, take note of the space
$cut = strrpos($tmp, 'value=""');
if ($cut) {
$tmp = substr($content, 0, $cut);
$tmp .= 'value="'.$this->grav['user']['email'].'"';
$tmp .= substr($content, $cut+8);
$this->grav->output = $tmp;
}
}
Butt-ugly, I know!