Define User Action
UI Integration
Arguments
Crud integration

Next demo defines an user action that requires arguments. You can specify arguments when the user action is invoked, but if not defined - user will be asked to supply an argument. User action will automatically validate argument types and it uses same type system as fields.

$model = new \Atk4\Data\Model($owner->getApp()->db, 'test');

$model->addUserAction('greet', [
    'appliesTo' => \Atk4\Data\Model\UserAction::APPLIES_TO_NO_RECORDS,
    'args' => [
        'age' => [
            'type' => 'string',
        ],
    ],
    'callback' => function ($model, $name) {
        return 'Hi ' . $name;
    },
    'ui' => ['executor' => [\Atk4\Ui\UserAction\JsCallbackExecutor::class]],
]);

$model->addUserAction('ask_age', [
    'appliesTo' => \Atk4\Data\Model\UserAction::APPLIES_TO_NO_RECORDS,
    'args' => [
        'age' => [
            'type' => 'integer',
            'required' => true,
        ],
    ],
    'callback' => function ($model, $age) {
        return 'Age is ' . $age;
    },
]);

$owner->add(new \Atk4\Ui\Form\Control\Line([
    'action' => $model->getUserAction('greet'),
]));

\Atk4\Ui\View::addTo($owner, ['ui' => 'divider']);

\Atk4\Ui\Button::addTo($owner, ['Ask Age'])
    ->on('click', $model->getUserAction('ask_age'));
Greet
Ask Age