I tried implementing hook_draft but my form was not being modified. I noticied on the Drupal API docs http://api.drupal.org/api/function/module_invoke this comment:

It is useful to realize that the args passed to invoked functions are not passed by reference with calls to module_invoke() (or to module_invoke_all(), for that matter).

I tried passing the $form argument as &$form to module_invoke but that didn't work.

This patch worked for me instead:

Index: draft.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/draft/draft.module,v
retrieving revision 1.2.2.41
diff -u -b -r1.2.2.41 draft.module
--- draft.module	15 Mar 2010 21:17:03 -0000	1.2.2.41
+++ draft.module	12 Oct 2010 20:51:22 -0000
@@ -137,7 +139,8 @@
           // This functionality is here specifically for modules that utilize AJAX to create widgets in the form once it
           // Is already loaded in the system so that those fields can be pre-created and then will be populated by the module
           foreach (module_implements('draft') as $module) {
-            module_invoke($module, 'draft', 'form elements', $_GET['draft_id'], unserialize($result->data), $form);
+            //module_invoke($module, 'draft', 'form elements', $_GET['draft_id'], unserialize($result->data), $form);
+            call_user_func($module.'_draft', 'form elements', $_GET['draft_id'], unserialize($result->data), &$form);
           }
         }
         // button to save the node as a draft for later use

Comments

darren.ferguson’s picture

Status: Active » Closed (works as designed)

If you look at the API.txt you will see the hook_draft function takes the form &$form. But in drupal you do not pass by reference this way but the function you are creating that is implementing the hook_draft should have the &$form in it. i.e.

function tmp_draft($op, $draft_id, $draft_data = array(), &$form) {
// Any changes to $form in here will be seen in the draft module
}

You do not however pass &$form too the module invoke since this was stated as not the correct way to do it in the documents.

darrick’s picture

Status: Closed (works as designed) » Active

My hook_draft function I'm using is included below. I copied it straight from the API.txt file. I also used dsm to print the form struct both at the end of my hook where my added fields showed up and then printed right after the module invoke statement where the fields didn't show up. Which AFAICT means my hook is getting called but the form isn't being referenced properly.

So, I tried a number of ways. I did some more poking around and found this: http://cstruter.com/blog/144 which talks about the difficulty of passing references via func_get_args. I passed &$form to module_invoke in hope of getting lucky.

My server info is Apache/2.2.16 (FreeBSD) mod_ssl/2.2.16 OpenSSL/1.0.0a DAV/2 PHP/5.2.14 with Suhosin-Patch maybe the problem is fixed in 5.3.

function merci_template_draft($op, $draft_id, $draft_data = array(), &$form) {
  foreach($draft_data as $key => $item){
    if(preg_match("/^choice\((\d{1,5})\)\(item\)$/",$key,$matches)){
      $delta = $matches[1];
      if($delta > 2){
        $form['choice_wrapper']['choice'][$delta] = _merci_choice_form($node, $form_state, $delta);
      }
    }
  }
}

darren.ferguson’s picture

Your using $form_state but it is never initialized it does not exist, could it be that it throws a null for the return information and hence does not add the information?

darrick’s picture

I checked my call to the _merci_choice_form function. I dumped the $form array at the end of function merci_template_draft to make sure my fields were added. They were. I also dumped the $form array after the module invoke. Which should have been the same array as dumped from the end of my hook. But it didn't include the added fields.

darren.ferguson’s picture

What version of php are you using??? If 5.1 i believe that did not support this however not 100% sure. It should work that way based off the documentation.

darrick’s picture

My server info is Apache/2.2.16 (FreeBSD) mod_ssl/2.2.16 OpenSSL/1.0.0a DAV/2 PHP/5.2.14 with Suhosin-Patch.

darren.ferguson’s picture

Status: Active » Fixed

Issue has been resolved in the module, module_invoke indeed was not the correct way to call the information. Have updated the dev branch in CVS and the fix will be available when the CVS running is run for development branch which is probably going to be tomorrow.

darren.ferguson’s picture

Status: Fixed » Closed (fixed)