Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Config-default@: grav.user.email does not work for email field in frontend form

Solved by pamtbaau View solution

Started by Anna 5 years ago · 6 replies · 793 views
5 years ago

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:

YAML
  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?

5 years ago

@Netzhexe,

Maybe you should try site.author.email instead of grav.user.email?

5 years ago

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…

5 years ago

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:

YAML
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

YAML
data-default@: '\Grav\Plugin\Admin::route'

It just won't work. Maybe there's a bug

5 years ago Solution

@Netzhexe,

I don't know if it's the best solution, but it seems to work:

  • Create plugin $ bin/plugin devtools new-plugin
    I called it 'xx' (yes I'm lazy)
  • In plugin add:

    PHP
    public static function getEmail() {
     $grav = Grav::instance();
    
     return $grav['user']['email'];
    }
    
  • In your form definition in frontmatter use:
    TXT
    data-default@: '\Grav\Plugin\XxPlugin::getEmail'
    

Note:

  • I guess, you could also add the static function to your theme. And adapt the call... '\Grav\Theme\MyTheme::getEmail'
last edited 01/31/21 by pamtbaau
5 years ago

@pamtbaau, any idea why wouldn't it work with any of my examples? Or even an example from docs?

5 years ago

Thank you both for your time and ideas! I also thought in the data-default@ direction and tried it almost @pamtbaau'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:

PHP
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! 😆

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1140 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 63 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 138 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 114 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 132 7 months ago