Default Location for CCK field on Drupal Node Add Form
We wanted to automatically fill in the Location fields on a node add form based upon the author’s location. Since most of these items that are posted would share the same location as the author, it seemed like a nice thing to do to make it a little easier for users.
In order to accomplish this, we used Drupal’s after_build. This allows you to call your own custom function. In this case it calls add_user_location which takes the user’s location and fills in the CCK location fields.
function digitalstax_customizations_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'widget_node_form') {
$form['#after_build'][] = 'add_user_location';
}
function add_user_location(&$form) {
global $user;
$viewinguser = user_load(array(‘uid’ => $user->uid));
$form['locations'][0]['street']['#value'] = $viewinguser->locations[0]['street'];
$form['locations'][0]['city']['#value'] = $viewinguser->locations[0]['city'];
$form['locations'][0]['province']['#value'] = $viewinguser->locations[0]['province'];
$form['locations'][0]['postal_code']['#value'] = $viewinguser->locations[0]['postal_code'];
return $form;
}
| Print article | This entry was posted by Raymond on May 4, 2010 at 2:47 am, and is filed under CCK, Drupal, Form Alter, Location. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.