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.

General

Setting plugin variables with a Form

Started by Richard Hainsworth 8 years ago · 1 replies · 667 views
8 years ago

I cannot get twig/md to recognise a dynamic variable.

In a plugin file, I have

PHP
class MyPlug extends Plugin 
{ protected $plugvar;
...
public function onPluginsInitialized() {
$this->grav['myplug'] = $this;
...
public function get($var) { return $this->plugvar[$var] ;}
public function set($var, $val) { $this->plugvar[$var]=$val;}
...
public onFormProcessed(Event $e) {
...
  $data=$form->value()->toArray();
  case 'myplug':
     foreach($data as $key => $val) {
        $this->plugvar[$key] = $val; }
...

Then in a shortcode file

PHP
 $this->shortcode->getHandlers()->add('myplug', function(ShortcodeInterface $sc) {
  $params = $sc->getParameters();
  foreach( $params as $key => $val){
   if ($val != null ) {
      $resp = $this->grav['myplug']->get($key);
      if ($resp) return $resp;
      else return 'undefined';

In a form.md

YAML
---
form:
  name: set-operator-form
  fields:
    - name: operator
      type: hidden
      content: undefined
  buttons:
    - type: submit
      value: "Select Operator"
    - type: reset
      value: Reset
  process:
    - myplug
process:
    twig: true
cache_enable: false

And finally in default.md

TXT
[myplug operator/]

The first time default.md is served, I get an expected 'undefined' response in the html.

Also after clicking on the Submit button of the Form, and going to default.md, there is no change, and I get an undefined response.

Using dump and $this->grav['debugger']->addMessage(...);
provide feedback that the form.md operates correctly.

Somehow default.md is not being processed again with the new dynamic information.

Is there an alternative way to get session variables? I have tried creating twig_vars, but I cannot access them across pages.

I know I am missing something, but I can't find a clue in the documentation.

last edited 04/15/18 by Richard Hainsworth

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 85 13 hours ago
General · by pamtbaau, 18 hours ago
1 60 18 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 350 5 days ago
General · by Duc , 6 days ago
3 44 5 days ago