ctools_build_form

Versions
6
ctools_build_form($form_id, &$form_state)

Build a form, similar to drupal_get_form(). However, arguments to the form builder are not sent through. Instead, the $form_state can be given all the necessary data to fully utilize the form.

▾ 14 functions call ctools_build_form()

bulk_export_export in contributions/ctools/bulk_export/bulk_export.module
FAPI gateway to the bulk exporter.
ctools_context_create_node_add_form in contributions/ctools/plugins/contexts/node_add_form.inc
It's important to remember that $conf is optional here, because contexts are not always created from the UI.
ctools_context_create_node_edit_form in contributions/ctools/plugins/contexts/node_edit_form.inc
It's important to remember that $conf is optional here, because contexts are not always created from the UI.
ctools_modal_form_wrapper in contributions/ctools/includes/modal.inc
Wrap a form so that we can use it properly with AJAX. Essentially if the form wishes to render, it automatically does that, otherwise it returns so we can see submission results.
ctools_node_comment_form_content_type_render in contributions/ctools/plugins/content_types/node_context/node_comment_form.inc
ctools_search_form_content_type_render in contributions/ctools/plugins/content_types/search/search_form.inc
Render the custom content type.
ctools_wizard_multistep_form in contributions/ctools/includes/wizard.inc
Display a multi-step form.
nodecomment_nodecomment_form_content_type_render in contributions/nodecomment/plugins/content_types/nodecomment_form.inc
page_manager_edit_page in contributions/ctools/page_manager/page_manager.admin.inc
Render the edit page for a a page, custom or system.
page_manager_edit_page_operation in contributions/ctools/page_manager/page_manager.admin.inc
Entry point to edit a single operation for a page.
page_manager_list_page in contributions/ctools/page_manager/page_manager.admin.inc
Output a list of pages that are managed.
_panels_edit in contributions/panels/includes/display-edit.inc
Handle calling and processing of the form for editing display content.
_panels_edit_layout in contributions/panels/includes/display-layout.inc
Handle calling and processing of the form for editing display layouts.
_panels_edit_layout_settings in contributions/panels/includes/display-layout-settings.inc
Handle calling and processing of the form for editing display layout settings.

Code

contributions/ctools/includes/form.inc, line 21

<?php
function ctools_build_form($form_id, &$form_state) {
  // Ensure that we have some defaults.

  // These are defaults only; if already set they will not be overridden.
  $form_state += array('storage' => NULL, 'submitted' => FALSE, 'input' => $_POST, 'method' => 'post');

  $args = isset($form_state['args']) ? $form_state['args'] : array();
  $cacheable = FALSE;

  if (isset($_SESSION['batch_form_state'])) {
    // We've been redirected here after a batch processing : the form has
    // already been processed, so we grab the post-process $form_state value
    // and move on to form display. See _batch_finished() function.
    $form_state = $_SESSION['batch_form_state'];
    unset($_SESSION['batch_form_state']);
  }
  else {
    // If the incoming $_POST contains a form_build_id, we'll check the
    // cache for a copy of the form in question. If it's there, we don't
    // have to rebuild the form to proceed. In addition, if there is stored
    // form_state data from a previous step, we'll retrieve it so it can
    // be passed on to the form processing code.
    if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) {
      $form = form_get_cache($_POST['form_build_id'], $form_state);
      if (!empty($form['#no_cache']) || empty($form)) {
        unset($form);
      }
    }

    // If the previous bit of code didn't result in a populated $form
    // object, we're hitting the form for the first time and we need
    // to build it from scratch.
    if (!isset($form)) {
      $form_state['post'] = $form_state['input'];
      // This allows us to do some interesting form embedding stuff without
      // messing up the form IDs too badly.
      if (isset($form_state['wrapper callback']) && function_exists($form_state['wrapper callback'])) {
        // If there is a wrapper callback, we do not use drupal_retrieve_form.
        // Instead, we call $form_id builder function directly. This means the args
        // are *different* for forms used this way, which may not be ideal but
        // is necessary right now.
        $form = array();
        $form_state['wrapper callback']($form, $form_state);
        if (function_exists($form_id)) {
          $form_id($form, $form_state);
        }
      }
      else {
        // Use a copy of the function's arguments for manipulation
        $args_temp = $args;
        array_unshift($args_temp, 'placeholder');
        $args_temp[0] = &$form_state; // replaces the placeholder.
        array_unshift($args_temp, $form_id);

        $form = call_user_func_array('drupal_retrieve_form', $args_temp);
      }

      $form_build_id = 'form-' . md5(mt_rand());
      $form['#build_id'] = $form_build_id;


      if ($form_state['method'] == 'get' && !isset($form['#method'])) {
        $form['#method'] = 'get';
      }

      drupal_prepare_form($form_id, $form, $form_state);
      // Store a copy of the unprocessed form for caching and indicate that it
      // is cacheable if #cache will be set.
      $original_form = $form;
      $cacheable = TRUE;
      unset($form_state['post']);
    }
    $form['#post'] = $form_state['input'];

    // Now that we know we have a form, we'll process it (validating,
    // submitting, and handling the results returned by its submission
    // handlers. Submit handlers accumulate data in the form_state by
    // altering the $form_state variable, which is passed into them by
    // reference.
    ctools_process_form($form_id, $form, $form_state);
    // If we were told not to redirect, but not told to re-render, return
    // here.
    if (!empty($form_state['executed']) && empty($form_state['rerender'])) {
      return;
    }
    if ($cacheable && !empty($form['#cache']) && empty($form['#no_cache'])) {
      // Caching is done past drupal_process_form so #process callbacks can
      // set #cache. By not sending the form state, we avoid storing
      // $form_state['storage'].
      form_set_cache($form_build_id, $original_form, NULL);
    }
  }

  // Most simple, single-step forms will be finished by this point --
  // drupal_process_form() usually redirects to another page (or to
  // a 'fresh' copy of the form) once processing is complete. If one
  // of the form's handlers has set $form_state['redirect'] to FALSE,
  // the form will simply be re-rendered with the values still in its
  // fields.
  //
  // If $form_state['storage'] or $form_state['rebuild'] have been
  // set by any submit or validate handlers, however, we know that
  // we're in a complex multi-part process of some sort and the form's
  // workflow is NOT complete. We need to construct a fresh copy of
  // the form, passing in the latest $form_state in addition to any
  // other variables passed into drupal_get_form().

  if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) {
    $form = ctools_rebuild_form($form_id, $form_state, $args);
  }

  // If whoever is calling this wants the $form array (so that it can render it
  // another way, for example) then return it.
  if (!empty($form_state['want form'])) {
    return $form;
  }

  // Do not render certain items if requested not to:
  if (!empty($form_state['drop tokens'])) {
    unset($form['#id']);
    unset($form['#build_id']);
    unset($form['#token']);
    unset($form['form_token']);
  }
  // If we haven't redirected to a new location by now, we want to
  // render whatever form array is currently in hand.
  // Do not render certain items if requested not to:
  if (!empty($form_state['drop tokens'])) {
    unset($form['form_id']);
    unset($form['form_build_id']);
    unset($form['form_token']);
  }
  return drupal_render_form($form_id, $form);
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.