Views Embed Form: Embedding a form in a Views display
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 it's 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_view_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:
- When editing/adding a view
- 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
<?php
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.
| Attachment | Size |
|---|---|
| views_embed_form.png | 8.38 KB |
