How to create a cck text field with php default value

paispita's picture

Hello everybody.
I've been searching for quite a while now and still wasn't able to find an answer yet.

I'm using Drupal 7.4 and I created a new content type. There i want to add a simple text field that has a Default value where I can add PHP. Normally, the Default value shows only plain text.

To be more specific:
For the users I defined a field_city that they must complete when they sign up.
So for example i would have a user "john" from "Bucharest".
I need to assign the value of field_city - namely Bucharest - to the default value of the text field via PHP (I know how to do this part).
So when john adds new content, he would already have Bucharest printed in that text field but still be able to write whatever other city there.

How could I achieve this?
Can I get away without making a custom module?
Thanx.

Comments

Try the prepopulate module

kadimi's picture

What about prefilling the field you want using the prepopulate module, somthing like this: /node/add/content_type?edit[field_city][und][0][city]=casablanca

"casablanca" can be grabbed by the views module...

I solved it partially. I made

paispita's picture

I solved it partially.
I made a custom module as follows. It displays the city of the user in the "default value" but it doesn't save it. It only saves the firs letter.
What else do I still need to add?

<?php

/**
* Implements hook_field_widget_info().
*/
function mywidget_field_widget_info() {
return array(
'text_mywidget' => array(
'label' => t('My widget text field'),
'field types' => array('text'),
'settings' => array('size' => 60))
);
}

/**
* Implements hook_field_widget_form().
*/
function mywidget_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
global $user;
$user_fields = user_load($user->uid);
$orasel = $user_fields->field_city['und']['0']['value'];
//$items[$delta] = 'asdfasdfasdfsd';
$element += array(
'#type' => 'textfield',
'#default_value' => $orasel,
);
return $element;
}

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: