Updating toggle field value programmatically using $page->update()

Hi,

I am looking to create a kind of ‘auto publish’ publish feature for an array of Slides.

I have the following data structure, notice the published field

fields:
    - 
      slideimage:
        - slider-image.jpg
      heading: MHYSA
      headingcolour: white
      blurb: >
        Duis aute irure dolor in reprehenderit
        in voluptate velit esse cillum dolore eu
        fugiat nulla pariatur. Excepteur sint
        occaecat cupidatat non proident.
      link: http://localhost:3000/discover
      newtab: 'false'
      published: 'true'
    - 
      slideimage:
        - underwater.gif
      heading: 'Stocks & Shares'
      headingcolour: white
      blurb: >
        Duis aute irure dolor in reprehenderit
        in voluptate velit esse cillum dolore eu
        fugiat nulla pariatur. Excepteur sint
        occaecat cupidatat non proident.
      link: https://noodsradio.myshopify.com/
      newtab: 'true'
      published: 'false'
    - 
      slideimage:
        - keith-image.jpg
      heading: In Fine Style with Wilfy D
      headingcolour: white
      blurb: >
        Duis aute irure dolor in reprehenderit
        in voluptate velit esse cillum dolore eu
        fugiat nulla pariatur. Excepteur sint
        occaecat cupidatat non proident.
      link: >
        http://localhost:3000/shows/back-to-the-future-cosmic-80s-breakfast-show-w-murphy-mcfly-15th-april-20
      newtab: 'false'
      published: 'false'
...

I would like to know how I would update the published field to be true

At the moment I have the following code, I’m updating another toggle field on the page as a test. This doesn’t seem to be working…

'bvdputte.kirbyqueue.queues' => [
        'home' => function ($home) {


            $field = 'featuredartistsslotone';
            $fieldData = false;


            try {
                $newPage = page('home')->update(array($field => $fieldData));
                return true;
            } catch (Exception $e) {
                return $e->getMessage();
            }

            // No need to return or display anything else!
        }
    ],

You probably need a string here:

 $fieldData = 'false';
1 Like

Okay, I’ve made the Boolean a string.

Nothing happens when I run the queue though :thinking:

Where is this code running, not familiar with the plugin? If nothing at all happens, you probably have to authenticate (kirby()->impersonate()).

1 Like

Okay. I tried running that within the queue function and still, nothing happened. Is inside the function the right place to call the impersonate method?

@bvdputte Do you have any tips on running the update method within a queue function?

When I add

$kirby   = kirby();
$kirby->impersonate('kirby');

I get the following error

Call to a member function impersonate() on null

I’m not sure. AFAIK your code should work?

Okay, think I’ve found the solution. I added the $kirby->impersonate('kirby') call to the Queueworker.php file and then ran the queue file. Worked like a charm!

I used your Auto Publishing plugin as reference to solve the issue so thanks for that! :smiley:

I’ll go with this solution for now, unless you can specify where would be a better place to add the impersonate call?