Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 347
Sam Martin Avatar asked Oct 11 '22 07:10

Sam Martin


1 Answers

bundle is pretty much the same as content type. But since in D7 users are entities too, but they are not content, using the term 'content type' didn't make sense. Reference: Barry Jaspan's DrupalCon Paris 2009 Session: Intro to the Field API for Module Developers.

Intro to the Field API for Module Developers

like image 55
cluther Avatar answered Oct 14 '22 01:10

cluther