I've got $_SESSION['my_module']['webform_nid']['webform_sid'] type data.

When I try to use %session['my_module'][10][10] for the default value of a field, the value ends up being "Array[10][10]". Looks like it's only getting the first dimension of an array.

Is this a token.module issue? Do I need rethinking with my use of $_SESSION? If neither, please fix :)

Comments

lefnire’s picture

or alternatively, could provide a text-area for php-determined default-values... maybe in the component's advanced fieldset or whatnot.

lefnire’s picture

Title: %session doesn't work with multi-dimensional array » Custom PHP for Default Values
Category: support » feature

scrap my %session comment, I think a custom-php box for default values would be really useful. Doing some pretty advanced pre-population stuff on forms, and I've been exposing a bunch of data in $_SESSION in order to do it in PHP.

fabio61’s picture

Here is a solution. It is some source from my app. (5.x.2.2) . You can use php tag in default value.

1. This is a patch (or hack) what you have to insert into the webform.module file in the newest version from line No. 1920.

$string = str_replace($find, $replace, $string);

  // Clean up any unused tokens.
  foreach (array_keys($tokens) as $token) {
    $string = preg_replace('/\\'. $token .'\[\w+\]/', '', $string);
  }
  /************ FZ ********************* line 1920 /
  if(stripos($string, '<?php') !== FALSE){
    $string = str_ireplace('<?php','',$string);
    $string = str_ireplace('?>','',$string);
    $string = eval('return '.$string);
  }
  /************ end ********************/
  if ($strict) {
    return filter_xss($string);
  }
  else {
    return $string;
  }
} 

2. Choose the Webform Edit / Form Coponents / Something Component / Edit.
In the default value field you can use php code, calling a function, for example:

<?php foo("%get[name]"); ?>

If you use the select component, you can call a function from the settings field, and the function returns with the right string...

<?php listofgroup(); ?>

3. You have to call these functions from an included file. I include this file in the index.php after the bootsrap.inc

<?php
// $Id: index.php,v 1.91 2006/12/12 09:32:18 unconed Exp $

require_once './includes/bootstrap.inc';
include_once('./lib/lib.php');            // This is my hack
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$return = menu_execute_active_handler(); ....
?>

4. The content of lib.php is something like this

<?php

function foo($par){
  ....
  $str =
  return $str;
}

function listofgroup()
{
    $sql = "SELECT rid, name FROM {role} WHERE rid not in (1,2,5) ORDER BY rid";
    $rs= db_query($sql);
    $str ="";
    while ($row = db_fetch_array($rs)){
        $str .= $row['rid']."|".$row['name']."\n";
    }
    return $str;
}
.....
?>

daniorama’s picture

Fabio61, could you explain a bit more how to use that code or where should I put it? What I want is to use values from tables of my database in the default value of a webform, quicksketch told me php is not supported, what can I do then to make an SQL query? Thanks!

PS: by the way, I'm using the D6 dev version, I hope this will work also there.

uberellis-dupe’s picture

Ditto for me. Would like to fill out default values from Bio or Nodeprofile based on currently logged in user (using 5.x version however). Default values for CCK fields have the same issue unless using the profile module.

murz’s picture

subscribe

quixote’s picture

Fabio61, your method/hack works great. And just in the nick of time in my case, so extra special thanks to you!

In addition to your examples for output, I have found that
$var = "whatever";
also works, and will by default return the value of $var. Obviously you can use any $stringname. $_SERVER (and probably $_SESSION) variables work too, which I used to create a link to the node in a hidden field (as my form is embedded in another node).

Anyway, nice work!

asciikewl’s picture

Status: Active » Needs review
StatusFileSize
new3.1 KB

I needed the same feature, but preferably with some access control over who can use php for default values.

It was a little bit tricky as the permission should come from the author of the webform, not the current user.

Also I've used drupal_eval to make it a little bit less hackish :) It has the effect that instead of returning a value in the php code, you just need to print it, which is more like what most of the other cases that allow php tags in cases like this where you can have mixed text, tokens and php.

Attached is the patch.

asciikewl’s picture

