'
Tips & Links
- Example hook_node_insert() implementation that notifies site admin of new content.
function myhook_node_insert($node) {
drupal_mail_system(\'myhook\', \'mail\')->mail(array(
\'to\' => \'admin@example.com\',
\'subject\' => \'New content\',
\'body\' => \'New content entered at \' . url(\'node/\' . $node->nid, array(\'absolute\' => TRUE)),
\'headers\' => array(\'From\' => variable_get(\'site_mail\', ini_get(\'sendmail_from\'))),
));
}
- If your code causes a fatal error or if you want to disable it for a single request, just append
?myhook=0
to the URL. Ex: ' . url('admin/config/development/myhook', array('query' => array('myhook' => '0'), 'absolute' => TRUE)) . '
- You may need to flush caches to make some hooks such as
myhook_menu()
or myhook_theme()
to take effect.
- Drupal core hooks
- Views hooks
- Various module hooks
');
return $output;
}
/**
* Returns admin form.
*/
function myhookadmin_admin_form($form, &$form_state) {
$form = array();
$form['code'] = array(
'#title' => 'Content of myhook.module',
'#type' => 'textarea',
'#default_value' => myhookadmin_var_get('myhook_code', ''),
'#rows' => 20,
'#cols' => 60,
'#description' => 'Define your custom hooks as if they are part of myhook.module. Do not include PHP opening and closing tags.',
);
$form['is_inc'] = array(
'#title' => 'Save the code in a file and include it as a file',
'#type' => 'checkbox',
'#default_value' => variable_get('myhookadmin_is_inc', 0),
'#description' => 'This may save a DB hit and an eval execution on each request as the code will be saved in a file whose path must be defined below.',
);
$form['inc_path'] = array(
'#title' => 'File path to include',
'#type' => 'textfield',
'#size' => 60,
'#default_value' => variable_get('myhookadmin_inc_path', ''),
'#description' => 'This must be a PHP-writable file path such as public://myhook.inc. It will be made readonly automatically after writing.',
);
$weight = db_query("SELECT weight FROM {system} WHERE name = 'myhook'")->fetchField();
$sql = "SELECT name, weight FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight";
$min = db_query_range($sql, 0, 1)->fetchObject();
$max = db_query_range($sql . ' DESC', 0, 1)->fetchObject();
$form['weight'] = array(
'#title' => 'Myhook module weight',
'#type' => 'textfield',
'#size' => 60,
'#maxlength' => 4,
'#default_value' => $weight,
'#description' => 'Currently Min: ' . check_plain($min->name) . '(' . $min->weight . '), Max: ' . check_plain($max->name) . '(' . $max->weight . ')',
);
$form['vars'] = array(
'#title' => 'Add/edit/delete a myhook variable',
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['vars']['varname'] = array(
'#title' => 'Variable name',
'#type' => 'textfield',
'#size' => 60,
'#maxlength' => 128,
'#default_value' => '',
);
$form['vars']['vardelete'] = array(
'#title' => 'Delete this variable',
'#type' => 'checkbox',
'#default_value' => 0,
);
$form['vars']['varvalue'] = array(
'#title' => 'Variable value',
'#type' => 'textarea',
'#default_value' => '',
'#rows' => 10,
'#cols' => 60,
'#description' => 'The variables are stored in table {myhookadmin} and can be managed by myhookadmin_var_get("variable_name", "optional_default_value")
and myhookadmin_var_set("variable_name", "value")
.',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
$form['#validate'][] = 'myhookadmin_admin_validate';
$form['#submit'][] = 'myhookadmin_admin_submit';
return $form;
}
/**
* Validates admin form.
*/
function myhookadmin_admin_validate($form, &$form_state) {
$is_inc = $form_state['values']['is_inc'];
$inc_path = $form_state['values']['inc_path'];
$code = $form_state['values']['code'];
if (substr($code, 0, 5) == 'fields(array('weight' => $weight))->condition('name', 'myhook')->execute();
// Save/delete myhook variable
if (!empty($varname) && $varname != 'myhook_code') {
if ($vardelete) {
$varvalue = NULL;
drupal_set_message('The ' . check_plain($varname) . ' variable has been deleted.');
}
myhookadmin_var_set($varname, $varvalue);
}
drupal_set_message('The changes have been saved.');
}