A hook not working? Am I missing something here?

esend7881 - January 5, 2009 - 05:11

Hey Everyone,

I am trying to display the advanced search a little differently than the drupal core shows it.

There is a function in node.module:

function node_form_alter(&$form, $form_state, $form_id)

which controls this.

I am trying to create a function in my template.php file called

function mytheme_form_alter(&$form, $form_state, $form_id)

But it does nothing. Drupal doesn't recognize it at all. I was under the impression that the hook system worked by creating a function with your theme name, then the core function's name, analogous to what I did above.

My "mytheme" is the default theme and yes it is running. All other aspects of mytheme are running fine. Its just this that Drupal doesn't acknowledge it.

So what am I doing wrong? Am I missing a certain ingredient that allows these hook functions to activate (I could be using the "hook" terminology wrong here, I am a bit new to Drupal development).

Thanks

hooks are simply functions

nevets - January 5, 2009 - 05:25

hooks are simply functions in a module that follow a name convention. So for example node_form_alter is an implementation of hook_form_alter by the node module. While you can not override a modules hooks you can write your own module with it's own set of hooks.

Theme functions are something different. The base function is called theme_something() and called from code by theme('something', ...);. Theme functions can be overridden in a number of ways and can also map to a tpl.php file.

Follow up...

esend7881 - January 5, 2009 - 07:23

Nevets, thanks for the reply. In it, you said:
While you can not override a modules hooks you can write your own module with it's own set of hooks.
So, with that said, how would one "replace" the function node_form_alter()?

I thought I read somewhere you can replace the way drupal core functions operate (and still not edit drupal's core). Maybe I'm confused and this has nothing to do with hooks.

Either way, I would like to change the way node_form_alter() operates without changing drupal's core. How would I do that?

Thanks again

You can not really replace

nevets - January 5, 2009 - 16:27

You can not really replace it. You could write a module (pick a name that comes alphabetically after node) that implements hook_form_alter and modifies/undoes/adds to what node_form_alter does.

You can not really replace

esend7881 - January 6, 2009 - 00:27

You can not really replace it. You could write a module (pick a name that comes alphabetically after node) that implements hook_form_alter and modifies/undoes/adds to what node_form_alter does.

But wasn't that exactly what I was doing before? I create a new function called mytheme_form_alter() meant to replace node_form_alter() but it didn't work...

I know you said write a module but I guess I don't see the difference here... aren't modules just function you include in your .tpl files? (again I am a newbie here trying to absorb a lot of stuff at once... I really appreciate your help, I love the open source community :))

Theming a form

kovalev - January 5, 2009 - 16:43

I got it that you are using Drupal 6, so this is what you are looking for: http://api.drupal.org/api/file/modules/search/search-theme-form.tpl.php
If it's not clear enough, feel free to ask for help. Anyway, If all you want is to display the form differently, there is no need for custom modules.

Sergata - פיתוח תוכנה

this is what you are looking

esend7881 - January 6, 2009 - 00:30

this is what you are looking for: http://api.drupal.org/api/file/modules/search/search-theme-form.tpl.php

You know, my partner tried that also and nothing happened. It was like the file wasn't even there.

How exactly do you use that .tpl.php file in a theme?

I am sure I am missing some simple piece of code here... I just am not sure what.

Cache! Clear the cache

kovalev - January 6, 2009 - 12:35

Drupal has it's own inner theme cache. After you make a change in a theme, go to:
/admin/settings/performance ,
in the bottom of the page push "Clear cached data"

------------------------------------------------------------------------------------------------------------------------------------
Sergata - פיתוח תוכנה

Drupal has it's own inner

esend7881 - January 6, 2009 - 17:54

Drupal has it's own inner theme cache. After you make a change in a theme, go to:
/admin/settings/performance ,
in the bottom of the page push "Clear cached data"

I did that though... it didn't work.

Weird.

Example?

esend7881 - January 6, 2009 - 18:28

Is there any example, or guide, that will explain step-by-step how to override a default/core drupal functionality?

I see a lot of philosophical guides describing all these functions and hooks, but nothing concrete. No examples.

Perhaps I just can't find it. Do any of you know of examples?

What exactly are you trying to change?

gforce301 - January 6, 2009 - 19:23

It is hard to give/make examples for general questions. Many times it depends on what you are trying to change. You mention in your OP that you want to change the way the advanced search form displays, but what are you trying to change about it's display? The reason there are no concrete examples is because you have not asked a concrete question. In order to help you do something specific we need to know specifically what you are trying to do.

