First of all, thanks for the module -- it's saving me a lot of time.

The biggest issue with using it is figuring out the exact syntax to use to populate a particular field. Sometimes it can get really baroque. I was banging my head against the wall trying to figure it out for adding path redirects, and finally hacked the code to put in some debug statements to output the data structures.

All I did was add some drupal_set_message() statements into prepopulate_form_alter() and _prepopulate_get_walk() that dump the parameters if there are any edit parameters.

/**
 * Implementation of hook_form_alter().
 */
function prepopulate_form_alter(&$form, $form_state, $form_id) {

  if (isset($_GET['edit'])) {
  
    // output debugging info
    
    if (isset($_GET['prepopulate-debug'])) {
  		drupal_set_message("prepopulate: form = " . var_export($form,true));
		drupal_set_message("prepopulate: form_state = " . var_export($form_state,true));
		drupal_set_message("prepopulate: form_id = " . var_export($form_id,true));
    }
    
    foreach (array_keys((array)$_GET['edit']) as $getvar) {
      if (element_child($getvar) && !is_null($form[$getvar])) {
        _prepopulate_get_walk($form[$getvar], $_GET['edit'][$getvar]);
      }
    }
  }
}

/**
 * Internal helper to set element values from the $_GET variable.
 *
 * @param &$form
 *   Array. A form element.
 * @param &$getslice
 *   String or array. Value(s) to be applied to the element.
 */
function _prepopulate_get_walk( & $form, & $getslice) {

  if (isset($_GET['prepopulate-debug'])) {
    drupal_set_message("form(element) = " . var_export($form,true));
    drupal_set_message("getslice = " . var_export($getslice,true));
  }
    
  if (is_array($getslice)) {
    if (!is_null($form['#default_value'])) {
      if (!is_array($form['#default_value'])) {
        // Something went wrong so stop here.
        return;
      }
      $form['#default_value'] = array_merge($form['#default_value'], $getslice);
    }
    else {
      foreach (array_keys($getslice) as $getvar) {
        if (element_child($getvar) && is_array($form) && !is_null($form[$getvar])) {
          _prepopulate_get_walk($form[$getvar], $getslice[$getvar]);
        }
      }
    }
  }
  else {
    $form['#default_value'] = $getslice;
  }
}
CommentFileSizeAuthor
#5 prepopulate.zip11.61 KBMadOverlord
#4 prepopulate.module.zip1.25 KBMadOverlord

Comments

MadOverlord’s picture

Grr... meant to preview.

Anyway, this made it much easier to figure out that I needed edit[redirect][redirect] instead of edit[redirect], which a naive reading of the html would suggest was correct, particularly when you look at the page source so the formatting is better.

Humbly suggest you fold it into the next version of the module.

Best,R

bentonboomslang’s picture

Hi, sorry for being a bit slow but is this code designed to output information about all the available codes to alter any given form? If so it could come in really handy for me!

I don't quite understand where I'm supposed to place this code? Does it need it's own module or can I put it in template.php? Also where and how do I print the output of the code to the screen?

Thanks,
Ben

bentonboomslang’s picture

Hi,
I've now worked out a way to display the data structures (I know it's bad practice but I'm temporarily dropping them into the Prepopulated .module file).
My next question would be "How do I use these to tell me what urls to use."
Sorry for the questions!
Ben

MadOverlord’s picture

StatusFileSize
new1.25 KB

No problem.

Here is a patch you can run against the latest development version (6.x-2.x-dev of 2009-Oct-28) that will output the data you need in a much nicer format. It traverses the data structure and outputs the field information. You'll still have to figure out what you need to use, but it's usually pretty obvious.

--- prepopulateOld.module	2009-10-28 16:38:36.000000000 -0400
+++ prepopulate.module	2009-11-23 16:27:30.000000000 -0500
@@ -30,6 +30,13 @@ function prepopulate_form_alter(&$form, 
     parse_str(base64_decode($_REQUEST['pp']), $_REQUEST);
   } 
   if (isset($_REQUEST['edit'])) {
+
+    // output debugging info
+    
+    if (isset($_REQUEST['prepopulate-debug'])) {
+  		drupal_set_message(_prepopulate_debug_walk(&$form,'edit'));
+    }
+
     foreach (array_keys((array)$_REQUEST['edit']) as $requestvar) {
       if (element_child($requestvar) && !is_null($form[$requestvar])) {
         _prepopulate_request_walk($form[$requestvar], $_REQUEST['edit'][$requestvar]);
@@ -39,6 +46,32 @@ function prepopulate_form_alter(&$form, 
 }
 
 /**
+ * Internal helper that dumps out possible edit form values
+ * that might be settable
+ *
+ * @param &$form
+ *   Array. A form element.
+ * @param $prefix
+ *   String. Prefix to add to whatever values we find, passed
+ *   down in the recursive traverse.
+ */
+function _prepopulate_debug_walk( & $form, $prefix) {
+
+  $result = '';
+  
+  foreach ($form as $index => $value) {
+    if (is_array($value) && ($index{0} != '#')) {
+      $result .= _prepopulate_debug_walk(&$value,$prefix . '[' . $index . ']');
+    } elseif ($index == '#type') {
+      $result .= $prefix . ' (' . $value . ')<br />' . "\n";
+    }
+  }
+
+  return $result;
+  
+}
+ 
+/**
  * Internal helper to set element values from the $_REQUEST variable.
  *
  * @param &$form

The output, in the yellow box on the page where messages appear, looks like this:

edit[nid] (value)
edit[vid] (value)
edit[uid] (value)
edit[created] (value)
edit[type] (value)
edit[language] (value)
edit[changed] (hidden)
edit[title] (textfield)
edit[body_field][teaser_js] (textarea)
edit[body_field][teaser_include] (checkbox)
edit[body_field][body] (textarea)
... and so on...

I've also included the actual module, which you can drop into the prepopulate folder as a replacement for the original file; again, make sure you're running the latest development version before doing this!!!

MadOverlord’s picture

StatusFileSize
new11.61 KB

Ah, damn. Don't use the version above. There's something weird going on when I try and modify the development version.

Here's a hacked version of the current release version that seems to do the job. Keep a backup of the release version to slot back in if I've messed up again. Sacrificing virgins is optional.

Best,R

bentonboomslang’s picture

Wow. Thanks very much for this. I have fixed my problem now. It turns out I was using the right code but for some reason it wasn't working. I tried deactivating the "Use on registration" box and reactivating it again and for some reason now the same url does work.
I don't have any idea why this may have been but as long as it is OK now I'm happy!

Ben

MadOverlord’s picture

One headsup - this debugging output apparently does not give info about multiple-entry fields such as images and links -- the kind of thing where there's a button to add more fields, and you can drag the fields around. These are internally implemented in a different way and my walk routine doesn't find them.

However, in this situation you can just look at the raw html and find the info pretty easily, it'll end up being something like "edit[field_reviews][0][title]", "edit[field_reviews][1][title]", etc.

TimelessDomain’s picture

if this could become a sub-module, that would be great. maybe have beautytips pop-ups appear next to each field. the pop-up would provide the exact code needing to map to that field.

scuba_fly’s picture

Status: Active » Closed (outdated)