Currently there is no general way to re-use entity forms.

Attached patch implements another entity helper, entity_form() which allows that. The patch works for me with all core entity types, as well as entity types using the entity API admin ui. We should add some tests for the form though.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fago’s picture

FileSize
10.43 KB

Somehow the vocabulary callback got lost, new patch attached.

fago’s picture

and one for drush make users

fago’s picture

FileSize
900 bytes

this is a patch for profile2 to support entity_form().

scroogie’s picture

Very interesting, subscribing.

fago’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

amitaibu’s picture

Status: Closed (fixed) » Needs work
+++ contrib/profile2_page.moduleundefined
@@ -163,6 +164,15 @@ function profile2_page_uri_callback($profile) {
+  form_load_include($form_state, 'inc', 'profile2_page');

$form_state is undefined. Should it be an empty array?

I'm looking at it for Message - I'd like to provide a UI for users to CRUD message (i.e. message instance not message type).

sun’s picture

Oink.

+++ entity.module
@@ -411,6 +418,38 @@ function entity_access($op, $entity_type, $entity = NULL, $account = NULL) {
+    return $info['form callback']($entity, $entity_type);

First arguments should obviously be $form, $form_state...

:(

+++ entity.module
@@ -953,7 +995,7 @@ function entity_forms($form_id, $args) {
+function entity_ui_get_form($entity_type, $entity, $op = 'edit', $form_state = array()) {

$form_state is never empty in D7. Should at least be form_state_defaults(), prepared and provided by the function invoking the hook.

fago’s picture

Status: Needs work » Needs review
FileSize
534 bytes
567 bytes

First arguments should obviously be $form, $form_state...

entity_form() is supposed to build and process the form for you - there is no possibility to embed forms in others or similar. That way there is no point or possibility to customize $form and $form_state.
This would be certainly nice, but I fear that would basically require a subform element for d7...

$form_state is never empty in D7.

Right. Fix attached, please review.

Status: Needs review » Needs work

The last submitted patch, profile2_form_defaults.patch, failed testing.

fmizzell’s picture

sub

tim.plunkett’s picture

entity_form() is supposed to build and process the form for you - there is no possibility to embed forms in others or similar. That way there is no point or possibility to customize $form and $form_state.

This prevents entity_form from being used in ctools modals. That needs a custom form_state.
A possible workaround for that is entity_metadata_form_id_* to complement entity_metadata_form_*.

function entity_metadata_form_id_node(&$form, &$form_state, $node) {
  // Pre-populate the form-state with the right form include.
  $form_state['build_info']['args'] = array($node);
  form_load_include($form_state, 'inc', 'node', 'node.pages');
  return $node->type . '_node_form';
}
function entity_metadata_form_node(&$form, &$form_state, $node) {
  $form_id = entity_metadata_form_id_node($form, $form_state, $node);
  return drupal_build_form($form_id, $form_state);
}

Unsure about the &$form part, but this would really help with the ctools modal stuff.

fago’s picture

Status: Needs work » Fixed

I've committed the fix from #9.

ad #12 : I see. Well we could add an optional parameter $form_state to entity_form() what allows prepopulating it that way. Anyway, let's please use a separate issue for improving this.

tim.plunkett’s picture

Automatically closed -- issue fixed for 2 weeks with no activity.