Version: 5.x-2.1.3 » 5.x-2.4
StatusFileSize
new3.15 KB

I've updated this patch for webform-5.x-2.4

Cory Goodwin’s picture

I implemented this patch on a local install. I played around with different things. Everything worked as intended. I tested different default php strings. Also tested different user roles with different user settings. Everything worked. I vote for this to be committed, full php is very useful.

Nice work AsciiKewl.

Whiskey’s picture

Version: 5.x-2.4 » 6.x-2.4
StatusFileSize
new3.57 KB

I manually applied the patch to 6.x-2.4 and it seems to work fine. Created a patch that should work (first time creating a patch though..)
So I second to commit this one, thanks AsciiKewl!

codevoice’s picture

I vote that this gets included in the HEAD, it's incredibly useful!

detot’s picture

subscribe

asciikewl’s picture

I just tried and the patch still works vs. 5.x-2.6

deepak@1your.com’s picture

webform - 6.x-2.6

#11 does not seem to work for me as it is.

Had to change

if (strpos($string, '<?php') !== FALSE && arg(0) == 'node' && is_numeric(arg(1))) {

to

if ((strpos($string, '<?php') !== FALSE)) {

to get this fix working. However I am not sure of the implications of the removed code. Could anyone please shed some insight into this problem?

Thanks,

Deepak

A collection of Java and other tutorials/articles

TimG1’s picture

subscribing

cayenne’s picture

I want! I Want! Subscribing.

asciikewl’s picture

Version: 6.x-2.4 » 6.x-2.7
StatusFileSize
new3.18 KB

Fresh patch of this vs. 2.7 Works for 5.x and 6.x

quicksketch’s picture

Version: 6.x-2.7 »

AsciiKewl, I really appreciate the reroll, but I'd prefer to not add this functionality. I'm doing everything possible to *remove* custom PHP fields from Webform by providing more robust options for validation and submission. If this functionality is added, it'll need to be in an add-on module. In any case new features are only being added to the 3.x version.

asciikewl’s picture

No probs. I have to re-roll for D5 as I have a site using it, so I might as well post it in case someone else is using it as well.

So far I've used it for 2 use cases: Pull a profile field from the user filling in the form into a hidden field and fill in a hidden field with a custom format date (YYYYMM in this case), both which then become available for quick drill down functionality in the analysis.

I'm pretty sure both could be done in another way, php just seemed to be the quickest at the time.

Is there anywhere a writeup/roadmap with what you're planning for webform 3? Maybe I can help.

irozner’s picture

Version: » 6.x-2.8

hey,
can someone port this patch to version 6.x - 2.8 and upload the entire webform.module file (or send it to me)? i need it ASAP!!!!
thanks in advance!
Ido

asciikewl’s picture

The 2.7 patch posted above still works for 2.8 and 2.9. (with some offsets, which should be fine).

To patch it, cd into the webform directory and do:

patch -p1 < webform-php-defaults-2.7.patch

quicksketch’s picture

Version: 6.x-2.8 »
Status: Needs review » Postponed

As noted in #19, I won't be adding any additional "Custom PHP" fields to Webform. I'll likely move the existing Additional Validate and Processing fields to a separate module, in which case this functionality can be added there.

vacilando’s picture

Version: » 6.x-2.9

I also urgently need to fill in default values using PHP :-(

I will try asciikewl's patch for now but I also hope there will be a proper way to do this in the future. (Already there are so many ways Drupal can be wrecked via PHP -- e.g PHP filter for body or CCK -- but it's all responsibility of the admins and developers!)

vacilando’s picture

Is there a patch / way to do this currently in Webform 3?

quicksketch’s picture

Project: Webform » Webform PHP
Version: 6.x-2.9 » 6.x-3.x-dev
Component: User interface » Webform PHP
Status: Postponed » Active

This will never be added to Webform core. Moving this over to the Webform PHP project.

quicksketch’s picture

Status: Active » Closed (won't fix)
surendra77c’s picture

Issue summary: View changes

In hook_form_alter you can set the default value by php code

$form['submitted']['myfield']['my_date']['#default_value'] = date("Y-m-d h:i a", time());