You are here

function textimage_js_background in Textimage 6.2

Same name and namespace in other branches
  1. 5.2 textimage.module \textimage_js_background()
1 call to textimage_js_background()
textimage_js in ./textimage.module
Textimage AHAH functionality

File

./textimage.module, line 1011

Code

function textimage_js_background(&$output) {
  if (empty($_POST['form_build_id'])) {

    // Invalid request.
    drupal_set_message(t('An unrecoverable error occurred.'), 'error');
    print drupal_to_js(array(
      'data' => theme('status_messages'),
    ));
    exit;
  }

  // Build the new form.
  $form_state = array(
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);
  if (!$form) {

    // Invalid form_build_id.
    drupal_set_message(t('An unrecoverable error occurred. This form was missing from the server cache. Try reloading the page and submitting again.'), 'error');
    print drupal_to_js(array(
      'data' => theme('status_messages'),
    ));
    exit;
  }

  // form_get_cache() doesn't yield the original $form_state,
  // but form_builder() does. Needed for retrieving the file array.
  $built_form = $form;
  $built_form_state = $form_state;
  $built_form += array(
    '#post' => $_POST,
  );
  $built_form = form_builder($_POST['form_id'], $built_form, $built_form_state);

  // Clean ids, so that the same element doesn't get a different element id
  // when rendered once more further down.
  form_clean_id(NULL, TRUE);
  foreach (element_children($form['settings']['preview']['text']) as $key) {
    if ($key != 'default') {
      unset($form['settings']['preview']['text'][$key]);
    }
  }
  if (is_numeric($_POST['settings']['background']['image']) && ($preset = _textimage_preset_load($_POST['settings']['background']['image']))) {
    if (!isset($form['settings']['preview']['text']['additional'])) {
      $form['settings']['preview']['text']['additional'] = array();
    }
    _textimage_js_background($form);
    while (is_numeric($preset['settings']['background']['image']) && ($preset = _textimage_preset_load($preset['settings']['background']['image']))) {
      _textimage_js_background($form);
    }
  }
  form_set_cache($form_build_id, $form, $form_state);
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
  );
  $form = form_builder($_POST['form_id'], $form, $form_state);
  $output = drupal_render($form['settings']['preview']['text']);
}