0

I have EntityA which has one or more EntityB's which have one or more EntityC's. EntityB references values of EntityA and EntityC references values of EntityB. My current plan is to have an id field of EntityB reference the id of EntityA and the same for EntityC referencing EntityB. Normal one-to-many relationships, possibly with foreign keys defined so mysql will enforce referential integrity.

I am trying to use the Subform module to build the lists of required forms inside the edit form for EntityA. EntityA builds the settings for a new EntityB into an object and passes that to EntityB's form via Subform '#subform_arguments'.

My question is about how to save EntityB and EntityC forms. Should I save EntityB in the submit function for EntityA or try to communicate EntityA's id to the EntityB submit function and have it save its own entity? If the save logic for EntityB goes in the submit function for EntityA's form, how do I retrieve the subform values to save?

1

This is working now with entity saving logic neatly packaged with the sub-item entity code. Before saving the subitems, I set the value of a hidden element with the parent entity's id. I modified the wrapper form's submit handler array:

    '#submit' => array(
        'wrapper_form_submit',
        'update_subforms_before_save',
        'subform_submit_all', 
    ),

First, the wrapper is saved and is assigned an id. Then it updates the subforms with that id. And finally the subforms are submitted. The function to update the subforms is modeled after subform_submit_all():

function update_subforms_before_save(&$form, &$form_state) {

    $wrapper_item_id = $form_state['values']['item_id'];

    if (!empty($form_state['temporary']['subform'])) {
        foreach ($form_state['temporary']['subform'] as $subform_name => &$subform_state) {

            $subform_element = &subform_array_get_nested_value($form, $subform_state['subform_element_parents'], $subform_exists);
            if ($subform_exists) {

                switch($subform_element['#subform_id']) {
                    case 'subitem_entity_form' :
                        $subform_state['values']['parent_item_id'] = $wrapper_item_id;
                        break;
                }

            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.