0

I having a problem saving user data to the database. Everything is being saved apart from 'street_address', 'town_city', 'region' 'postal_code' and 'country'.

full module_create_user:

function openid_connect_create_user($sub, $userinfo, $client_name) {

  $address = $userinfo['address'];
  $decodeAddress = json_decode($address);

  $firstName = $userinfo['given_name'];
  $lastName = $userinfo['family_name'];


  $streetAddress2 = $decodeAddress->{"street_address"};
  $townCity2 = $decodeAddress->{"locality"};
  $region2 = $decodeAddress->{"region"};
  $postalCode2 = $decodeAddress->{"postal_code"};
  $country2 = $decodeAddress->{"country"};

  $streetAddress = (string)$streetAddress2;
  $townCity = (string)$townCity2;
  $region = (string)$region2;
  $postalCode = (string)$postalCode2;
  $country = (string)$country2;

  $edit = array(
    'name' => $firstName.$lastName.$sub, //works
    'pass' => user_password(), //works
    'mail' => $userinfo['email'], //works
    'init' => $userinfo['email'], //works
    'family_name' => $userinfo['family_name'], //works
    'given_name' => $userinfo['given_name'], //works
    'nickname' => $userinfo['nickname'], //works
    'gender' => $userinfo['gender'], //works
    'birthdate' => $userinfo['birthdate'], //works
    'phone_number' => $userinfo['phone_number'], //works
    'street_address' => $streetAddress, //doesn't work
    'town_city' => $townCity, //doesn't work
    'region' => $region, //doesn't work
    'postal_code' => $postalCode, //doesn't work
    'country' => $country, //doesn't work
    'status' => 1, //works
    'openid_connect_client' => $client_name, //works
    'openid_connect_sub' => $sub, //works
  );

  return user_save(NULL, $edit);
}

different $edit i've tried:

$edit = array(
    'name' => $firstName.$lastName.$sub, //works
    'pass' => user_password(), //works
    'mail' => $userinfo['email'], //works
    'init' => $userinfo['email'], //works
    'family_name' => $userinfo['family_name'], //works
    'given_name' => $userinfo['given_name'], //works
    'nickname' => $userinfo['nickname'], //works
    'gender' => $userinfo['gender'], //works
    'birthdate' => $userinfo['birthdate'], //works
    'phone_number' => $userinfo['phone_number'], //works
    'field_street_address_pixelpin' => $streetAddress, //doesn't work
    'field_town_city_pixelpin' => $townCity, //doesn't work
    'field_region_pixelpin' => $region, //doesn't work
    'field_postal_code_pixelpin' => $postalCode, //doesn't work
    'field_country_pixelpin' => $country, //doesn't work
    'status' => 1, //works
    'openid_connect_client' => $client_name, //works
    'openid_connect_sub' => $sub, //works
  );

examples of the fields which are being programmability created:

Family name (works):

if (!field_info_field('field_family_name_pixelpin')) {
    $field = array(
        'field_name' => 'field_family_name_pixelpin', 
        'type' => 'text', 
    );
    field_create_field($field);

    $instance = array(
        'field_name' => 'field_family_name_pixelpin', 
        'entity_type' => 'user', 
        'label' => 'Family Name', 
        'bundle' => 'user', 
        // If you don't set the "required" property then the field wont be required by default.
        'required' => FALSE,
        'settings' => array(
           // Here you inform either or not you want this field showing up on the registration form.
            'user_register_form' => 1,
        ),
        'widget' => array(
            'type' => 'textfield',
        ),
        'display' => array(
          'default' => array('label' => 'above'),
      ),
    );
    field_create_instance($instance);
  }

postal code (doesn't work):

if(!field_info_field('field_postal_code_pixelpin')) {
    $field = array(
        'field_name' => 'field_postal_code_pixelpin', 
        'type' => 'text', 
    );
    field_create_field($field);
    $instance = array(
        'field_name' => 'field_postal_code_pixelpin', 
        'entity_type' => 'user', 
        'label' => 'Postal Code', 
        'bundle' => 'user', 
        // If you don't set the "required" property then the field wont be required by default.
        'required' => FALSE,
        'settings' => array(
           // Here you inform either or not you want this field showing up on the registration form.
            'user_register_form' => 1,
        ),
        'widget' => array(
            'type' => 'textfield',
        ),
        'display' => array(
          'default' => array('label' => 'above'),
      ),
    );
    field_create_instance($instance);
  }
1

In general, for custom user fields, user_save requires the field values to be provided as an array. An example would be the following:

 $edit['field_yourfield'] = array(
   LANGUAGE_NONE => array(
     0 => array('value' => $yourValue),
   ),
 );
  • I'll give it a try – Callum Jan 20 '17 at 11:32
  • It works, well done. – Callum Jan 20 '17 at 11:43

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.