You are here

function inline_entity_form_form_alter in Inline Entity Form 7

Same name and namespace in other branches
  1. 8 inline_entity_form.module \inline_entity_form_form_alter()

Implements hook_form_alter().

Adds the IEF submit function and the #ief_submit_all flag to the main submit button of a form that contains an IEF widget. Needs to be done in an alter hook because many forms add the submit button after inline_entity_form_field_widget_form() is called.

File

./inline_entity_form.module, line 804
Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…

Code

function inline_entity_form_form_alter(&$form, &$form_state, $form_id) {
  if (!empty($form_state['inline_entity_form'])) {

    // Try to find the main submit button in the most common places.
    $submit_element = NULL;
    if (!empty($form['submit'])) {
      $submit_element =& $form['submit'];
    }
    elseif (!empty($form['actions']['submit'])) {
      $submit_element =& $form['actions']['submit'];
    }
    if ($submit_element) {

      // Merge the IEF submit handler with the button level submit handlers if
      // available. Otherwise use the form level submit handlers.
      if (!empty($submit_element['#submit'])) {
        $submit = array_merge(array(
          'inline_entity_form_trigger_submit',
        ), (array) $submit_element['#submit']);
      }
      else {
        $submit = array_merge(array(
          'inline_entity_form_trigger_submit',
        ), (array) $form['#submit']);
      }

      // Reduce any duplicates on the off chance the IEF submit handler was
      // already a part of the array used.
      $submit_element['#submit'] = array_unique($submit);
      $submit_element['#ief_submit_all'] = TRUE;
    }
  }
}