From 4ac63757675dcacae9d63b900321bfc3a66bf428 Mon Sep 17 00:00:00 2001
From: Tom Kirkpatrick <tom@systemseed.com>
Date: Fri, 16 Mar 2012 21:45:32 +0100
Subject: [PATCH 1/2] Only affect formflow fieldsets rather than any fieldset

---
 formflow.module |   55 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/formflow.module b/formflow.module
index 4027c9d..e5bb565 100644
--- a/formflow.module
+++ b/formflow.module
@@ -111,7 +111,21 @@ function formflow_field_group_format_settings($group){
  * Implements hook_field_attach_form().
  */
 function formflow_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode){
-  formflow_attach_groups($form, $form_state, (isset($entity->is_new) ? 'add' : 'edit'));
+  if (isset($entity->is_new)) {
+    // Some entities provide an "is_new" property. If that is present, respect
+    // whatever it's set to.
+    $is_new = $entity->is_new;
+  }
+  else {
+    // Otherwise, try to find out if the entity is new by checking its ID. Note
+    // that checking empty() rather than !isset() is important here, to deal
+    // with the case of entities that store "0" as their ID while the final
+    // entity is in the process of being created (user accounts are a good
+    // example of this).
+    list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
+    $is_new = empty($id);
+  }
+  formflow_attach_groups($form, $form_state, ($is_new ? 'add' : 'edit'));
 }
 
 /**
@@ -521,19 +535,39 @@ function formflow_menu_cache_clear(){
  * @param String $view_mode The mode for the build.
  */
 function formflow_attach_groups(&$form, &$form_state, $display_mode){
-  // If there are no ghroups defined, do nothing
-  if(!isset($form['#groups'])){return;}
+  // If there are no groups defined, do nothing
+  if(!isset($form['#groups'])){
+    return;
+  }
+  
+  // Build up a list of formfield groups.
+  $formflow_groups = array();
+  foreach($form['#groups'] as $group_name => $group) {
+    if ($group->format_type == 'formflow') {
+      $formflow_groups[] = $group_name;
+    }
+  }
+
+  // If there are no formfield groups, do nothing.
+  if (empty($formflow_groups)) {
+    return;
+  }
+
   $form['#display_mode'] = $display_mode;
   ctools_add_css('wizard');
+
   // Attach the fieldgroup CSS
   $form['#attached']['css'] = array(
     drupal_get_path('module', 'formflow') . '/css/formflow.css'
   );
-  // If there's no current step, default tot he first one
+
+  // If there's no current step, default to the first one
   if(!isset($form_state['current_group'])){
     $form_state['current_group'] = @array_shift(array_keys($form['#groups']));
   }
+
   $form['#current_group'] = $form_state['current_group'];
+
   // Build a trail to display at the top of the form
   $form['trail'] = array(
     '#theme' => 'formflow_wizard_trail',
@@ -541,7 +575,8 @@ function formflow_attach_groups(&$form, &$form_state, $display_mode){
     '#weight' => -1000,
     '#formflow' => true
   );
-  foreach($form['#groups'] as $group){
+
+  foreach($form['#groups'] as $group) {
     if(empty($group->format_settings['instance_settings']['display_mode']) || $group->format_settings['instance_settings']['display_mode'] == $display_mode){
       if($group->group_name == $form['#current_group']){
         $class = 'wizard-trail-current';
@@ -551,13 +586,16 @@ function formflow_attach_groups(&$form, &$form_state, $display_mode){
       $form['trail']['#value'][] = '<span class="' . $class . '">' . $group->label . '</span>';
     }
   }
+
   // If there's still groups for this build mode, add some buttons
   if(count($form['trail']['#value'])){
+
     // Get the numeric position of the current group
     $form['#position'] = array_search($form['#current_group'], array_keys($form['#groups']));
     $form['formflow_actions'] = array(
       '#type' => 'actions'
     );
+
     // If this isn't the last group, show the back button
     if($form['#position'] > 0){
       $form['formflow_actions']['back'] = array(
@@ -571,6 +609,7 @@ function formflow_attach_groups(&$form, &$form_state, $display_mode){
         '#formflow' => true // Set a flag to preserve this over all formflow steps
       );
     }
+
     // If this isn't the last group, show the next button
     if(($form['#position'] + 1) < count($form['#groups'])){
       $form['formflow_actions']['next'] = array(
@@ -583,6 +622,7 @@ function formflow_attach_groups(&$form, &$form_state, $display_mode){
         '#formflow' => true // Set a flag to preserve this over all formflow steps
       );
     }
+
     // limit validation errors to only the form items in the current group
     foreach($form['#group_children'] as $element_name => $group){
       if($group == $form['#current_group']){
@@ -647,8 +687,11 @@ function formflow_field_group_navigate($op, $form, &$form_state){
  * @param $form The form object itself.
  */
 function formflow_field_group_pre_render(&$element, $group, &$form){
+  // Prepare some variables to use in the logic.
+  $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
+  $id = $form['#entity_type'] . '_' . $form['#bundle'] . '_' . $view_mode . '_' . $group->group_name;
+  
   if(empty($group->format_settings['instance_settings']['display_mode']) || $group->format_settings['instance_settings']['display_mode'] == $form['#display_mode']){
-    $id = $form['#entity_type'] . '_' . $form['#bundle'] . '_' . $form['#display_mode'] . '_' . $group->group_name;
     // Prepare extra classes.
     $classes = array(
       'field-group-' . $group->format_type,
-- 
1.7.4.4


From c529754c373189bfcc4b7a59d9239c417161cd3a Mon Sep 17 00:00:00 2001
From: Tom Kirkpatrick <tom@systemseed.com>
Date: Fri, 16 Mar 2012 22:02:54 +0100
Subject: [PATCH 2/2] Only try to render something if #display_mode has been
 set

---
 formflow.module |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/formflow.module b/formflow.module
index e5bb565..d8071a1 100644
--- a/formflow.module
+++ b/formflow.module
@@ -364,7 +364,7 @@ function formflow_page($flow, $current_step = 0){
     $node = (object)array(
       'uid' => $user->uid,
       'name' => (isset($user->name) ? $user->name : ''),
-      'type' => 'page',
+      'type' => $node_type,
       'language' => LANGUAGE_NONE
     );
     $form_state['build_info']['args'] = array(
@@ -687,11 +687,8 @@ function formflow_field_group_navigate($op, $form, &$form_state){
  * @param $form The form object itself.
  */
 function formflow_field_group_pre_render(&$element, $group, &$form){
-  // Prepare some variables to use in the logic.
-  $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
-  $id = $form['#entity_type'] . '_' . $form['#bundle'] . '_' . $view_mode . '_' . $group->group_name;
-  
-  if(empty($group->format_settings['instance_settings']['display_mode']) || $group->format_settings['instance_settings']['display_mode'] == $form['#display_mode']){
+  if(isset($form['#display_mode']) && (empty($group->format_settings['instance_settings']['display_mode']) || $group->format_settings['instance_settings']['display_mode'] == $form['#display_mode'])){
+    $id = $form['#entity_type'] . '_' . $form['#bundle'] . '_' . $form['#display_mode'] . '_' . $group->group_name;
     // Prepare extra classes.
     $classes = array(
       'field-group-' . $group->format_type,
-- 
1.7.4.4

