1

I'm trying to programmatically add fields that are manually added here:

/admin/config/people/accounts/fields

I'd want to add the fields when an admin enables my module/extension.

I've tried this in my MODULE.install file:

function openid_conect_enable() {
  field_cache_clear();
  field_associate_fields('user_alternative');

  //Given name field.
  if (!field_info_field('field_given_name')) {
    $field = array(
        'field_name' => 'field_given_name', 
        'type' => 'text', 
    );
    field_create_field($field);

    // Create the instance on the bundle.
    $instance = array(
        'field_name' => 'field_given_name', 
        'entity_type' => 'user', 
        'label' => 'Given Name', 
        'bundle' => 'user', 
        // If you don't set the "required" property then the field wont be required by default.
        'required' => TRUE,
        '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);
  }
}

but with no success.

Your Answer

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

Browse other questions tagged or ask your own question.