0

I'm having a problem displaying a cached multi-step form in a custom module which generates custom entities.

  1. After two steps, the form posts to an external URL (action changed with hook_form_form_id_alter).
  2. If the external URL returns errors, the form is pulled from cache with form_get_cache() and displayed using drupal_render().
  3. Form displays okay, but field elements are missing name and value attributes.
  4. Printing the form object retrieved from cache shows expected values (#entity, fields, and user input associated with fields).

      $form_state = array(
        'submitted' => FALSE,
        'rebuild' => FALSE,
        'cache' => TRUE,
      );
    
      $form = form_get_cache($form_build_id, $form_state);
    
      $output = drupal_render($form);
    

I suspect that I need to rebuild $form_state input/values or am missing a form rebuild step.

0

Figured it out. Use form_get_cache() to get the cached entity, throw that into $form_state, and then use drupal_build_form() to put it together again.

    $form_state = array(
        'submitted' => FALSE,
        'rebuild' => FALSE,
        'cache' => TRUE,
    );

    // Pull the entity object from cache.
    $cached_form = form_get_cache($form_build_id, $form_state);
    $myentity = $cached_form['#entity'];

    $form_state['myentity'] = $myentity;
    $output = drupal_build_form('myentity_form', $form_state);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.