I am new to drupal development. I tried the sample in chapter 3 in Pro Drupal Development Second Edition (apress publishing, John K. VanDyk). The action should block the user after inserting a comment. I created a folder for the action with the following path:
D:\xampp\htdocs\drupal_610\sites\all\modules\custom\user

The action module has be activated automatically. When I click any menu item, I get the following error:

--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
/** * Implementation of hook_action_info(). */ function user_action_info() { return array( 'user_block_user_action' => array( 'description' => t('Block current user'), 'type' => 'user', 'configurable' => FALSE, 'hooks' => array(), ), 'user_block_ip_action' => array( 'description' => t('Ban IP address of current user'), 'type' => 'user', 'configurable' => FALSE, 'hooks' => 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) { // Make the "Block current user" action available to the // comment insert trigger. If other modules have modified the // array already, we don't stomp on their changes; we just make sure // the 'insert' operation is present. Otherwise, we assign the // 'insert' operation. if (isset($info['user_block_user_action']['hooks']['comment'])) { array_merge($info['user_block_user_action']['hooks']['comment'], array('insert')); } else { $info['user_block_user_action']['hooks']['comment'] = array('insert'); } } /** * Implementation of a Drupal action. * Blocks the current user. */ function user_block_user_action(&$object, $context = array()) { if (isset($object->uid)) { $uid = $object->uid; } elseif (isset($context['uid'])) { $uid = $context['uid']; } else { global $user; $uid = $user->uid; } db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $uid); sess_destroy_uid($uid); watchdog('action', 'Blocked user %name.', array('%name' => check_plain($user->name))); }
Fatal error: Call to undefined function user_access() in D:\xampp\htdocs\drupal_610\includes\menu.inc on line 449

--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------

I have only created 2 files (in D:\xampp\htdocs\drupal_610\sites\all\modules\custom\user) for the custom action: user.infoHere and user.module. Here is the code for my custom action:
1) user.info
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
; $Id$
name = Block
description = Blocks a user not authorized.
package = Pro Drupal Development
core = 6.x
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------

2) user.module
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------

/**
* Implementation of hook_action_info().
*/
function user_action_info() {
return array(
'user_block_user_action' => array(
'description' => t('Block current user'),
'type' => 'user',
'configurable' => FALSE,
'hooks' => array(),
),
'user_block_ip_action' => array(
'description' => t('Ban IP address of current user'),
'type' => 'user',
'configurable' => FALSE,
'hooks' => 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) {
// Make the "Block current user" action available to the
// comment insert trigger. If other modules have modified the
// array already, we don't stomp on their changes; we just make sure
// the 'insert' operation is present. Otherwise, we assign the
// 'insert' operation.
if (isset($info['user_block_user_action']['hooks']['comment'])) {
array_merge($info['user_block_user_action']['hooks']['comment'], array('insert'));
}
else {
$info['user_block_user_action']['hooks']['comment'] = array('insert');
}
}

/**
* Implementation of a Drupal action.
* Blocks the current user.
*/
function user_block_user_action(&$object, $context = array()) {
if (isset($object->uid)) {
$uid = $object->uid;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
}
else {
global $user;
$uid = $user->uid;
}

db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $uid);
sess_destroy_uid($uid);
watchdog('action', 'Blocked user %name.', array('%name' => check_plain($user->name)));
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------

Can anybody tell me what is wrong with this code. Pleas notice that I copy the sample code from the book Pro Drupal Development Second Edition (apress publishing, John K. VanDyk) as stay above.

I will be very thankful for your help.

Thanks in advance.

Comments

scrypter’s picture

module called "user" so this may be a bad choice of name. Conflicts are likely. I am not absolutely sure about this, but would recommend trying with a new module name, eg my_user. The undefined function "user_access" is in the core user module so it looks like you new module of the same name is causing strife.

www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy

drup-it’s picture

Thank you verry much for trying to help me. The idea behind this sample is, how to override drupal action. So the name of the action shouldn't be a problem and as told early in my post it as a sample from the book Pro Drupal Development Second Edition.

Again thank you for trying to help help me.

scrypter’s picture

the function user_action_info() is the hook_action_info within the "user" module. This is the naming convention used by Drupal. When you see an example of a hook_something function the word hook is replaced by the module name. It is fundamental to how Drupal works and enables easy changes to nearly everything by module. I have installed over 20 Drupal sites and every one would have had at least a site specific module using the hook_form_alter(), it is so useful. Be aware there is a hook_user() function also which is for dealing with users, eg my_module_user(). It OK to use the word "user" like this but not in place of "hook".

Please try renaming your module, it will fix your problem. I have the same book, that edition is for Drupal 5 but the hook system is the same in D6.

Good luck.

www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy

drup-it’s picture

really thank you for your response. I renamed the hook to my_user but I same the same error message but without the code appeared before renaming the hook. Here is the changed code:
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
/**
* Implementation of hook_action_info().
*/
function my_user_action_info() {
return array(
'my_user_block_user_action' => array(
'description' => t('Block current user'),
'type' => 'user',
'configurable' => FALSE,
'hooks' => array(),
),
'my_uuser_block_ip_action' => array(
'description' => t('Ban IP address of current user'),
'type' => 'user',
'configurable' => FALSE,
'hooks' => 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) {
// Make the "Block current user" action available to the
// comment insert trigger. If other modules have modified the
// array already, we don't stomp on their changes; we just make sure
// the 'insert' operation is present. Otherwise, we assign the
// 'insert' operation.
if (isset($info['my_user_block_user_action']['hooks']['comment'])) {
array_merge($info['my_user_block_user_action']['hooks']['comment'], array('insert'));
}
else {
$info['my_user_block_user_action']['hooks']['comment'] = array('insert');
}
}

/**
* Implementation of a Drupal action.
* Blocks the current user.
*/
function my_user_block_user_action(&$object, $context = array()) {
if (isset($object->uid)) {
$uid = $object->uid;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
}
else {
global $user;
$uid = $user->uid;
}

db_query("UPDATE {users} SET status = 0 WHERE uid = %d", $uid);
sess_destroy_uid($uid);
watchdog('action', 'Blocked user %name.', array('%name' => check_plain($user->name)));
}

--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------

I renamed also the files to myuser.info and myuser.module.

Here is the error message appears after renaming the hook:

Fatal error: Call to undefined function user_access() in D:\xampp\htdocs\drupal_610\includes\menu.inc on line 449

Thanks again & best regards

scrypter’s picture

from the performance menu. It will rebuild the menu.

www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy

drup-it’s picture

Drupal does not work any more

Thank you. But Drupal does not work any more, even after deleting the module but Drupal does even not work. Did you have any idea, what is the reason and how to fix it!!!

scrypter’s picture

Look in the apache and php error logs. You should not just delete a module by removing the folder because Drupal still has it in the database. If you did remove the folder, go into the "system" table and remove the record for my_user or whatever it was called. There could be many reasons Drupal does not work, but try these first. You may have to re-install and start over!

www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy

jaypan’s picture

You probably changed the name of your module without first uninstalling it. That's going to screw everything up, as Drupal is still looking for your module and not finding it. Re-install Drupal from scratch, and work with your re-named module in the fresh environment.

Contact me to contract me for D7 -> D10/11 migrations.

metaltoad’s picture

Agreed, Jay. If anyone's gotten to the point where Drupal doesn't work anymore, it's best to start from a fresh install.

jaypan’s picture

Particularly in a case where it's just a sandbox testing ground (I actually have a Drupal installation that I use as a sandbox - I wipe it clean periodically and turn it into a fresh install. I do all my development there.

Contact me to contract me for D7 -> D10/11 migrations.

drup-it’s picture

Thank all of you for your help!