Ralf Posted February 6, 2021 Share Posted February 6, 2021 Hi all, i am currently facing a task where i want to fill an integer field using php based on an input from another field as already written in the subject. Specifically, my request is as follows: I have a date field in which I select e.g. the date 2021-02-06. In a second field (I thought e.g. of an integer field) the correct calendar week is now automatically entered according to the input from the "date field". In my case this would be "06" or "202106". The PHP code for the conversion would be theoretically this date("oW", strtotime("2011-01-07")); // gives 201101 Could someone help me with the hook here??? Link to comment Share on other sites More sharing options...
BillH Posted February 6, 2021 Share Posted February 6, 2021 I'd suggest hooking on Pages::saveReady. So, you could put something like this (not tested) in ready.php: $pages->addHookAfter('Pages::saveReady', function($event) { $page = $event->arguments('page'); if($page->template == "relevant-template") { $originalValue = $page->date_field; $newValue = date("oW", strtotime($originalValue)); // or whatever you need to do $page->set("integer_field", $newValue); } }); Note that if hooking on saveReady you don't need to save the field or page because the page is about to be saved. 1 Link to comment Share on other sites More sharing options...
Ralf Posted February 9, 2021 Author Share Posted February 9, 2021 @BillH thank you very much, that was exactly what I was looking for.... I only had to modify the following line slightly, otherwise it gives me problems with my date (fieldtype: datetime). // old line $originalValue = $page->date_field; // modified line to my needs $originalValue = strftime('%Y-%m-%d', $page->getUnformatted('date_field')); Thanks cu Ralf If someone wants to see the whole code again... Spoiler $pages->addHookAfter('Pages::saveReady', function($event) { $page = $event->arguments('page'); if($page->template == "relevant-template") { $originalValue = strftime('%Y-%m-%d', $page->getUnformatted('date_field')); $newValue = date("oW", strtotime($originalValue)); // or whatever you need to do $page->set("interger_field", $newValue); } }); 1 Link to comment Share on other sites More sharing options...
BillH Posted February 9, 2021 Share Posted February 9, 2021 It might also be a good idea or turn output formatting off. Then you probably wouldn't need to use $page->getUnformatted(), and it might be better when setting values. So: ... if($page->template == "relevant-template") { $page->of(false); ... Link to comment Share on other sites More sharing options...
Ralf Posted February 9, 2021 Author Share Posted February 9, 2021 8 minutes ago, BillH said: It might also be a good idea or turn output formatting off.... Thanks for the suggestion, but I need a formatted output in various other places (eg in the backend overview), so I can not implement this manner. I have inserted the $page->of(false); ? But I still have a question about your first answer, or rather I do not really understand the last sentence. On 2/6/2021 at 6:36 PM, BillH said: Note that if hooking on saveReady you don't need to save the field or page because the page is about to be saved. How can this work? If I enter a date in the field A, how can he fill the field B WITHOUT saving the page (without Ajax)? Or am I just misunderstanding your statement? Link to comment Share on other sites More sharing options...
BillH Posted February 10, 2021 Share Posted February 10, 2021 When you add data to a field, the data is not saved to the database. This is the same as when you type the data into the CMS form (before you hit the save button). As you know, after changing the content of a field you can save either the whole record or just that field: $page->save(); // Or just the field $page->save("integer_field"); But the hook is after Pages::saveReady, so it always runs just before the page is saved to the database – for example, the hook will run after a user hits the Save button but before data is saved to the database. So if you use $page->save(), the data will be saved twice, which is unnecessary – though extra safe! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now