how to create a action and hook

/**
  how to write our own action. this action have those functions.
  1. tell the drupal. this action support which trigger
  2. creat a action function
*/
//action_info is the hook's name
//system mean can use everywhere
//configuration mean whether we can get the paramter from another places
function beep_action_info(){
   $info['beep_beep_action']= array(
     'type'=>'system',
     'description'=>'t("Beep"),
     'configurable'=>FALSE,
     'hooks' => array(
'nodeapi' => array('view', 'insert', 'update', 'delete'),
'comment' => array('view', 'insert', 'update', 'delete'),
'user' => array('view', 'insert', 'update', 'delete', 'login'),
'taxonomy' => array('insert', 'update', 'delete'),
    );
   return $info;
}
//this mean this action can use any hooks
function beep_action_info(){
   $info['beep_beep_action']= array(
     'type'=>'system',
     'description'=>'t("Beep"),
     'configurable'=>FALSE,
     'hooks' => array(
'any'=>TRUE
    );
   return $info;
}
// create a form that can store many time
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_numeric($beeps)) {
        form_set_error('beeps', t('Please enter a numeric value.'));
    }
    else if ((int) $beeps > 10) {
form_set_error('beeps', t('That would be too annoying. Please choose fewer than 10 beeps.'));
    }
}
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 = 1; $i < $context['beeps']; $i++) {
beep_beep();
}
}

你可能感兴趣的:(action)