I want to use the title over my content profile node in a webform as default value for the name. I can't find the token for it. %profile is the token for the drupal default profile. Am I missing something?

Comments

jdln’s picture

Subscribing

serg.remote’s picture

subscribing

robbertnl’s picture

Any suggestion already?

blauerberg’s picture

subscribing

Rob_Feature’s picture

Status: Active » Closed (won't fix)

Unfortunately the webform module maintainer has no intention of supporting content profile...which is too bad, but that means there won't be support to do this. See #244152: CCK, Content Profile and Webform

tonytosta’s picture

Here's some simple code to implement a 'ghetto' token. Its not pretty, but I thought I would share....

/**
 * Implementation of hook_nodeapi().
 *
 * @param &$node The node the action is being performed on.
 * @param $op What kind of action is being performed. Possible values: alter, delete, delete revision, insert, load, prepare, prepare translation, print, rss item, search result, presave, update, update index, validate, view
 * @param $a3
 * @param $a4
 */
function MODULENAME_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  if ($node->type == 'webform' && $op == 'view') {
    $components = $node->webform['components'];

    foreach ($components as $i => $comp) {
      // TODO : replace with full profile support
      
      if ($comp['value'] == '%content_profile[field_first_name] %content_profile[field_last_name]') {

        global $user;
        $profile = content_profile_load('profile', $user->uid);
        if ($profile->nid){
          $default_name = $profile->field_first_name[0]['value'] .' '. $profile->field_last_name[0]['value'];
          $new = str_replace('%content_profile[field_first_name] %content_profile[field_last_name]', $default_name, $node->content['webform']['#value']);
          $node->content['webform']['#value'] = $new;
        }
      }
    }

  }
}
gumrol’s picture

Contribooting.
Thanks @tonytosta - your code was most helpful. I took your code and made it a bit more generic, so one can replace multiple tokens in a form. The only thing is: I broke your 'Multiple tokens to one field' logic. I just replace 1-1, but I'm sure the code can be modified fairly easily to accomplish this.
One just needs to get the naming of your fields right in your replacement string from CCK and it should just work. Sorry for the mixed naming conventions - pushed on a deadline and don't have time to change the older code to my convention.
So if you have a surname field in cck named field_surname - your token would be %content_profile[field_surname]

/**
 * Implementation of hook_nodeapi().
 *
 * @param &$node The node the action is being performed on.
 * @param $op What kind of action is being performed. Possible values: alter, delete, delete revision, insert, load, prepare, prepare translation, print, rss item, search result, presave, update, update index, validate, view
 * @param $a3
 * @param $a4
 */
function MODULENAME_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  
  // our own implementation of token replace for webform.
  if( $node->type == 'webform' && $op == 'view' ){
	global $user;
    
	$components = $node->webform['components'];
	$profile = content_profile_load('profile', $user->uid);

	$a_replace = array(); // replace variables stored
	
    foreach ($components as $s_key => $a_comp) {
      
      if( strpos($a_comp['value'], '%content_profile')  !== false ) {
		$s_replacement = '';
		$s_replacementkey = str_replace(array('%content_profile[', ']'), '', $a_comp['value']);// cheeky replace (could be better with regexp)
		
		if( $profile->nid && isset($profile->{$s_replacementkey}) ){
          $s_replacement = trim($profile->{$s_replacementkey}[0]['value']);
        }
		
		$a_replace[$a_comp['value']] = $s_replacement;
      }
    }
	
	$a_replace_keys = array_keys($a_replace);
	$a_replace_vals = array_values($a_replace);
	
	// not a pretty way of doing things, but it seems webform components in dpl6 are a bit of an issue to change with other hooks:
	$node->content['webform']['#value'] = str_replace($a_replace_keys, $a_replace_vals, $node->content['webform']['#value']);

  }
}
rade’s picture

Issue summary: View changes

Thanks @gumrol, this was just what I needed!

To others whom might use this solution as well: Note the first parameter given to content_profile_load is the name of the content type that acts as a profile. On the site I'm updating now that content type was called 'member' instead of 'profile', as it is user configurable. So make sure you edit this line where needed:

$profile = content_profile_load('profile', $user->uid);