Last updated 25 March 2014. Created on 3 November 2008.
Edited by henrikakselsen, orjantorang, aufumy, meba. Log in to edit this page.

There are times when you need to embed a form in a row of views display - as a field. Imagine an ecommerce module listing products using Views and needing a "Add to cart" button for every product. See screenshot.

Here comes Views Embed Form module. It's available for 6.x+ Views module.

It allows module developers to embed forms to Views display as fields. For security reasons, it's not possible to embed ANY Drupal form, it needs to be whitelisted first.

User howto

So you have installed Views Embed Form and some module which allows embedding its form in a View. You can now click Views -> Add/Edit view and add a new Field. Select category Embedded and click Embedded: form. Select desired form in a box and save.

Developer howto

You need to embed your form in a view? There is a simple hook_views_embed_form() API call which allows that. Implement this call to allow embedding your form in a display. This call should return an array in a form:

array('form_name' => t('Form description'))

Always honor your permissions in this call. It is used two times:

  1. When editing/adding a view
  2. When displaying results

If the form should be accessible for administrators only ("administer content" permission), do not forget to ensure it is checked in your API hook call. If user doesn't have access to this permission, he will get You do not have permission to access embedded form. message.

Example

function testmodule_views_embed_form() {
  if (user_access('administer content')) {
    return array(
        // Key in this array is the name of a form and also the name of a form function.
        'testmodule_form' => t('Form for testing module'),
        // Value in this array is a human-readable name of the form, use t() to allow internationalization.
        'testmodule_basket_form' => t('Add to basket form'),
        );
  }
}

Download

See Views Embed Form module page.

AttachmentSize
views_embed_form.png8.38 KB

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Tiquelou’s picture

Looks good. But I am not being able to figure out where exactly to declare this function. Can you tell me if I have to make the change to the .module file of this module or the one from which I Intend to use the form? Is there anything else I have to keep in mind when I want to use a Form in a View?

DjebbZ’s picture

You have to declare this function in a new module where you will declare the form.

beautifulmind’s picture

Hello,
Thank you very much for such a useful module.
I've been trying to get the contact forms (site wide & personal) to be available in the embed forms but it didn't work. I have created a custom module. Here is the code:

function contact_embed_view_embed_form() {
	return array(
    'contact_mail' => t('Site wide Contact form'),
    'contact_mail_user' => t('User\'s contact form'),
	);
}

Is it possible to embed already created form like drupal's form or forms from any otherm module?
I really appreciate your reply.

Regards.

[edit]
Never mind, I have found the issue, the function name was written wrong

Thanks a million.

jamuy’s picture

There are plans to migrate to Views3.

I'll start to try, if I do, communicate the results.

Greetings and thank you very much for the idea.