From 83e71aef65122d8286da301789cedd3540c879c6 Mon Sep 17 00:00:00 2001
From: Kristiaan Van den Eynde <magentix@gmail.com>
Date: Wed, 8 Oct 2014 14:50:36 +0200
Subject: [PATCH] Issue #1865996 by kristiaanvandeneynde, DanChadwick: Fixed
 form_profile2_form and form_profile2_TYPE_form alter
 functions called many, many times for user registration.

---
 profile2.module |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/profile2.module b/profile2.module
index d1e3ad9..ea7b00f 100644
--- a/profile2.module
+++ b/profile2.module
@@ -388,14 +388,17 @@ function profile2_form_user_register_form_alter(&$form, &$form_state) {
       if (empty($form_state['profiles'][$type_name])) {
         $form_state['profiles'][$type_name] = profile2_create(array('type' => $type_name));
       }
-      profile2_attach_form($form, $form_state);
       // Wrap each profile form in a fieldset.
-      $form['profile_' . $type_name] += array(
+      $form['profile_' . $type_name] = array(
         '#type' => 'fieldset',
         '#title' => check_plain($profile_type->getTranslation('label')),
       );
     }
   }
+
+  if (!empty($form_state['profiles'])) {
+    profile2_attach_form($form, $form_state);
+  }
 }
 
 /**
-- 
1.7.9.4

