2015-12-23 02:16:02 +00:00
|
|
|
|
---
|
|
|
|
|
title: Programmatically Load an Entityform in Drupal 7
|
2020-03-08 14:32:13 +00:00
|
|
|
|
date: 2015-12-22
|
2020-03-08 17:52:59 +00:00
|
|
|
|
excerpt:
|
|
|
|
|
How to programmatically load, render and embed an entityform in Drupal 7.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
tags:
|
2020-03-08 17:52:59 +00:00
|
|
|
|
- drupal
|
|
|
|
|
- drupal-7
|
|
|
|
|
- drupal-planet
|
|
|
|
|
- entityform
|
2015-12-23 02:16:02 +00:00
|
|
|
|
---
|
2020-03-08 17:52:59 +00:00
|
|
|
|
|
|
|
|
|
I recently had my first experience using the
|
|
|
|
|
[Entityform module](https://www.drupal.org/project/entityform) in a project. It
|
|
|
|
|
was quite easy to configure with different form types, but then I needed to
|
|
|
|
|
embed the form into an overlay. I was expecting to use the `drupal_get_form()`
|
|
|
|
|
function and render it, but this didn’t work.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
|
|
|
|
|
Here are the steps that I took to be able to load, render and embed the form.
|
|
|
|
|
|
|
|
|
|
## Loading the Form
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The first thing that I needed to do to render the form was to load an empty
|
|
|
|
|
instance of the entityform using `entityform_empty_load()`. In this example,
|
|
|
|
|
`newsletter` is the name of my form type.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```language-php
|
2015-12-23 02:16:02 +00:00
|
|
|
|
$form = entityform_empty_load('newsletter');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This returns an instance of a relevant `Entityform` object.
|
|
|
|
|
|
|
|
|
|
## Rendering the Form
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The next step was to be able to render the form. I did this using the
|
|
|
|
|
`entity_form_wrapper()` function.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
As this function is within the `entityform.admin.inc` file and not autoloaded by
|
|
|
|
|
Drupal, I needed to include it using `module_load_include()` so that the
|
|
|
|
|
function was available.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```language-php
|
2015-12-23 02:16:02 +00:00
|
|
|
|
module_load_include('inc', 'entityform', 'entityform.admin');
|
2017-03-16 08:09:52 +00:00
|
|
|
|
|
2015-12-23 02:16:02 +00:00
|
|
|
|
$output = entityform_form_wrapper($form, 'submit', 'embedded'),
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The first argument is the `Entityform` object that was created in the previous
|
|
|
|
|
step (I’ve [submitted a patch](https://www.drupal.org/node/2639584) to type hint
|
|
|
|
|
this within entityform so that it’s clearer what is expected), which is
|
|
|
|
|
required.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
The other two arguments are optional. The second argument is the mode (`submit`
|
|
|
|
|
is the default value), and the last is the form context. `page` is the default
|
|
|
|
|
value, for use on the submit page, however I changed this to `embedded`.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
I could then pass this result into my theme function to render it successfully
|
|
|
|
|
within the relevant template file.
|
2015-12-23 02:16:02 +00:00
|
|
|
|
|
|
|
|
|
## Resources
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
- [The entityform module](https://www.drupal.org/project/entityform)
|
|
|
|
|
- [My issue and patch to add the type hint to the entityform_form_wrapper function](https://www.drupal.org/node/2639584)
|