1

I have built a custom module named 'ABC'. I have created a content type 'CT' having some text fields. When a user add content of type 'CT' and Save/Preview the form, I want to use the values that the user added in the form and make a web service call and update the form values based on the data returned from the web service call which would further be saved to database.

I am using hook_node_submit() as below.

function CT_node_submit($node, $form, &$form_state) {
  global $user;
  //dpm($user);
  $nameofuser = $user->name;
  $timezone = $user->timezone;
  $title = $form['field_title']['#value'];
  $pid = $user->uid;
  $pname = $user->name;
  $rurl = $form['field_recording_url']['#value'];
  $purl = $form['field_presenter_url']['#value'];

  $kdetails  = array (
     'nameofuser' => $nameofuser,
'title' => $title,
'pid' => $pid,
'pname' => $pname,
  );
  Make_A_WebService_Call($kdetails, $form, $form_state);
}

The problem is that when I am calling the above code from ABC.module file, the code is not being called on clicking the Save/Preview button.

2 Answers 2

2

hook_node_submit() will only be invoked for the module implementing the associated content type. If module A defines the content type, module B cannot implement hook_node_submit() on module A's behalf, unless it specifically calls the function module_B_node_submit().

If you want to hook into the validation process of a node form you can use the standard hook_form_alter(), e.g.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'article_node_form') {
    $form['#validate'][] = 'MYMODULE_node_form_validate';
  }
}

function MYMODULE_node_form_validate($form, &$form_state) {
  // Logic here...
}
3
  • So, Can I use CT_ABC_Node_Submit() as suggested by you ? Commented Aug 28, 2013 at 13:29
  • No, you need to use hook_form_alter() (see the answer above). Oh wait, is the name MYMODULE_node_form_validate causing confusion? That's just a randomly named function, the name has no significance in terms of hooks/being automatically called or anything like that. You can substitute that for whatever you want, as long as you update the string reference to it in the form alter
    – Clive
    Commented Aug 28, 2013 at 13:31
  • 2
    @AnubhavSharma MYMODULE is just a generic replacement for the module short name; if your module is my_nice_module.module, then replace MYMODULE with my_nice_module.
    – avpaderno
    Commented Aug 28, 2013 at 13:53
1

In short, you are saying that in the ABC.module you are implemented the CT content type, and you are wondering why ABC_node_submit() (the implementation of hook_node_submit()) is not being invoked.

If ABC_node_submit() is a function contained in ABC.module, then it will be invoked only in the case, the implementation of hook_node_info() is similar to the following one.

function abc_node_info() {
  return array(
    'ct' => array(
      'name' => t('the content type name'),
      'base' => 'ct',
      'description' => t('the content type description'),
    ),
  );
}

(I use lowercase letters since it is preferable that the short name of a module uses only lowercase letters.)

In this case, when invoking hook_node_submit() for that content type, Drupal will look for abc_node_submit().

In fact, in the documentation for hook_node_info(), base is described as follows:

The base string used to construct callbacks corresponding to this node type (for example, if base is defined as example_foo, then example_foo_insert() will be called when inserting a node of that type). This string is usually the name of the module, but not always.

This answer is valid if ABC_node_submit() is a function contained in the ABC.module. If you have two modules (ABC.module and CT.module) where ABC.module is implementing the content type, and CT.module is another module that needs to add a form validation or a form submission handler to the form used to edit a node of that content type, then see Clive's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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