Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Hi! I need to save contents of an API call after I hit "Save" button in admin dashboard.
In kfunctions.php i've added event listener but using code provided below I get pre-save field values but not current. Can someone give any hint? :)

Code: Select all
$FUNCS->add_event_listener( 'page_saved', 'partial_cache' );
function partial_cache(){
    global $PAGE, $CTX;

    if ($PAGE->tpl_name == 'SOME_TEMPLATE') {
        $longtitude = $CTX->get('map_longtitude');
        $latitude = $CTX->get('map_latitude');
        $map = file_get_contents("EXTERNAL_API_URL" . $longtitude . "," . $latitude);
        file_put_contents( K_COUCH_DIR . "../" . "FILE_TO_SAVE" , $map);
    }
}
Hi,

The 'page_saved' event is passed the page object being saved; you can get the current values from it directly e.g. as follows -
Code: Select all
$FUNCS->add_event_listener( 'page_saved', 'my_save_handler', 10 );
function my_save_handler( &$page, &$errors ){
    global $FUNCS;

    if( $page->tpl_name != 'some_template.php' ) return; // not the template we are interested in. Return quick!

    if( !$errors ){
        // get the data from fields we are interested in
        $longtitude = $page->_fields['map_longtitude']->get_data();
        $latitude  = $page->_fields['map_latitude']->get_data();
        ..
        ..
    }
}

Hope this helps.
Thank you! It is working as expected now. I've used your approach and tested if I could use a global $PAGE object the same way. If I understood correctly it is set on each event occurrence. Is there any difference if I use your method or this one instead:

Code: Select all
$FUNCS->add_event_listener( 'page_saved', 'partial_cache' );
function partial_cache(){
    global $PAGE;

    if ($PAGE->tpl_name == 'TEMPLATE') {
        $longtitude = $PAGE->_fields['map_longtitude']->get_data();
        $latitude = $PAGE->_fields['map_latitude']->get_data();
        ..
    }
}

From the admin panel, the global $PAGE is usually the page being saved so, as in your case, this global object and the object supplied by the event are the same and it wouldn't matter which of the two you used.

However this might not be the case in front-end DBFs (or custom admin forms) where multiple pages get saved. So it is better to work with the page object being supplied by the event itself.

Also, you need to take the '$error' param into consideration so I'd advice to use the event's params.
Thank you so much! It helped a lot
5 posts Page 1 of 1