Hello

I'm trying to prepopulate a couple of CCK text fields that are used in several different content types (it's basically contact information), however I cannot find a working solution. How should I go by doing this from template.php for instance?

I would really like the solution to work whenever the field is used - even if it's in a group or not.

I know of the Prepopulate module, and how that can work in this scenario - however I've got some business logic needed for the field values and I don't want the arguments to be passed in the query-string.

Best regards,
Thomas René Sidor

Comments

markus_petrux’s picture

Status: Active » Fixed

I think you need to use hook_form_alter(). You have an example in Prepopulate module itself. That one gets the values from the URL, your may get the values from somewhere else... or based on any logic that you may want to apply.

Sidor’s picture

Thanks for the reply.

I've already had a look at the Prepopulate module, but it doesn't really work well for CCK fields - especially not when they're in groups. Then the reference for the field is something like $form['group']['field_something'][0]['#default_value']['value'] as far as I can remember.

What I really could use is a method that iterates the form object and finds the proper location of the form_something field (independent on what group it is in) and then assigns it some value. This is because I'm using field_something a lot of places and in differently named groups.

markus_petrux’s picture

Note that the fieldgroup module has a weight of... 10, IIRC. That means its hook_form_alter() implementation runs after the one implemented by CCK itself, which uses a module weight of 0. If your module has a weight between 0 and 10 (exclusive), it will run between CCK and fieldgroup, so you do not need to care about $form['group'] stuff.

Sidor’s picture

Great - thanks to your help I managed to create a working solution with some changes to the Preload module.

Here's the code if anyone else should stumble upon this post in the future:

function altomtrekroner_form_alter(&$form, $form_state, $form_id) {

	if($form['#id'] == 'node-form'){

		
		$field_name_value = '';
		$field_phonenumber_value = '';
		$field_email_value = '';

		//Get cookie values
		$cookievalues = $_COOKIE['contactfields'];
		if(!is_null($cookievalues))
		{
			if(!is_null($cookievalues['name']))
				$field_name_value = $cookievalues['name'];
				
			if(!is_null($cookievalues['phonenumber']))
				$field_phonenumber_value = $cookievalues['phonenumber'];
				
			if(!is_null($cookievalues['email']))
				$field_email_value = $cookievalues['email'];
		}
		//If they don't exist - use user values
		else
		{
			global $user;
			$field_name_value = $user->name;
			$field_phonenumber_value = '';
			$field_email_value = $user->mail;
		}
		
		//Prepare the array with field names and arrays.
		$fields = array(
			'field_name' =>  		array(0 => $field_name_value),
			'field_phonenumber' =>  array(0 => $field_phonenumber_value),
			'field_email' => 		array(0 => $field_email_value)
		);
		
		//Iterate the form array and set
		foreach (array_keys($fields) as $fieldname) {
			if (element_child($fieldname) && !is_null($form[$fieldname])) {
				_altomtrekroner_get_walk($form[$fieldname], $fields[$fieldname]);
			}
		}
  	}
}

function _altomtrekroner_get_walk( & $form, & $fieldsslice) {
  
	if (is_array($fieldsslice)) {
		
		//If we're at the end of the tree
		if (!is_null($form['#default_value'])) {
		
			//The default value should always be an array
			if (!is_array($form['#default_value'])) {
				// Something went wrong so stop here.
				return;
			}
			
			$form['#default_value'] = array_merge($form['#default_value'], $fieldsslice);
		}
		else {
			//Go a step deeper - this is basically the [0]-thingies
			foreach (array_keys($fieldsslice) as $fieldname) {
				if (element_child($fieldname) && is_array($form) && !is_null($form[$fieldname])) {
					_altomtrekroner_get_walk($form[$fieldname], $fieldsslice[$fieldname]);
				}
			}
		}
	}
	else {
		//Field found - now setting default value
		$form['#default_value']['value'] = $fieldsslice;
		if(!is_null($form['#default_value']['email']))
			$form['#default_value']['email'] = $fieldsslice;
	}
}

function altomtrekroner_nodeapi(&$node, $op) {
	if ($op == 'insert') {
		$field_name_value = $node->field_name[0]['value'];
		$field_phonenumber_value = $node->field_phonenumber[0]['value'];
		$field_email_value = $node->field_email[0]['email'];
		
		setcookie("contactfields[name]", $field_name_value);
		setcookie("contactfields[phonenumber]", $field_phonenumber_value);
		setcookie("contactfields[email]", $field_email_value);
	}
}

Basically the module (called altomtrekroner) sets the default values for three fields (currently they're hard coded - but you could easily create a solution that take the field names from a list and does the same thing - I just won't bother today) based on the following conditions

- If the user has saved a node with values for the fields previously, those values are loaded as default the next time.
- If the user has never used those fields before values from the user profile are used (if the user is logged in of course).

The values are stored in a cookie.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.