mecha-cms / x.panel

Control panel feature.
Other
6 stars 0 forks source link

TODO: Need a Way to Update the `$lot` Data From Inside the Hook #17

Closed taufik-nurrohman closed 4 years ago

taufik-nurrohman commented 4 years ago

Currently, it is not possible to pass $lot variable by reference due to the design of call_user_func():

// Does not work :(
Hook::set('do.page.set', function($_, &$lot) {
    $lot['data']['time'] = date('Y-m-d H:i:s'); // Automatic `time` data on publish
    return $_;
});
// Sometimes works :|
Hook::set('do.page.set', function($_) {
    $_POST['data']['time'] = date('Y-m-d H:i:s'); // Automatic `time` data on publish
    return $_;
});

Possible solution: remove the $lot parameter and simply pass it as another property of $_ variable:

Hook::set('do.page.set', function($_) {
    $_['form']['data']['time'] = date('Y-m-d H:i:s'); // Automatic `time` data on publish
    return $_;
});

$GLOBALS['_']['form'] = e($_POST ?? []);