diff --git a/panopoly_magic.module b/panopoly_magic.module index 11b6313..44bfffd 100644 --- a/panopoly_magic.module +++ b/panopoly_magic.module @@ -273,53 +273,50 @@ function panopoly_magic_form_alter(&$form, &$form_state, $form_id) { unset($configuration['image_link']); } } - $content = (empty($preview_subtype)) ? ctools_content_render($pane->type, $pane->subtype, $configuration, $keywords, $args, $context) : ctools_content_render($pane->type, $preview_subtype, $configuration, $keywords, $args, $context); - // Create the preview fieldset - if ($form_id == 'fieldable_panels_panes_fieldable_panels_pane_content_type_edit_form' || is_object($content)) { - - // Create the fieldset with appropriate content - $form['widget_preview'] = array( - '#type' => 'fieldset', - '#title' => 'Preview', - '#attributes' => array('class' => array('widget-preview', 'widget-preview-single')), - '#collapsible' => FALSE, - '#weight' => -100, - ); - $form['widget_preview']['preview'] = array( - '#markup' => (!empty($style['render pane'])) ? theme($style['render pane'], array('content' => $content, 'pane' => $pane, 'display' => $display, 'style' => $style, 'settings' => $pane->style['settings'])) : theme('panels_pane', array('content' => $content, 'pane' => $pane, 'display' => $display)), - ); + // Creates preview outside of form itself to fix various bugs, like form + // inside form and double rendering. + $form['#post_render'][] = 'panopoly_magic_form_post_render_preview'; + $form['#panopoly_magic_preview_info'] = array( + 'preview_subtype' => $preview_subtype, + 'pane' => $pane, + 'configuration' => $configuration, + 'keywords' => $keywords, + 'args' => $args, + 'context' => $context, + 'style' => $style, + 'display' => $display, + ); - // Remove the clearfix for preview floating - if (isset($form['aligner_start'])) { - $form['aligner_start']['#markup'] = str_replace('clearfix', '', $form['aligner_start']['#markup']); - } + // Remove the clearfix for preview floating + if (isset($form['aligner_start'])) { + $form['aligner_start']['#markup'] = str_replace('clearfix', '', $form['aligner_start']['#markup']); + } - $preview_attributes = array( - 'class' => array( - 'widget-preview-button', - 'ctools-use-ajax', - ), - ); + $preview_attributes = array( + 'class' => array( + 'widget-preview-button', + 'ctools-use-ajax', + ), + ); - // If live previewing, don't show button, and autoupdate. - if (variable_get('panopoly_magic_live_preview', 1) == 1) { - $preview_attributes['style'] = 'display: none'; - $preview_attributes['class'][] = 'ctools-auto-submit-click'; - } + // If live previewing, don't show button, and autoupdate. + if (variable_get('panopoly_magic_live_preview', 1) == 1) { + $preview_attributes['style'] = 'display: none'; + $preview_attributes['class'][] = 'ctools-auto-submit-click'; + } - // Create the preview refresh button - $form['buttons']['preview'] = array( - '#type' => 'button', - '#value' => t('Update Preview'), - '#wizard type' => 'next', - '#attributes' => $preview_attributes, - ); + // Create the preview refresh button + $form['buttons']['preview'] = array( + '#type' => 'button', + '#value' => t('Update Preview'), + '#wizard type' => 'next', + '#attributes' => $preview_attributes, + ); - // Autosubmit the form - ctools_add_js('auto-submit'); - $form['#attributes']['class'][] = 'ctools-auto-submit-full-form'; - } + // Autosubmit the form + ctools_add_js('auto-submit'); + $form['#attributes']['class'][] = 'ctools-auto-submit-full-form'; } /** @@ -330,7 +327,7 @@ function panopoly_magic_form_alter(&$form, &$form_state, $form_id) { if (!empty($form['buttons']['return'])) { $form['buttons']['return']['#value'] = t('Save'); } - $form['buttons']['#weight'] = (!empty($form['widget_preview']['#weight'])) ? ($form['widget_preview']['#weight'] +1 ): -99; + $form['buttons']['#weight'] = -99; } /** @@ -385,6 +382,33 @@ function panopoly_magic_form_alter(&$form, &$form_state, $form_id) { } } + +/** + * Add the preview to the form output. + * + * It is done here so the form is fully processed. + */ +function panopoly_magic_form_post_render_preview($output, $form) { + extract($form['#panopoly_magic_preview_info']); + $content = (empty($preview_subtype)) ? ctools_content_render($pane->type, $pane->subtype, $configuration, $keywords, $args, $context) : ctools_content_render($pane->type, $preview_subtype, $configuration, $keywords, $args, $context); + + // Create the fieldset with appropriate content + $preview = array( + '#type' => 'fieldset', + '#title' => 'Preview', + '#attributes' => array('class' => array('widget-preview', 'widget-preview-single')), + '#collapsible' => FALSE, + '#weight' => -100, + ); + if (!empty($content)) { + $preview['preview']['#markup'] = (!empty($style['render pane'])) ? theme($style['render pane'], array('content' => $content, 'pane' => $pane, 'display' => $display, 'style' => $style, 'settings' => $pane->style['settings'])) : theme('panels_pane', array('content' => $content, 'pane' => $pane, 'display' => $display)); + } + else { + $preview['preview']['#markup'] = t('[no preview]'); + } + return drupal_render($preview) . $output; +} + /** * Recursively parse form elements to add special autosubmit handling on a per field-type basis. */