Search code examples
drupaldrupal-7

How to add user-fields programmatically in Drupal 7


I'm trying to create a .install for a module that I'm migrating from Drupal 6. It requires two 'profile fields', which, in drupal 6, it checked for and created automatically.

To upgrade it to drupal 7 I'm trying to do this with fields! Easy enough right?

So far I have

if(!field_info_field('user_fullname')) {
    $field = array(
        'field_name' => 'user_fullname',
        'type' => 'text',
        'settings' => array(
            'required' => TRUE,
        ),
    );
    field_create_field($field);
    $instance = array(
        'field_name' => 'user_fullname',
        'entity_type' => 'user',
        'label' => 'The user\'s full name',
        'bundle' => 'additional_info',
        'required' => true,
        'widget' => array(
            'type'=>'options_select',
        )
    );
    field_create_instance($instance);
}

Which, sure enough, creates the field, but it's not visible in the user's profile?
Do I need something additional for that? If so, What?

Many Thanks.

SOLVED: It was due to the bundle (not entirely sure what a bundle is really), I changed the bundle to 'user' and it appeared!


Solution

  • It was due to the bundle (not entirely sure what a bundle is really), I changed the bundle to 'user' and it appeared!