0

I've created a module that has a 'form builder' on the admin side. Once the form has been created, I store the form array in the database. Then on the front end I've created a custom field that pulls the form array from the database and displays it as part of a node. Here is the pseudo-code to show my thinking:

- Create form via admin side and store form array in database
- Create node with custom field, choose which form you wish to display
- Set url alias on the node
- When user views url, display the form as part of the node 

The problem I have is that when I complete the form on the front end and press submit, nothing happens. Looking at the html, it doesn't have a <form> tag, presumably because it's part of a node and not a hook_form.

So, I'm wondering if it's possible to display a submittable form as a custom field? Or do I need to find another way around this?

Edit: I'm using the following code to display the form:

function mantis_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

    $element = array();

    switch($display['type']) {
        case 'mantisform_default': {

            // Retrieve the form from the database
            $form_data = mantis_retrieve_form_data($items[0]['mantis_form_id'], 'display');

            $element[]['#markup'] = drupal_render($form_data);

        }
    }
    return $element;
}

Edit: $form_data structure can be seen here http://pastebin.com/JfeNGBGA and $element is here http://pastebin.com/HU5NSHd5

1

You have to check your field handler, node tpl.php or field tpl.php and use drupal_render($yourform), it's the cleanest solution I see.

  • I've edited the question to show the code I am using to display the form. I am using drupal_render already, so wondering if there is a difference between using drupal render in a template file as oppose to using it in hook_field_formatter_view? – littledynamo Feb 4 '13 at 17:01
  • 1
    What give your dpm($form_data) and dpm($element) ? – Gregory Kapustin Feb 4 '13 at 17:02
  • Sorry for the delay, I've added the structure of form data to pastebin pastebin.com/JfeNGBGA – littledynamo Feb 5 '13 at 9:05
  • Output for $element pastebin.com/HU5NSHd5 – littledynamo Feb 5 '13 at 9:22
0

Figured this one out. The reason the form wasn't submitting was because I wasn't calling drupal_get_form. It seems that drupal_get_form carries out a lot of essential processing that allows the form to function. Simply creating a form array and calling drupal_render is not enough to create a functional form.

The solution was to create an implementation of hook_form and retrieve my form data from the database:

function mantis_form($form, &$form_state) {

    $data = db_select('mantis_forms', 'f')
    ->fields('f', array('form_data'))
    ->condition('id', $form_state['build_info']['args'][0], '=')
    ->execute()
    ->fetchField();

    if($data) {
        $data = unserialize($data);

        // If we're on the admin side then disable the submit button
        if($form_state['build_info']['args'][1] == 'admin') {
            $data['form_wrapper']['submit_form']['#disabled'] = true;
        } else {
            // If we're on the front end then enable the submit button and remove the optional fields
            $data['form_wrapper']['submit_form']['#disabled'] = false;
            array_slice($data, 1, 1);
        }

        return $data;
    }
}

And then amend the hook_formatter_view to use drupal_get_form:

function mantis_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

    $element = array();

    switch($display['type']) {
        case 'mantisform_default': {

            $form_data = drupal_get_form('mantis_form', $items[0]['mantis_form_id'], 'display');

            $element[]['#markup'] = drupal_render($form_data);

        }
    }
return $element;
}
  • 1
    drupal_get_form before drupal_render. Good one, bye ! – Gregory Kapustin Feb 5 '13 at 10:30

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.