Drupal multiple node forms on one page

I have a view that displays multiple nodes. I want to place a node form below each displayed node. Both node_addand drupal_get_formdirectly template.php work fine, but I get a form with the same identifier form NODETYPE_node_form, and checking and sending are not working properly.

If you needed to place several node forms on one page, what would be your overall approach?

Progress so far ...

in template.php during pre-processing, node $ author_profile and $ content are set earlier.

$unique = $vars['node']->nid;

$node = new StdClass();
$node->uid = $vars['user']->uid;
$node->name = $vars['user']->name;
$node->type = 'review';
$node->language = '';
$node->title = t('Review of ') . $vars['node']->realname . t(' by ') . $vars['user']->realname . t(' on ') . $content->title;
$node->field_review_to_A[0]['nid'] = $nodeA->nid;
$node->field_review_to_B[0]['nid'] = $vars['node']->nid;
$node->field_review_to_profile[0]['nid'] = $author_profile->nid;

if(!function_exists("node_object_prepare")) {
    include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
}

//$vars['A_review_form'] = drupal_get_form('review_node_form', $node);
$vars['A_review_form'] = mymodule_view($node, $unique);

in mymodule module

function mymodule_view($node, $unique) {
    if(!function_exists("node_object_prepare")) {
        include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
    }
    $output = drupal_get_form('review_node_form_' . $unique, $node);
    return $output;
}

function mymodule_forms($form_id, $args) {
    $forms = array();
    if (strpos($form_id, "review_node_form_") === 0) {
        $forms[$form_id] = array('callback' => 'node_form');
    }
    return $forms;
}


function mymodule_form_alter(&$form, $form_state, $form_id) {
    if (isset($form['type']) && isset($form['#node']) && $form_id != $form['type']['#value'] .'_node_form' && $form['type']['#value'] == 'review') {
        $type = content_types($form['#node']->type);
        if (!empty($type['fields'])) {
            module_load_include('inc', 'content', 'includes/content.node_form');
            $form = array_merge($form, content_form($form, $form_state));
        }
        $form['#pre_render'][] = 'content_alter_extra_weights';
        $form['#content_extra_fields'] = $type['extra'];

        //$form['#id'] = $form_id;
        //$form['#validate'][0] = $form_id . '_validate';

        $form['title']['#type'] = 'value';
        $form['field_review_to_A']['#type'] = 'value';
        $form['field_review_to_B']['#type'] = 'value';
        $form['field_review_to_profile']['#type'] = 'value';
    }
}

Questions

My opinion on obscure issues ...

  • Is this a good general approach for displaying multiple node forms on the same page?
  • / content content_form_alter mymodule_form_alter? , content ?
  • $form['#id']? HTML node_form, , review_node_form_254. , , . $form['#validate'][0] . , $form[button]['#submit'][0] - ? node_form_submit.
  • ? , . ? - ? , ?
+3
3

, , .

, , , , , content_form_alter() - -.

, , hook_form_alter(), content_form_alter(), , ? - :

function modulename_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && 
  stripos($form_id, $form['type']['#value'] .'_node_form')) {
    ...
  }
}

, , : , MYCCKTYPE_node_form form_id ( es: MYCCKTYPE_node_form_234).

, ... !:)

:

  • , , hook_form_alter() CCK, , :

    && $form_id != $form['type']['#value'] .'_node_form'

  • , , CCK, ( , ).

0

hook_forms(), .

NODETYPE_node_form, , , (node_form()) node node_forms().

"" drupal_get_form().

+2

.

API- drupal - . , , , :

CCK adds its fields to your form through hook_form_alter (). But this is done based on form_id. Therefore, if the form identifier is different (because you want to have several on one page), you need to replicate the CCK code bit to add fields to your form independently.

That's what it does brilliantly.

+1
source

Source: https://habr.com/ru/post/1722514/


All Articles