0

I made a test module and only a few of my hooks seem to get called. I get the Javascript console messages for the block_view, form_alter and form_user_login_alter hook functions when I do the corresponding actions. However, when I login, logout, view a node, create a node or delete a node I get nothing in the console. Shouldn't these hooks get called when I do those things? I reset my cache, disabled and enabled my module multiple times. Here is the code for test_module.module:

<?php

function console_log ($stringIn) {
    print "<script>console.log('" . $stringIn . "');</script>";
}
function test_module_help ($path, $arg) {
    switch($path) {
        case "admin/help#test_module":
            return '<p>' . t("This is just a test module right now.") . '</p>';
            break;
    }
}
function test_module_insert ($node) {
    console_log("node inserted");
}
function test_module_user_login_submit (&$form, &$form_state) {
    console_log("login submitted");
}
function test_module_block_view ($delta = '') {
    console_log("block viewed");
}
function test_module_user_login (&$edit, $account) {
    console_log("user logged in");
}
function test_module_user_logout ($account) {
    console_log("user logged out");
}
function test_module_view ($node, $view_mode) {
    console_log("node viewed");
}
function test_module_form_alter (&$form, $form_state, $form_id) {
    console_log("form altered");
}
function test_module_delete ($node) {
    console_log("node deleted");
}
function test_module_form_user_login_alter(&$form, &$form_state, $form_id) {
    console_log("user login form altered");
}

Here is the test_module.info file:

name = Test Module
description = just a test module
package = ZZ Test category
core = 7.x

Those two files are in sites/all/modules/test_module/

What am I doing wrong?

0

I'm surprised doing a 'print' works at all and does not produce a headers already sent error.
Your hooks may well be being called.
Instead of calling your own console.log function, install the devel module and call dpm('my message');
It will give you a much better idea of what hook is and is not being called.

eg. (your module with devel enabled).

function test_module_help ($path, $arg) {
    switch($path) {
        case "admin/help#test_module":
            return '<p>' . t("This is just a test module right now.") . '</p>';
            break;
    }
}
function test_module_insert ($node) {
    dpm("node inserted");
}
function test_module_user_login_submit (&$form, &$form_state) {
    dpm("login submitted");
}
function test_module_block_view ($delta = '') {
    dpm("block viewed");
}
function test_module_user_login (&$edit, $account) {
    dpm("user logged in");
}
function test_module_user_logout ($account) {
    dpm("user logged out");
}
function test_module_view ($node, $view_mode) {
    dpm("node viewed");
}
function test_module_form_alter (&$form, $form_state, $form_id) {
    dpm("form altered");
}
function test_module_delete ($node) {
    dpm("node deleted");
}
function test_module_form_user_login_alter(&$form, &$form_state, $form_id) {
    dpm("user login form altered");
}
8
  • Much better. I'm still a little confused after testing each function. After logging in, logging out, creating a node, deleting a node and loading a node with a block in it, here are my results: – Jason Feb 1 '15 at 3:51
  • There is a permission the see the dpm messages. By default is is only user 1 I think. So if you are not logged in you will not see the messages unless you change the permission to let anonymous users see the messages. – 2pha Feb 1 '15 at 3:55
  • The ones that still don't show: hook_login_submit, hook_login_alter, hook_logout, hook_insert, hook_view, hook_delete. So the logout, login submit and login alter ones don't show because of permissions. That makes sense. But I don't understand why hook_insert, hook_view and hook_delete don't show anything. I read the docs for those but I don't understand how they are different from the hook_node_ ones. – Jason Feb 1 '15 at 4:02
  • As far as I can see, your hooks should be being called. Try calling watchdog instead of dpm. eg watchdog('my module', 'insert');. Thn check your watchdog logs. – 2pha Feb 1 '15 at 7:16
  • I think I understand everything that's going on now. The only login or logout hook that makes a dpm message show up is user_login because my admin account is logged out or not finished logging in when I call dpm in the other hooks. Also, I found a better explanation about hook_view and hook_insert than I saw in the docs. Apparently, and please let me know if I haven't quite got this right yet, hook_view and hook_insert will only work on nodes whose type is defined in the module in which I am declaring the hooks. I will try out watchdog, too. Thanks for all of your help, I was really struggling – Jason Feb 1 '15 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.