Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
i want to provide possibility to edit another node in field widget. But how to submit node which form is included (by fubform element) in field widget and give back to widget the NID of saved node (for create relation to new node)?
Example of my code, which properly load subform but preview and save doesn't work:
/**
* Implements of hook_field_widget_form().
*/
function relation_edit_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$widget = $instance['widget'];
$settings = $widget['settings'];
if(isset($items[$delta]))
$related_node = node_load($items[$delta]);
else {
global $user;
$related_node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $settings['bundle']);
}
$element['node_' . $delta] = array(
'#type' => 'subform',
'#subform_id' => 'node_form',
'#subform_file' => array('module' => 'node', 'name' => 'node.pages'),
'#subform_arguments' => array($related_node, $settings),
'#subform_default_triggering_element' => array('actions' => 'submit'),
'#required' => FALSE,
);
//dpm($element);
return $element;
}
Comments
Comment #1
casey CreditAttribution: casey commentedCool use case! Don't you have a git sandbox or something, so I can check it out/see how to fix it?
Comment #2
Honza Pobořil CreditAttribution: Honza Pobořil commentedIt will be there in a few minutes: http://drupal.org/sandbox/bobik/1195712
Comment #4
casey CreditAttribution: casey commentedI am sorry but the whole relation dummy field doesn't seem to be usable to create/save relations. Relations aren't stored in the field data so you can't retrieve them and their target entities per field index.
So this is entirely incorrect (but there isn't really a correct way to do it):
I think the dummy field is only created to display relations as a field so you can use different formatters.
Anyways, to get the subforms submitting while pressing the save button on the parent form you can:
Comment #5
Honza Pobořil CreditAttribution: Honza Pobořil commentedI am using patched version of relation_dummy_field and there is loaded related NIDs, but it seems that is not propper way. I am going to update my module to work with master version of relation_dummy field.
Comment #6
Honza Pobořil CreditAttribution: Honza Pobořil commentedTry the newest version. Now it loads relations and multiple values is handled in one widget, so clear cache, please.
Comment #7
casey CreditAttribution: casey commentedOw wait,
should be
Else it would add the submit handler multiple times if your form contains multiple subforms.
Comment #8
Honza Pobořil CreditAttribution: Honza Pobořil commentedor $form variable?
Comment #9
casey CreditAttribution: casey commented$form argument in hook_widget_form() is not necessarily the actual $form element.
Comment #10
Honza Pobořil CreditAttribution: Honza Pobořil commentedI've updated this, but it still doesn't work. (checkout last commit)
Comment #11
casey CreditAttribution: casey commentedOww of course, hook_widget form is called before form_builder() is run. There is no complete form yet.
Add this to relation_edit_field_widget_form():
And this extra function:
Comment #12
Honza Pobořil CreditAttribution: Honza Pobořil commentedNo, it still does not work (related nodes were not updated).
Comment #13
casey CreditAttribution: casey commentedWell thats because you hide all submit buttons on the subform. If you expose those, it works.
But I see why you need to be able to have subforms without submit buttons, so I'll work out a solution.
Comment #14
casey CreditAttribution: casey commentedActually it is possible. You had '#subform_default_triggering_element' wrong
should be
Comment #15
Honza Pobořil CreditAttribution: Honza Pobořil commentedRepaired, but still doesn't work. Btw, you have the same mistake in subform_example.module on the line 289.
Comment #16
casey CreditAttribution: casey commentedHave you updated to latest subform version?
Comment #17
Honza Pobořil CreditAttribution: Honza Pobořil commentedYes, after update Subform it is working now. Thank you.
How do you recommend to implement creating new nodes? I have prepared creating new nodes, but I don't know how to get back NID of new node to create relation.
Comment #18
casey CreditAttribution: casey commentedTo save you some troubles when debugging, note that sessions have a storage limit. if you dsm($form_state), and the form is submitted and redirected (which means that $_SESSION['messages'] is going to be stored), you'll likely don't see any status messages at all. $form_state is way too large to be stored in session storage.
I ran into this while getting this to work.
Comment #19
ejustice CreditAttribution: ejustice commentedDid you ever get this working? Would you be willing to share your code?
Comment #20
casey CreditAttribution: casey commentedhttp://drupal.org/project/relation_edit
Comment #21
Honza Pobořil CreditAttribution: Honza Pobořil commentedThank you, casey. I recommend to use dev release. It has been rewrited for more clear code, but still missing some features what green release have.
Comment #22
ejustice CreditAttribution: ejustice commentedThank you!