Programmatically change the content of the file field

Hello! Please tell me how I can programmatically change the content of the file field of a template for a specific page without having to do it manually.

Welcome to the forum :slight_smile:

Could you please let us know which version of Kirby the question is about?

Thanks. I use latest version 3

Have a look at ->update()

1 Like

Thank you! I think that is it. I will try ant replay you.

Yes it does. Now I can insert data from the desired selection. But there was one more question directly related to this. If in the same template there are fields on which this selection depends. Can I display changes in real time? For example, the selection changes with the addition of a new tag in the template field and I update it on the page, but I don’t see the changes until I reload the browser page. Only after that I see the changed data. Is there a function or hook that allows you to display data when changing the values ​​of other fields in this page?

Ok, lets take a step back. What are you trying to achieve? What’s the complete picture? Generally with Kirby, you dont manually update the feilds from code.

What is your project, and what what needs to happen from a users point of view? I suspect there is probably a better way forward.

You wont see the changes in realtime beacause the panel is powered Vue, so until you reload, it is not aware of the changes that have happened since you first loaded the page. So unless the change happens in the panel (rather then in the backround), you wont see it in realtime.

I am currently testing the system on a local server. And I want to understand how much it suits me. I will describe what I want to achieve. There is a field with the type: pages in it through the panel, I select the parent page for the future image gallery. -> There is also a field with the type: tags through which, based on the selected gallery page, tags are dynamically selected through children.pluck (“tags”, “,”, true). -> and the last field with the type: files through which I can select only those images that are in the parent page in the hierarchy and contain only those tags that are written in the field with the type: tags. All this is achieved by going through a loop in which the filter passes to the desired tags> filterBy (‘tags’, $ my_tag, ‘,’). As a result, I would like that after I selected the page for the future gallery, the tags in the panel were pulled dynamically in the panel and the section with the type: files for gallery was automatically filled.

Ok - i think you probably need to take a look at the query language. This allows you to pull data from one feild to another. If the built in queries and filters dont do it for you, you can actually make ur own. See how you get on, and come back if you get stuck. You dont need to brute force it in the template.

Edit: you edited your post while i was typing - looks like you already know of the query language.

i think the filters should work for you, but u might need to make your own in a page model that you call on in the query option in the blueprint.

@texnixe will probably be able to help more here.

You could populate the tags field useing the api

If you make a route that plucks the tag from the gallery pages, you can pipe that into the field. Look at options via api on the page above.

Thank you for your answer, but I’ve already implemented everything that you are talking about. The functions for extracting from children based on the parent page are registered in the page model and are transferred to the blueprint template but are not loaded on the fly (dynamically) into the blueprint template, but are displayed only after it is reloaded. There has to be a way or hook which will help to insert data on the fly?

@VECT0R Please don’t link to screenshots of code but post it here as real code for easy reference. Thank you.

There is (currently) no way to trigger the frontend to reload from the server. I think you would need a custom field that queries the server at regular intervals and then inserts any changes. Or some such mechanism…

Is not one of the hooks that are available on the Kirby system not suitable for this task? If this is true and you recommend that I create a custom field that receives data from the server at certain intervals, you may be hinting how to do this in Kirby. Thanks!

The hooks trigger stuff on the server using PHP, its not in real time. You will need to do it from Vue. Bascially you create a route in the config.php and then from the custom field, you trigger that endpoint to make stuff happen.

So you get the route to use PHP to find all the tags from the pages you need and prepare it in JSON, then from Vue you consume that data and populate the field.

1 Like

Hooks do things in the background before or after some action, but they don’t return anything to the frontend.

But as far as I can see, you don’t want to get stuff when something on the server is changed, but when a field value is changed.

  • User selects a page in the pages field.
  • Your code has to listen to a change in that field even before that change is stored anywhere on the server
  • When a change happens, your code has to get the value from that field (from the content store) and query the files and tags via a route/endpoint.
  • Then insert the results on the fly into the other fields.
1 Like

How to listen to events of changing the value of fields using Kirby? Can you enlist your help and get links to the CMS Kirby documentation section to implement each of the steps you have proposed?

  • Your code has to listen to a change in that field even before that change is stored anywhere on the server
  • When a change happens, your code has to get the value from that field (from the content store) and query the files and tags via a route/endpoint.
  • Then insert the results on the fly into the other fields.
    I would be very grateful!

You have to look into the custom field documentation:

Both your tags field and the files field need a listener.

Then you can get the value from the content store:

Custom API endpoints:

1 Like

Thank you very much for the information. They will study and implement their plans.

I wonder if it would make sense to create a custom set of fields for this purpose rather than individual fields that do the same thing.

I haven’t seen Pages Field in the UI Kits.
Otherwise, how can I work with the field and hang an event listener on it?

Looks like it is missing, don’t know why.