Save global variable in “admin interface” and get/read variable in kirby snippet …

Dear community,

I think I’m having a very simple to solve problem here … but I could not figure it out in several hours of trying.

My current kirby project has one hidden page which serves as “admin interface” … on this page I’d like to set a global variable via

<form action="<?= $page->url() ?>" method="post">

and

<button name="localization-submit" type="submit" value="SAVE">SAVE</button>

(I also tried <input name="localization-submit" type="submit" value="SAVE">. The <form> has two radio buttons (but different values) and the value should be written into that global variable.

On some other page, I’d like to get that global variable and use it in some if / else condition.

I created a controller file in site/controllers/admin.php with the following content:

<?php
return function($kirby, $pages, $page) {

  if($kirby->request()->is('POST') && get('localization-submit')) {
    $localization = $_POST;
  }

  return compact('localization');
}

And I created a plugin file in site/plugins/functions/index.php:

<?php
function localization() {
  //return page("admin")->$localization;
  return $localization;

Am I missing out on something, or what am I doing wrong?

Here’s two references I used besides the email form cookbook:

Without going into details yet, don’t know if you have mixed up your files in your description, but this

<?php return function($kirby, $pages, $page) { if($kirby->request()->is('POST') && get('localization-submit')) { $localization = $_POST; } return compact('localization'); }

should be in your controller and the function in the plugin.

@texnixe, oh yeah … sorry, I mixed it up in my description. I’ll quickly edit my post.

EDIT: After submit, it seems the function in plugins does not know about $localization

That’s what I was just going to say, $localization is undefined in your function.

In your controller, you just return the $_POST array to the template, but you don’t do anything with it like storing it in the session or in a page…

Oh. I tought that’s what return compact('localization'); does.

That passes the data to the template, so you can use it there but nowhere else.

Depending on the purpose of your localization value, you can store it permanently as a page value or in the session, or in a cookie…

I’d like to store it permanently … until it is changed via that same <form>

Thought I could NOT use localStorage or a cookie as it’s client sided. So I wanted to store it as maybe a global variable or as you said page value. Then I could retrieve that page value from some other page, I guess.

Maybe I could have Localization: xxx in my content/site.txt?

Yes, that would make the most sense for values that you want to store permanently or until they are changed. You can then easily retrieve that value using $site->localization().

Could you maybe please link to a guide or cookbook on how to achieve this in the controller? Probably it’s way easier than I fear it is.

Maybe even the controller is the wrong location to do this, I’m still a little bit confused about controllers and plugins unfortunately.

My first guess would be something in the controller like:

<?php
return function($kirby, $site, $page) {

  if($kirby->request()->is('POST') && get('localization-submit')) {
    $site->$localization = $_POST;
  }
}

You have to use [$site->update()](https://getkirby.com/docs/reference/objects/site/update). But it doesn’t make sense and you cannot store the $_POST array, but only the field value (get('fieldname')) which should also be validated (i.e. only accept intended values), so that the user cannot store any nonsense. I’d also check if the given value is different from the stored value and only update if different.

I’d basically like to store the value of the selected radio button on submit.

As I wrote above:

See the form examples in cookbook.

I fear I cannot even change the content of my site.txt when updating with a string:

<?php
return function($kirby, $site, $page) {

  //if($kirby->request()->is('POST') && get('localization-submit')) {
  if($kirby->request()->is('POST')) {
    $site->update([
      //'localization' => get('localization')
      'localization' => 'update-test-hello-world'
    ]);
  }
}

I even added the field to my blueprint, though I guess that shouldn’t be necessary at all, right?

Here’s my form:

<form id="localization-form" action="<?= $page->url() ?>" method="post">
  <h2>Localization</h2>
  <div>
    <input id="kmh" type="radio" name="localization" value="kmh" required>
    <label for="kmh">Kilometers per Hour (km/h)</label>
  </div>
  <div>
    <input id="mph" type="radio" name="localization" value="mph">
    <label for="mph">Miles per Hour (mph)</label>
  </div>
  <button id="localization-submit" name="localization-submit" type="submit" value="SAVE">SAVE</button>
  <!--
  <input id="localization-submit" name="localization-submit" type="submit" value="SAVE">
  -->
</form>

PS: I don’t quite get the value="<?= $data['some-name'] ?? '' ?>" definitions in the cookbook form examples …

PPS: On another page, I was able to create an email form which works as intended … thanks to the cookbook.

Some methods(updating, creating etc.) need permissions (they are marked with a lock icon in the reference), either through authentication or impersonation: https://getkirby.com/docs/guide/users/permissions#authenticate-or-impersonate

Saw that in the cookbook, too.

Tried with:

<?php
return function($kirby, $site, $page) {

  if($kirby->request()->is('POST') && get('localization-submit')) {
    $kirby->impersonate('kirby');

    $site->update([
      'localization' => "update-test-hello-world"
    ]);
  }
}

… and also without the && get('…') part in the if condition.

Didn’t think this would be that hard. Sorry for the circumstances.

Try without the post stuff first, then if that works try without the second condition

 if($kirby->request()->is('POST')) {
    $kirby->impersonate('kirby');

    $updated = $site->update([
      'localization' => "update-test-hello-world"
    ]);
  }

Uhm … how would it work without the POST stuff?

I read through all the form cookbooks again and the linked forum posts … just cannot get it working.

How could I log what’s going on in the console?

Does the site update if you put this into the controller and open the page in your browser:

<?php
return function($kirby, $pages, $page) {

    $kirby->impersonate('kirby');

    $updated = $site->update([
      'localization' => "update-test-hello-world"
    ]);
};
1 Like