I'm developing a module where I have to set a user profile date_field. I tried several methods to implement this but not one worked for me.

Here is my method (status today)

private function UpdateUser($user_import,$uid = null) {

    if(!$uid) return false;

    $roles = $this->MatchRoles($user_import->roles);
    $birthday = $this->BirthdayDate($user_import->profile_geburtsdatum);
    $language = \Drupal::languageManager()->getCurrentLanguage()->getId();

    $user = \Drupal\user\Entity\User::load($uid);

    //Optional settings
    $user->set("init", $user_import->init);
    $user->set("langcode", $language);
    $user->set("preferred_langcode", $language);
    $user->set("preferred_admin_langcode", $language);
    foreach ($roles as $role)$user->addRole($role);
    $user->set("field_profile_geburtsdatum", array('#default_value' => $birthday));

    //Save user

    drupal_set_message("UPDATE: User " . $user_import->name . ' ( '. $user_import->mail . ')');

private function BirthdayDate($import_date) {
    $date = null;
    if(!empty($import_date)) {

        $date = strtotime($import_date->day.'.'.$import_date->month.'.'.$import_date->year);
        $date = date('Y-m-d 00:00:00',$date);
        $date = new \Drupal\Core\Datetime\DrupalDateTime($date, 'Europe/Berlin');

        /*$date = array(
            'year' => date('Y',$date),
            'month' => date('m',$date),
            'day' => date('d',$date),
    return $date;
  • I assume you mean a datetime field? If so, is it configured for date+time or date-only? – mpdonadio Sep 8 '16 at 14:05
  • true, and its set to date only – themca Sep 8 '16 at 14:07

Assuming field_profile_geburtsdatum is configured as a singular date-only datetime field, the following are all equivalent in the Field API (in order of my preferred usage):

$user->field_profile_geburtsdatum = '1999-12-31';
$user->field_profile_geburtsdatum->value = '1999-12-31';
$user->field_profile_geburtsdatum->set(0, '1999-12-31');
$user->set('field_profile_geburtsdatum', ['1999-12-31']);

The value you set should be a string in the DATETIME_DATE_STORAGE_FORMAT, which is 'Y-m-d', and not a DrupalDateTime object. That is used internally by the field type.

  • it worked perfectly thx! – themca Sep 9 '16 at 5:11
$user->set('field_profile_geburtsdatum', [['value' => $birthday]]);

There are two issues with the call to set in the question.

  1. A field requires to set an indexed array of values. This will call FieldItemBase::setValue().
  2. Each array within the values array should be keyed by the properties of that field. In this case value, not #default_value.

It is understandably confusing because as of this time there is no clear documentation around setting field items in either the Entity API section of Develop for Drupal or on api.drupal.org.

  • It's not clear where this would best be added (in the typed data section of entity api? wait until the docs pages are all migrated?), but it would probably be best to make the parameters in FieldableEntityInterface::set much more verbose instead of the dubious mixed type. – mradcliffe Sep 8 '16 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.