Posted by mikedrupalfan on November 19, 2012 at 3:43am
Hi,
this is a use of a hook_form,
is it possible to set arguments?
what is this context variable ?
/**
* Form for configurable Drupal action to beep multiple times
*/
function beep_multiple_beep_action_form($context) {
$form['beeps'] = array(
'#type' => 'textfield',
'#title' => t('Number of beeps'),
'#description' => t('Enter the number of times to beep when this action executes'),
'#default_value' => isset($context['beeps']) ? $context['beeps'] : '1',
'#required' => TRUE,
);
return $form;
}
Comments
That's not hook_form(). If it
That's not hook_form(). If it were, the first value would be a $node object, and you are using the $context as an array, so it's not an object. It looks like it may be a form definition, but it's likely not the full definition, which means you are showing us a random custom function, not a standard Drupal hook, which also means that we won't be able to answer what $context is.
Jaypan We build websites
Is this better :
Is this better :
/**
*Implemenation of hook_action_info().
*/
function beep_beep() {
watchdog('beep', 'Beep!');
}
function beep_action_info() {
return array(
'beep_beep_action' => array(
'type' => 'system',
'label' => t('beep anonyingly'),
'configurable' => FALSE,
'triggers' => array('node_view', 'node_insert', 'node_update', 'node_delete'),
),
'beep_multiple_beep_action' => array(
'type' => 'system',
'label' => t('beep multiple times'),
'configurable' => TRUE,
'triggers' => array('node_view', 'node_insert', 'node_update', 'node_delete'),
),
);
}
/**
* Form for configurable Drupal action to beep multiple times
*/
function beep_multiple_beep_action_form($context) {
$form['beeps'] = array(
'#type' => 'textfield',
'#title' => t('Number of beeps'),
'#description' => t('Enter the number of times to beep when this action executes'),
'#default_value' => isset($context['beeps']) ? $context['beeps'] : '1',
'#required' => TRUE,
);
return $form;
}
function beep_multiple_beep_action_validate($form, $form_state) {
$beeps = $form_state['values']['beeps'];
if (!is_int($beeps)) {
form_set_error('beeps', t('Please enter a whole number between 0 and 10.'));
}
else if ((int) $beeps > 10 ) {
form_set_error('beeps', t('That would be too annoying. Please choose fewer than 10
beeps.'));
}
else if ((int) $beeps < 0) {
form_set_error('beeps', t('That would likely create a black hole! Beeps must be a
positive integer.'));
}
}
function beep_multiple_beep_action_submit($form, $form_state) {
return array(
'beeps' => (int)$form_state['values']['beeps']
);
}
/**
* Configurable action. Beeps a specified number of times.
*/
function beep_multiple_beep_action($object, $context) {
for ($i=0, $i<$context['beeps'], $i++) {
beep_beep();
}
}
/**
* Implements hook_action_info().
*/
function user_action_info() {
return array(
'user_block_user_action' => array(
'type' => 'user',
'label' => t('Block current user'),
'configurable' => FALSE,
'triggers' => array(),
),
);
}
/**
* Implementation of hook_drupal_alter(). Called by Drupal after
* hook_action_info() so modules may modify the action_info array.
*
* @param array $info
* The result of calling hook_action_info() on all modules.
*/
function beep_action_info_alter(&$info) {
if(!in_array("comment_insert", $info['user_block_action']['triggers'])) {
$info['user_block_user_action']['triggers'][] = 'comment_insert';
}
}
/**
* Loads associated objects for comment triggers.
*
* When an action is called in a context that does not match its type, the
* object that the action expects must be retrieved. For example, when an action
* that works on nodes is called during the comment hook, the node object is not
* available since the comment hook doesn't pass it. So here we load the object
* the action expects.
*
* @param $type
* The type of action that is about to be called.
* @param $comment
* The comment that was passed via the comment hook.
*
* @return
* The object expected by the action that is about to be called.
*/
function _trigger_normalize_comment_context($type, $comment) {
switch ($type) {
case 'node':
return node_load(is_array($comment) ? $comment['nid'] : $comment->nid);
case 'user':
return user_load(is_array($comment) ? $comment['uid'] : $comment->uid);
}
}
/**
* Blocks the current user.
*
* @ingroup actions
*/
function user_block_user_action(&$entity, $context = array()) {
if (isset($entity->uid)) {
$uid = $entity->uid;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
}
else {
global $user;
$uid = $user->uid;
}
db_update('users')
->fields(array('status' => 0))
->condition('uid', $uid)
->execute();
drupal_session_destroy_uid($uid);
watchdog('action','Blocked user %name.', array('%name' => $user->name));
}
It does. As I suspected, that
It does. As I suspected, that function is a helper function to something else, in this case, it's a callback function defined in hook_action_info(). Looking at the manual page for actions, it says the following:
Also, please wrap your code in PHP tags in the future so that it is formatted properly. Just pasting it as-is makes it difficult to read.
Jaypan We build websites
Thanks jaypan
Thanks jaypan