Hello !

I didn't find this request and I'm sorry if it've already been reported.

I'd like an option for the user reference to set the currently logged in user as default value.

Comments

marcvangend’s picture

I did this using a simple custom module called user_reference_default, which contains the following code:

/**
 * Implements hook_field_widget_form_alter().
 */
function user_reference_default_field_widget_form_alter(&$element, &$form_state, $context) {
  global $user;
  if ($context['field']['type'] == 'user_reference' &&
      empty($element['#default_value']) &&
      isset($element['#options'][$user->uid])) {
    $element['#default_value'][0] = $user->uid;
  }
}

This code tries to set the logged in user as default for every user_reference field in the site.

For a proper contrib module, It would be needed to implement hook_form_FORM_ID_alter as well (as described in http://api.drupal.org/hook_field_widget_settings_form#comment-19724) and provide a setting to control this behavior per field instance.

Hope this helps anyone.

Alex Andrascu’s picture

Version: 7.x-2.0-beta3 » 7.x-2.x-dev
Status: Active » Postponed
Morasta’s picture

Issue summary: View changes

Thank you! I was trying the normal hook_form_alter approach without much luck. This works beautifully.