diff -u b/lib/Drupal/profile2/Profile.php b/lib/Drupal/profile2/Profile.php --- b/lib/Drupal/profile2/Profile.php +++ b/lib/Drupal/profile2/Profile.php @@ -100,6 +100,20 @@ } /** + * Overwrites EntityInterface::id(). + */ + public function id() { + return isset($this->pid) ? $this->pid : NULL; + } + + /** + * Overwrites EntityInterface::bundle(). + */ + public function bundle() { + return $this->type; + } + + /** * Returns the full url() for the profile. */ public function url() { diff -u b/profile2.module b/profile2.module --- b/profile2.module +++ b/profile2.module @@ -5,6 +5,9 @@ * Support for configurable user profiles. */ +use Drupal\profile2\Profile; +use Drupal\profile2\ProfileType; + /** * Implements hook_entity_info(). */ @@ -87,7 +90,7 @@ * @param Drupal\profile2\Profile $profile * A Profile entity. */ -function profile2_profile_uri(Drupal\profile2\Profile $profile) { +function profile2_profile_uri(Profile $profile) { return array( 'path' => 'admin/structure/profiles', ); @@ -99,7 +102,7 @@ * @param Drupal\profile2\ProfileType $profile_type * A ProfileType entity. */ -function profile2_profile_type_uri(Drupal\profile2\ProfileType $profile_type) { +function profile2_profile_type_uri(ProfileType $profile_type) { return array( 'path' => 'admin/structure/profiles/manage', ); @@ -298,12 +301,11 @@ */ function profile2_get_types($type_name = NULL) { $types = entity_load_multiple('profile2_type', NULL, TRUE); - if (!is_null($type_name)) { - foreach ($types as $key => $type) { - if ($type->get('type') != $type_name) { - unset($types[$key]); - } + foreach ($types as $key => $type) { + if (is_null($type_name) || $type->get('type') == $type_name) { + $types[$type->get('type')] = $type; } + unset($types[$key]); } return isset($type_name) ? reset($types) : $types; } @@ -321,7 +323,7 @@ * @see profile2_load_multiple() */ function profile2_load($pid, $reset = FALSE) { - $profiles = profile2_load_multiple(array($pid), array(), $reset); + $profiles = profile2_load_multiple($pid, array(), $reset); return reset($profiles); } @@ -342,7 +344,13 @@ * @see profile2_load_by_user() */ function profile2_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) { - return entity_load('profile2', $pids, $conditions, $reset); + if (empty($conditions)) { + // it doesn't make sense to use pids and conditions, + // therefore ignore pids if conditions were set + return entity_load_multiple_by_properties('profile2', $conditions); + } + + return entity_load_multiple('profile2', $pids, $reset); } /** @@ -487,16 +495,21 @@ * @see profile2_form_submit_handler */ function profile2_form_user_profile_form_alter(&$form, &$form_state) { - if (($type = profile2_get_types($form['#user_category'])) && $type->userCategory) { - if (empty($form_state['profiles'])) { - $profile = profile2_load_by_user($form['#user'], $form['#user_category']); - if (empty($profile)) { - $profile = profile2_create(array('type' => $form['#user_category'], 'uid' => $form['#user']->uid)); - } - $form_state['profiles'][$profile->type] = $profile; + // #user_category no longer exists, use fieldsets instead + foreach (profile2_get_types() as $type_name => $profile_type) { + $profile = profile2_load_by_user($form_state['entity'], $type_name); + if (empty($profile)) { + $profile = profile2_create(array('type' => $type_name, 'uid' => $form_state['entity']->uid)); } + $form_state['profiles'][$profile->type] = $profile; profile2_attach_form($form, $form_state); + // Wrap each profile form in a fieldset. + $form['profile_' . $type_name] += array( + '#type' => 'fieldset', + '#title' => check_plain($profile_type->getTranslation('label')), + ); } + return; } /** @@ -504,7 +517,9 @@ */ function profile2_form_user_register_form_alter(&$form, &$form_state) { foreach (profile2_get_types() as $type_name => $profile_type) { - if (!empty($profile_type->data['registration'])) { + $data = $profile_type->get('data'); + $data = unserialize($data); + if (!empty($data['registration'])) { if (empty($form_state['profiles'][$type_name])) { $form_state['profiles'][$type_name] = profile2_create(array('type' => $type_name)); } @@ -539,6 +554,7 @@ foreach ($form_state['profiles'] as $type => $profile) { $form['profile_' . $profile->type]['#tree'] = TRUE; $form['profile_' . $profile->type]['#parents'] = array('profile_' . $profile->type); + field_attach_form('profile2', $profile, $form['profile_' . $profile->type], $form_state); if (count(field_info_instances('profile2', $profile->type)) == 0) { @@ -558,7 +574,7 @@ } } $form['#validate'][] = 'profile2_form_validate_handler'; - $form['#submit'][] = 'profile2_form_submit_handler'; + $form_state['submit_handlers'][] = 'profile2_form_submit_handler'; } /** @@ -570,11 +586,13 @@ foreach ($form_state['profiles'] as $type => $profile) { if (isset($form_state['values']['profile_' . $profile->type])) { // @see entity_form_field_validate() - $pseudo_entity = (object) $form_state['values']['profile_' . $profile->type]; - $pseudo_entity->type = $type; + $pseudo_entity = new Profile(array_merge($form_state['values']['profile_' . $profile->type], array('type' => $type))); field_attach_form_validate('profile2', $pseudo_entity, $form['profile_' . $profile->type], $form_state); } } + + // @todo Looking for a better solution; this one looks like a ugly hack + $form_state['submit_handlers'][] = 'profile2_form_submit_handler'; } /**