Using `$page` variable in `config.php` (Janitor Plugin)

Hello,
I want to use @bnomei’s excellent Janitor plugin to update specific fields on click, but for this to work, I need to use the $page variable - which has no context inside config.php.

I made a blueprint field, like this:

update:
    type: janitor
    label: Update fields
    progress: Updating ..
    job: updatejob

Now, in config.php, I’d like to do something like this:

<?php
    return [
        'bnomei.janitor.jobs' => [
            'updatejob' => function() {
                // some stuff
                $page->update([
                    'some-field' => $someValue
                ]);
                return [
                    'status' => 200,
                    'label' => 'Fields updated!'
                ];
            }
        ],
        // ... other configs
    ];

… but all I get is Updating fields .. and then back to its original state.

@texnixe, you suggested this plugin over in the other thread, are you possibly able to help me?

thx!

your are right that the janitor job has no such context, no knowledge about the page object the button might be at because they are triggered useing a route. i wanted mine to work without the panel as well as a within.

but i agree that the page object might be useful in the job if it was a button. i will think about a solution and post asap.

2 Likes

Marvelous, thank you so much! I’d like to hear back, and I will observe the issue on github!

Thx!!

We were in need of this as well and worked around the missing page context by using the referrer information:

            $referrer = $_SERVER['HTTP_REFERER'];

            if (!$referrer) {
                return [
                    'status' => 404,
                    'label' => 'Missing context!'
                ];
            }

            $referrer = explode('/', $referrer);
            $referrer = end($referrer);
            $referrer = str_replace('+', '/', $referrer);

            if ($referrer === 'site') {
                $page = site();
            } else {
                $page = kirby()->page($referrer);
            }

This all felt very hacky and it won’t work, if the browser doesn’t send a referrer. But it worked in our case. Having the plugin pass the context directly would be really great!

PS: You’ll have to use kirby()->page() if you’d like this to work with drafts as well. Learning by doing …

1 Like

… where do I need to use kirby()->page()?

Anyway, just brilliant, thanks for sharing :smiley:
It works as expected, except that …

          $page->update(['somefield' => 'something']); // .. this line ..

            return [
                'status' => 200,
                'label' => 'Updated!'
            ];

doesn’t work :frowning_face:

It’s in my example above when fetching the page from the referrer:
$page = kirby()->page($referrer);.
$page = page($referrer); won’t work for drafts.

1 Like

// Wait, it works! Reloading did the trick … (I forgot that before, I was using page.update.after which is why it was applied immediately)

Anyone know how to trigger a page reload in the panel? :slight_smile:

js location.reload()should do the trick. but there might be a panel specific way as well.
https://www.w3schools.com/jsref/met_loc_reload.asp

this.load(true); in vue component maybe.

i am making good progress on adding the context page. only thing left is actually getting the current page.uri in the vue component. does a field has access to the current page? maybe panel plugin guru @sylvainjule knows? :innocent:

Is $this->model()->uri() what you’re looking for?

almost. i need it in the js/vue component code. inside a custom method. ah… i will try just forwarding that in a prop.

I guess that’s the best way, aside from this.$route.path (which returns the panel uri with / replaced by +) I don’t recall the field having access to the uri.

1 Like

@nilshoerrmann @daybugging @S1SYPHOS
v1.3.0 now has page and custom data string from blueprint in job function.

4 Likes

A great plugin got even more excellent today!

1 Like

Cool, we’ll check that out next week!