hook_form_alter is used by modules to make changes to a form (structure, elements, etc...) before the form is rendered by the form api. hook_form_alter is not used by themes. That is why your implementation of it in your template.php file is "ignored". The theme engine does not invoke hooks. If you are not trying to add, remove, reorder elements of the form then you do not need to use a hook_form_alter implementation at all.

Furthermore, module hooks are not overridable specifically in the way that you seem to be thinking. You can not simply replace a modules implementation of a hook with a new version, unless you rewrite the module (<-- CAUTION don't do this). Hooks do not operate in this way. A hook is meant to extend the core functionality. Read here --> Hooks.

All enabled modules that implement a hook will have there hook executed by the core, at the proper time, in order. Order of execution depends on two things: the weight of the module, and it's name (if all modules have the same weight, then execution order is alphabetically).

gforce, thanks for the

esend7881 - January 6, 2009 - 20:18

gforce, thanks for the reply

Specifically I am trying to make the advanced search options be expanded instead of collapced.

Here is the code:

<?php
function node_form_alter(&$form, $form_state, $form_id) {
 
// Advanced node search form
 
if ($form_id == 'search_form' && $form['module']['#value'] == 'node' && user_access('use advanced search')) {
   
// Keyword boxes:
             
if (arg(0) == 'search' && arg(1) == 'node') {
   
$form['advanced'] = array(
     
'#type' => 'fieldset',
     
'#title' => t('Advanced search'),
     
'#collapsible' => TRUE,
     
'#collapsed' => TRUE,
     
'#attributes' => array('class' => 'search-advanced'),
    );           
            } else {
   
$form['advanced'] = array(
     
'#type' => 'fieldset',
     
'#title' => t('Advanced search'),
     
'#collapsible' => TRUE,
     
'#collapsed' => FALSE,
     
'#attributes' => array('class' => 'search-advanced'),
    );

 
//etc etc Just see node.module for Drupal 6.2
?>

This is from node.module. Notice how I had to change the code with that "if/else" logic?

I do not want to change drupal's core, but I am unsure of how else to implement this modification except here in "node.module"

I am going to re-read your post and read the reference pages you gave.

Thanks
Eric

PS. this part of node.module should look like this:

<?php
/**
* Implementation of hook_form_alter().
*/
function node_form_alter(&$form, $form_state, $form_id) {
 
// Advanced node search form
 
if ($form_id == 'search_form' && $form['module']['#value'] == 'node' && user_access('use advanced search')) {
   
// Keyword boxes:
   
$form['advanced'] = array(
     
'#type' => 'fieldset',
     
'#title' => t('Advanced search'),
     
'#collapsible' => TRUE,
     
'#collapsed' => TRUE,
     
'#attributes' => array('class' => 'search-advanced'),
    );

 
//etc etc
?>

I think I got it! I simply

esend7881 - January 6, 2009 - 20:28

I think I got it!

I simply added the hook to a module, instead of a template.php and it seemed to do the trick.

So if I understand, if we create a module and it has a hook meant to replace a drupal core function, the newly created module always has higher "weight"?

Thanks a lot

Not exactly so.

gforce301 - January 7, 2009 - 21:33

So if I understand, if we create a module and it has a hook meant to replace a drupal core function, the newly created module always has higher "weight"?

First (for clarification), hooks we write do not "replace" core hooks, they extend them. This means basically that our hooks also execute as well as the core hooks. In other words, all modules (core and custom) that have a hook_form_alter will have their hook executed. The hook_form_alter in node.module still executes, it is not overriden by one in another module. The idea behind this is: each module gets the chance to make "alterations" to a form even if another module has already made alterations to it or will make alterations to it afterwards.

The order in which module hooks are executed is alphabetical by module name for modules of the same weight. If we name our module "a.module" it would execute it's hooks before node.module, name it "z.module" and they execute after. Module weight is used to effect order of execution. Most modules have a weight of "0" so they execute in alphabetical order. If we named our module "z.module" but gave it a weight of "-5" then it would execute before node.module because it is "lighter" (smaller) than the weight of node.module. The weight of a module is stored in the system table in the database and is usually set (if needed) in hook_install.

Start here --> Module developer's guide to learn more about modules and how to write them.

 
 

Drupal is a registered trademark of Dries Buytaert.