Update problem between structure field and select field in a page

Hi,

In the site options page, I let the user build categories with a structure fields.
Ex: Categories A, B, C

In a page, I have a select field populate with the categories coming from the structure field above. (A, B, C)

Step 1: the user set page 1 with category C and save the page.
Step 2: the user decide to change the third categories name: A, B, D in the site options structure field.

Result: the page 1 has always “C” in his content txt file but his select box show now an empty result because Kirby can’t match the value save in the txt content file “C” and the new value in the select field which is now “D”

How to update the page 1 content to reflect the change made by the user in the structure field in the option page ?
Of course this change can concern several pages.

Any idea ?

I think you need a hook (panel.site.update) that registers the change on site update (since you have both the new and the old site object, you can check what has changed). Then you need to filter out all pages with the old value and update these pages with the new value.

Ok you confirm what I thought. I did some test yesterday with hook (panel.site.update) but had pop-up error message when just tried to var_dump new and old site object.

I need to test with a clean install to see if the problem come from a plugin or not.

Thanks Sonja, I will come back with the result of my test.

That’s not a bug, debugging hook is terrible. Keep the Chrome Dev Tools open and checkout the latest response in the networks tab.

Hooks are not expected to return anything so when you add something to the response, on the front-end Kirby doesn’t understand it.

It helps to write to file, though.

Anyway, you can get the content arrays of $site and $oldSite` like this:

kirby()->hook(['panel.site.update'], function($site, $oldSite) {
  $new = $site->content()->toArray();
  $old = $oldSite->content()->toArray();
});

Or even better:

kirby()->hook(['panel.site.update'], function($site, $oldSite) {
  $new = $site->content()->get('categories');
  $old = $oldSite->content()->get('categories');
});
1 Like

In your code example ‘categories’ is the name of my structure field ?

But without var_dump($new/$old) i’m a blind coder :-/

Yes, categories is the name of the structure field. Do you need the structure field for any reason? Otherwise, I’d go with a field that gives me a comma separated list (tags field, list field)

You can do the dumping in a template, to see what you get from the field, then transfer to the hook.