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.

Plugins

Language support for a plugin: undefined variable $grav

Solved by Ole Vik View solution

Started by Anna 7 years ago · 4 replies · 556 views
7 years ago

I'm adding language support to my first plugin, and I have some questions as to how to do that correctly. What I need is one string (configurable), to be used by a custom function. So I put the following in my plugin's languages.yaml file:

YAML
en:
  PLUGIN_ANTISPAM:
  NOSCRIPT: [email address is javascript encrypted]

de:
  PLUGIN_ANTISPAM:
  NOSCRIPT: [Mailadresse ist Javascript-verschlüsselt]

And in my custom function, I tried to read this with the following:

PHP
function munge($array) {
    // a bunch of irrelevant stuff here
    $txt .= "<noscript>".$grav['language']->translate(['PLUGIN_ANTISPAM.NOSCRIPT'])."<"."/noscript>";
  return $txt;
}

I got this from the documents. However, this returns a Crikey! error that says "Undefined variable: grav". What is wrong?

7 years ago Solution

$grav should probably be $this->grav, unless the instance was previously declared as a variable in the method.

7 years ago

That was it, that did the trick! 🤩 (I love you so much right now) Should I post a correction for the page in the documents where I found the $grav reference? I shall try to figure out how best to do that.

Now there's still a minor bug in the plugin, but mostly it's good to go, so I'm putting it up right now and posting that issue in the Grav tracker!

7 years ago

You can easily edit the page by clicking "Edit" at the top, that creates a Pull Request on GitHub.

7 years ago

Nice. Thank you yet again for lending a hand to a GitHub and plugin newbie! I think I managed to create a proper pull request – if I've gotten all of that right, including my plugin repo, I shall do my best improving the documents and making new plugins to help make Grav an even better CMS for everybody. :purple_heart:

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 72 2 months ago