1

I have an entityform called 'foia_generator' and I'm trying to embed it as a contact form on a page (the tutorial I used is for embedding entityforms in blocks) ... hence my problem...

function foiagen_form($form, &$form_state) {
    module_load_include('inc', 'entityform', 'entityform.admin');
    $entity_form_name = 'foia_generator';
    $render_estimate_form =
    entityform_form_wrapper(entityform_empty_load($entity_form_name), 'submit', 'embedded');
    echo drupal_render($render_estimate_form); 
    return $form;
}

This displays the form but it's not 'inside' the normal bounds of the site (using bootstrap subtheme) i.e. it's not rendered properly - it appears before the regular divs like the navbar etc... and spans the entire width of my screen.

If I can't do it this way can I programattically embed the 'entityform' on my 'contact page' as an entityreference field, (rendered)?

Thanks

2

Here's a function I made to embed any entityform:

function my_module_entityform_form($form, &$form_state, $entityform_name) {
  form_load_include($form_state, 'inc', 'entityform', 'entityform.admin');
  $entityform = entityform_create(array('type' => $entityform_name));

  return entityform_edit_form($form, $form_state, $entityform, 'submit', 'embed');
}

And then you can call it using your specific entityform name as a Drupal form:

$form = drupal_get_form('my_module_entityform_form', 'entityform_name');

This is good because it uses form_load_include instead of module_load_include, so the validate and submit functions will also have access to entityform.admin.inc.

2
  • 1
    This is useful, but makes it tricky to use hook_form_alter as the $form_id will always be 'my_module_entityform_form' – Chris Jun 8 '17 at 1:54
  • I believe you could use hook_form along with drupal_build_form (or whatever it is) in order to create a variable form_id -- allowing unique form alters. – Aronanda Jun 9 '17 at 5:19
2

Seems like a odd piece of code you've got there, for instance this will render your form randomly in the page:

    echo drupal_render($render_estimate_form); 

Which is probably what you're noticing already, that example you're linking is for render that piece of code inside a template but for what you describe you're rendering it in a form inside a block, I'd try something like this instead (untested):

function foiagen_form($form, &$form_state) {
    module_load_include('inc', 'entityform', 'entityform.admin');
    $entity_form_name = 'foia_generator';
    $render_estimate_form =
    entityform_form_wrapper(entityform_empty_load($entity_form_name), 'submit', 'embedded');
    $form['foiagen'] = $render_estimate_form;
    return $form;
}
2
  • Ok, so is it the 'echo' that causes the form to be rendered before everything else? Is the code odd in other ways? – iain maitland Jul 13 '14 at 16:32
  • Yeah, that's it, the rest looks fine but you'll probably want to tweak the form for your specific needs. – pcambra Jul 13 '14 at 19:52

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.