You are here

function inline_entity_form_field_attach_submit in Inline Entity Form 7

Implements hook_field_attach_submit().

File

./inline_entity_form.module, line 1486
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_field_attach_submit($parent_entity_type, $parent_entity, $form, &$form_state) {
  list(, , $bundle_name) = entity_extract_ids($parent_entity_type, $parent_entity);
  foreach (field_info_instances($parent_entity_type, $bundle_name) as $instance_name => $instance) {
    if (isset($instance['widget']) && strpos($instance['widget']['type'], 'inline_entity_form') === 0) {
      $field_name = $instance['field_name'];
      if (!isset($form[$field_name])) {

        // The field wasn't found on this form, skip it.
        // Usually happens on stub entity forms that don't contain all fields.
        continue;
      }
      $langcode = $form[$field_name]['#language'];
      if (!isset($form[$field_name][$langcode]['#ief_id'])) {

        // The field is present on the form, but the IEF widget wasn't added,
        // usually due to inline_entity_form_field_widget_properties_alter().
        continue;
      }
      $ief_id = $form[$field_name][$langcode]['#ief_id'];
      if (empty($form_state['inline_entity_form'][$ief_id])) {

        // No data found, no need to do anything.
        continue;
      }
      $values = $form_state['inline_entity_form'][$ief_id];
      $entity_type = $values['settings']['entity_type'];
      $controller = inline_entity_form_get_controller($instance);
      $context = array(
        'parent_entity_type' => $parent_entity_type,
        'parent_entity' => $parent_entity,
      );

      // Delete any entities staged for deletion.
      if (!empty($values['delete'])) {
        $controller
          ->delete(array_values($values['delete']), $context);
      }

      // Respect the entity weights.
      uasort($values['entities'], 'drupal_sort_weight');

      // Go through the IEF data and assemble a list of ids.
      $entity_ids = array();
      $need_reset = FALSE;
      foreach ($values['entities'] as $item) {
        if ($item['needs_save']) {
          $controller
            ->save($item['entity'], $context);
          $need_reset = TRUE;
        }
        list($entity_id) = entity_extract_ids($entity_type, $item['entity']);
        $entity_ids[] = array(
          $values['settings']['column'] => $entity_id,
        );
      }

      // Prevent the entity from showing up in subsequent add forms.
      // @todo Investigate a cleaner fix.
      if (isset($form['#op']) && $form['#op'] == 'add' && $need_reset) {
        $form_state['inline_entity_form'][$ief_id]['entities'] = array();
      }
      if (!empty($entity_ids)) {

        // Set the list of ids as the field value.
        $parent_entity->{$field_name}[$langcode] = $entity_ids;
      }
    }
  }
}