You are here

function civicrm_entity_form_submit in CiviCRM Entity 7.2

Same name and namespace in other branches
  1. 7 civicrm_entity.module \civicrm_entity_form_submit()

Form API submit callback for the entity form.

1 string reference to 'civicrm_entity_form_submit'
civicrm_entity_form in ./civicrm_entity.default_form.inc

File

./civicrm_entity.default_form.inc, line 419

Code

function civicrm_entity_form_submit(&$form, &$form_state) {
  $entity_type = $form['#entity_type'];
  $entity = $form['#entity'];
  civicrm_initialize();
  $civicrm_entity_fields = civicrm_api(substr_replace($entity_type, '', 0, 8), 'getfields', array(
    'version' => 3,
    'action' => 'create',
  ));

  // unset form and entity values if civicrm properties are hidden from the form by display suite
  if (module_exists('ds_forms')) {
    if ($ds_form = ds_build_load($form, $form['#form_id'])) {
      if ($layout = ds_get_layout($ds_form->entity_type, $ds_form->bundle, 'form', FALSE)) {
        foreach ($layout['settings']['fields'] as $field_name => $visibility) {
          if ($field_name != 'id' && $visibility == 'hidden' && strpos($field_name, 'field_') !== 0) {
            unset($form_state['values'][$field_name]);
            unset($entity->{$field_name});
          }
        }
      }
    }
  }
  foreach ($form_state['values'] as $key => $value) {

    //need to convert checkbox values to a consumable format for checkboxes custom fields
    if (substr($key, 0, 7) == 'custom_') {
      if ($civicrm_entity_fields['values'][$key]['html_type'] == 'CheckBox') {
        if (is_array($value)) {
          $stored_value = array();
          foreach ($value as $option => $checkbox_value) {
            if ($checkbox_value) {
              $stored_value[] = $option;
            }
          }
          $entity->{$key} = !empty($stored_value) ? $stored_value : array(
            '',
          );
        }
      }
      else {
        if (is_array($value) && isset($value['value'])) {
          $value = $value['value'];
        }
        $entity->{$key} = $value;
      }
      if (!empty($civicrm_entity_fields['values'][$key]['is_view'])) {
        unset($entity->{$key});
      }
    }
    else {
      if (is_array($value) && isset($value['value'])) {
        $value = $value['value'];
      }
      $entity->{$key} = $value;
    }
  }

  // Add in created and changed times.
  if ($entity->is_new = isset($entity->is_new) ? $entity->is_new : 0) {
    $entity->created = time();
  }
  $entity->changed = time();
  if ($entity_type == 'civicrm_case') {
    unset($entity->creator_id);
  }
  $wrapper = entity_metadata_wrapper($entity_type, $entity);

  //field_attach_submit($entity_type, $entity, $form, $form_state);

  //$wrapper->save();

  // handling for civicrm entity reference fields on civicrm entities
  $target_id_columns = array();
  if ($entity->is_new) {
    foreach ($form as $key => $form_field) {
      if (strpos($key, 'field_') === 0) {
        $field = field_info_field($key);
        if ($field['type'] == 'civicrm_entity_reference') {
          $ief_id = $form_field['und']['#ief_id'];
          $host_entity_type = $form_state['inline_entity_form'][$ief_id]['instance']['entity_type'];
          if ($host_entity_type == $entity_type) {
            $target_id_columns[$key] = array(
              $field['settings']['host_source_id'] => $field['settings']['target_id_column'],
            );
          }
        }
      }
    }
    if (!empty($target_id_columns)) {

      // first save the entity
      $wrapper
        ->save();

      // then update civicrm entity target id values with host id
      foreach ($target_id_columns as $key => $target_id_column) {
        $host_id = key($target_id_column);
        $target_id = reset($target_id_column);
        $ief_id = $form[$key]['und']['#ief_id'];
        foreach ($form_state['inline_entity_form'][$ief_id]['entities'] as $item_index => $item) {
          if (!empty($form_state['inline_entity_form'][$ief_id]['entities'][$item_index]['entity']->is_new)) {
            $form_state['inline_entity_form'][$ief_id]['entities'][$item_index]['entity']->{$target_id} = $entity->{$host_id};
          }
        }
      }

      // this will create the civicrm entity reference fields entities
      field_attach_submit($entity_type, $entity, $form, $form_state);
    }
    else {
      field_attach_submit($entity_type, $entity, $form, $form_state);
      $wrapper
        ->save();
    }
  }
  else {
    field_attach_submit($entity_type, $entity, $form, $form_state);
    $wrapper
      ->save();
  }
  $t_args = array(
    '%label' => entity_label($entity_type, $entity),
  );
  $destination = drupal_get_destination();
  $request_path = request_path();
  if (isset($destination['destination']) && $destination['destination'] != '' && $destination['destination'] != $request_path) {
    $form_state['redirect'] = $destination['destination'];
  }
  else {
    $form_state['redirect'] = str_replace('_', '-', $entity_type) . '/' . $entity->id;
  }
  drupal_set_message(t('Drupal fields and %label properties have been updated.', $t_args));
  drupal_redirect_form($form_state);
}