Porting an "old-style" (5.x-1.x) Action to a "new-style" (5.x-2.x) Action
Last modified: June 8, 2008 - 00:50
This document will analyze the difference between NodeQueue module's 1.x actions.inc and 2.x actions.inc in order to give guidelines on how to port "old-style" Actions to "new-style" actions.
$op 'metadata' is now an info hook
Old-style actions checked the $op parameter for case 'metadata' and then returned data about the action:
<?php
function action_nodequeue_add($op, $edit = array(), $node) {
switch($op) {
case 'metadata':
return array(
'description' => t('Add to Node Queues'),
'type' => t('node'),
'batchable' => true,
'configurable' => true,
);
break;
// ...
}
?>In new-style actions, hook_action_info() is used:
<?php
/**
* Implementation of hook_action_info().
*/
function nodequeue_action_info() {
return array(
// nodequeue_add_action() is the function triggered when this action runs.
'nodequeue_add_action' => array(
// This action acts on nodes. Other values might be 'cron' or 'user'.
'type' => 'node',
// A simple text description that will show up in admin/build/actions.
'description' => t('Add to Node Queues'),
// Whether or not advanced configuration options are available. If so,
// there must be a function defined that's {action_callback}_form()
// (nodequeue_add_action_form() in this case).
'configurable' => TRUE,
// The hooks array indicates which places during the page lifecycle
// should trigger this action. This action will trigger after a node is
// added or edited.
'hooks' => array(
'nodeapi' => array('insert', 'update'),
),
),
// More of the same here; but this time for removing node queues.
'nodequeue_remove_action' => array(
'type' => 'node',
'description' => t('Remove from Node Queues'),
'configurable' => TRUE,
'hooks' => array(
'nodeapi' => array('insert', 'update'),
),
),
);
}
?>$op case 'do' becomes action callback
The action callback specified in hook_action_info() is the new 'do' $op.
Old-style:
<?php
function action_nodequeue_add($op, $edit = array(), $node) {
switch ($op) {
// ...
case 'do':
$queues = nodequeue_load_queues($edit['qids']);
// Snip all the logic that should be performed when the action runs...
foreach ($eligible_subqueues as $subqueue) {
nodequeue_subqueue_add($queues[$subqueue->qid], $subqueue, $node->nid);
}
break;
}
}
?>New-style:
<?php
function nodequeue_add_action($node, $context) {
$queues = nodequeue_load_queues($context['qids']);
// Snip all the logic that should be performed when the action runs...
foreach ($eligible_subqueues as $subqueue) {
nodequeue_subqueue_add($queues[$subqueue->qid], $subqueue, $node->nid);
}
}
?>$op case 'form', 'validate', and 'submit' are now regular functions
If your action was flagged as 'configurable' => TRUE, in old-style actions you would use $op 'form' and 'submit' to handle the configure form:
<?php
function action_nodequeue_add($op, $edit = array(), $node) {
switch ($op) {
// ...
case 'form':
// Snipped form definition logic....
// Define elements to show on settings form.
$form['qids'] = array(
'#type' => 'select',
'#title' => t("Queue"),
'#default_value' => $edit['qids'],
'#multiple' => TRUE,
'#options' => $options,
'#required' => TRUE,
'#description' => t('Specify the queues into which the node should be submitted. If the queue is a smartqueue, the node shall be placed into every subqueue for which it is eligible.')
);
return $form;
case 'submit':
// Define array of parameters to send back.
$params = array(
'qids' => $edit['qids']
);
return $params;
break;
}
?>In new-style actions, this is transformed into the following:
<?php
/**
* Configuration form for Add to Node Queues action.
*/
function nodequeue_add_action_form($context) {
// Snipped form definition logic....
// Add form components.
$form['qids'] = array(
'#type' => 'select',
'#title' => t("Queue"),
'#default_value' => $edit['qids'],
'#multiple' => TRUE,
'#options' => $options,
'#required' => TRUE,
'#description' => t('Specify the queues into which the node should be submitted. If the queue is a smartqueue, the node shall be placed into every subqueue for which it is eligible.')
);
return $form;
}
/**
* Submit handler for Add to Node Queues action configuration.
*/
function nodequeue_add_action_submit($form_id, $form_values) {
// As before, returning $params.
return array(
'qids' => $form_values['qids'],
);
}
?>