2

I'm building a module which implements hook_enable to create fields. But as I'm developing I noticed a error in my code causing fields to be created in a wrong way.

Is there a way to update these fields? preferably using code so I can also use this code to hide these fields from lets say a user profile once the module is disabled.

here's my code that I used to create those fields:

//calling my function
popover_create_user_field('field_firstname', 'Voornaam', 'textfield', 1);

/**
 * field creator helper function
 */
function popover_create_user_field($field_name, $field_label, $field_type, $visible_on_registration_form = 1) {
    // Check if our field is not already created.   
    if (!field_info_field($field_name)) {
        $field = array(
            'field_name' => $field_name, 
            'type' => 'text', 
        );
        field_create_field($field);

        // Create the instance on the bundle.
        $instance = array(
            'field_name' => $field_name, 
            'entity_type' => 'user', 
            'label' => $field_label, 
            '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' => $visible_on_registration_form,
            ),
            'widget' => array(
                'type' => $field_type,
            ),
        );
        field_create_instance($instance);
    }
}

Lets say I want to update the user_register_form visibility to 0 so it will be hidden.

3

You can use a combination of field_info_instance() and field_update_instance():

// Load the instance
$instance = field_info_instance('user', $field_name, 'user');

// Make whatever changes you need to, e.g.
$instance['settings']['user_register_form'] = 0;

// And save it again
field_update_instance($instance);

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.