diff --git profile2.install profile2.install index c2555aa..dd26861 100644 --- profile2.install +++ profile2.install @@ -10,28 +10,16 @@ * Implements hook_install(). */ function profile2_install() { - // Auto-create a field for the full name, if it doesn't exist yet. - if (!field_info_field('profile_fullname')) { - $field = array( - 'field_name' => 'profile_fullname', - 'type' => 'text', - 'cardinality' => 1, - 'translatable' => FALSE, - ); - field_create_field($field); + // Install any fields that don't exist + // and attach them to relevant bundles + foreach(_profile2_fields_info() as $field_info) { + // Field + if (!field_info_field($field_info['field']['field_name'])) { + field_create_field($field_info['field']); + } + // Instance + field_create_instance($field_info['instance']); } - $instance = array( - 'entity_type' => 'profile', - 'field_name' => 'profile_fullname', - 'bundle' => 'main', - 'label' => 'Full name', - 'description' => 'Specify your first and last name.', - 'widget' => array( - 'type' => 'text_textfield', - 'weight' => 0, - ), - ); - field_create_instance($instance); } /** @@ -39,6 +27,42 @@ function profile2_install() { */ function profile2_uninstall() { variable_del('profile_register'); + // Remove any instance fields we've added + foreach(_profile2_fields_info() as $field_info) { + field_delete_instance($field_info['instance']); + } + + // %TODO% now need to purge, in case fields now have no + // instances +} + +/** + * Helper function defining fields and instances + * This allows us to tidy up in hook_uninstall properly + */ +function _profile2_fields_info() { + // Add an instance of this field to the main profile + return array( + array( + 'field' => array( + 'field_name' => 'profile_fullname', + 'type' => 'text', + 'cardinality' => 1, + 'translatable' => FALSE, + ), + 'instance' => array( + 'entity_type' => 'profile', + 'field_name' => 'profile_fullname', + 'bundle' => 'main', + 'label' => 'Full name', + 'description' => 'Specify your first and last name.', + 'widget' => array( + 'type' => 'text_textfield', + 'weight' => 0, + ), + ), + ), + ); } /**