ATK contains lots of hidden gems. Most of them are hidden because they are undocumented or because they are only documented in the API docs (which nobody seems to read...). In the past few years we've tried to improve the documentation for ATK. We've created a
Wiki with lots of how-to's, the
"Pizza Guides", a
demo application and improved the
API documentation. However there are still some features of ATK nobody seems to know about.
In this second iteration of the "
ATK's hidden gems" series we will look at a largely unknown feature of ATK's record actions.
Scripting record actions
ATK allows you to alter the actions which can be performed on records by overriding a special method called
recordActions inside your node. You can add, remove or modify actions inside the special
$actions key/value array. The keys of this array are the action names and the values contain the URLs for these actions. By default, depending on the node flags, this array contains actions for viewing, editing and deleting a record. The method is called for each displayed record which also means that the action can differ per record.
A small example:
CODE:
function recordActions($record, &$actions)
{
if ($record['is_ready_for_publishing'] == 1)
{
$actions['publish'] = dispatch_url($this->atkNodeType(), 'publish', array('atkselector' => '[atkselector]'));
}
}
In the example above we add an action
publish for each record which has
1 as value for the
is_ready_to_publish column. If you want to remove an action from the array you can simply call
unset($actions['action']) and if you want to alter an action you simply set the value to something different.
The
recordActions method is well documented, so if this is new for you I recommend reading the following
page. What most people don't know however is that you can also link JavaScript to actions. We can use this for example to implement a confirmation dialog which is shown before the real action is executed. As you might know normally ATK asks you to confirm the deletion of a record in a separate screen. But using the following code we can do it using a simple JavaScript pop-up:
CODE:
function recordActions($record, &$actions)
{
$url = session_url(dispatch_url($this->atkNodeType(), 'delete', array('confirm' => 1, 'atkselector' => '[pk]')), SESSION_NESTED);
$message = 'Do you really want to remove this record?';
$actions['delete'] = "javascript:if (confirm('{$message}')) document.location.replace('{$url}');";
}
This code takes advantage of the fact that the delete handler accepts direct confirmation by adding
confirm=1 to the request URL. As you can see the only thing you need to do if you want to use javascript for your action is prefix the action with
javascript:.
Summary
This is just a small example of what is possible. We already used this in a customer application in which we showed the edit screen for a certain node inside a DHTML popup instead of going to a new screen. In the future we might natively implement this as an option for some of the relations. You are of-course free to use this yourself for whatever reason possible.