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

#1

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!

#2

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
#3

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

Thx!!

#4

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
#5

… 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:

#6

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
#7

// 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:

#8

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.

#9

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:

#10

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

#11

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

#12

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
#13

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

4 Likes
#14

A great plugin got even more excellent today!

1 Like
#15

Cool, we’ll check that out next week!