Created
May 27, 2012 19:23
-
-
Save jpoesen/2815594 to your computer and use it in GitHub Desktop.
Drupal 6 hook examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_help(). | |
*/ | |
function helloworld_help($path, $arg) { | |
switch ($path) { | |
// Main module help for the helloworld module. | |
case 'admin/help#helloworld': | |
return '<p>' . t('Say hello to the world.') . '</p>'; | |
} | |
} | |
/** | |
* Implements hook_user(). | |
*/ | |
function helloworld_user($op, &$edit, &$account, $category = NULL) { | |
switch ($op) { | |
case 'after_update': | |
drupal_set_message(t('User object updated.')); | |
break; | |
} | |
} | |
/** | |
* Implements hook_nodeapi(). | |
*/ | |
function helloworld_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { | |
drupal_set_message('Node Op: ' . $op); | |
} | |
/** | |
* Implements hook_comment(). | |
*/ | |
function helloworld_comment(&$a1, $op) { | |
drupal_set_message('Comment Op: ' . $op); | |
} | |
/** | |
* Implements hook_block(). | |
*/ | |
function helloworld_block($op = 'list', $delta = '', $edit = array()) { | |
switch ($op) { | |
case 'list': | |
$blocks['helloworld'] = array( | |
'info' => t('Helloworld user block'), | |
); | |
$blocks['configurable-text'] = array( | |
'info' => t('Helloworld: configurable text string'), | |
); | |
return $blocks; | |
case 'configure': | |
$form = array(); | |
if ($delta == 'configurable-text') { | |
$form['helloworld_example_string'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Block contents'), | |
'#size' => 60, | |
'#description' => t('This string will appear in the example block.'), | |
'#default_value' => variable_get('helloworld_example_string', t('Some example content.')), | |
); | |
} | |
return $form; | |
case 'save': | |
if ($delta == 'configurable-text') { | |
variable_set('helloworld_example_string', $edit['helloworld_example_string']); | |
} | |
return; | |
case 'view': | |
switch ($delta) { | |
case 'helloworld': | |
$block['subject'] = t('Helloworld'); | |
$block['content'] = helloworld_block_contents($delta); | |
break; | |
case 'configurable-text': | |
$block['subject'] = t('Title of configurable-text block'); | |
$block['content'] = helloworld_block_contents($delta); | |
break; | |
} | |
return $block; | |
} | |
} | |
/** | |
* A block content function. | |
*/ | |
function helloworld_block_contents($delta) { | |
switch ($delta) { | |
case 'helloworld': | |
global $user; | |
$username = $user->name; | |
$output = t('hello !username', array('!username' => $username)); | |
return $output; | |
case 'configurable-text': | |
$output = variable_get('helloworld_example_string', t('A default value.')); | |
return $output; | |
} | |
} | |
/** | |
* Implements hook_perm(). | |
*/ | |
function helloworld_perm() { | |
return array('muck about with worlds'); | |
} | |
/** | |
* Implements hook_menu(). | |
* | |
* Registers our settings page with Drupal. | |
*/ | |
function helloworld_menu() { | |
$items = array(); | |
$items['admin/settings/helloworld'] = array( | |
'title' => 'Helloworld settings', | |
'description' => 'Settings for helloworld related activities.', | |
'page callback' => 'helloworld_settings_page', | |
'access arguments' => array('muck about with worlds'), | |
'type' => MENU_NORMAL_ITEM, | |
); | |
return $items; | |
} | |
/** | |
* An administrative form for our module. | |
*/ | |
function helloworld_settings_page() { | |
$form = array(); | |
$form['helloworld_worldcount'] = array( | |
'#type' => 'textfield', | |
'#value' => '42', | |
'#title' => t('How many worlds do we want to say hello to?'), | |
'#description' => t('This should probably be an integer value unless you want to say hello to half a world.'), | |
'#weight' => '-1', | |
); | |
//return drupal_render($form); | |
return drupal_render(system_settings_form($form)); | |
} | |
/** | |
* Implements hook_form_alter() | |
*/ | |
function helloworld_form_alter(&$form, $form_state, $form_id) { | |
switch ($form_id){ | |
case 'user_login_block': | |
$form['name']['#description'] = t('Enter your name here'); | |
$form['pass']['#description'] = t('Enter your password here'); | |
$form['submit']['#value'] = t('Log me in now please :-)'); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment