You are here

function entityconnect_add_edit_button_submit in Entity connect 7

Same name and namespace in other branches
  1. 7.2 includes/entityconnect.form.inc \entityconnect_add_edit_button_submit()

Call when a new entity is to be added or edited.

We cache the current state and form and redirect to the add or edit page with an append build_cached_id.

2 string references to 'entityconnect_add_edit_button_submit'
entityconnect_add_form_element_edit in includes/entityconnect.form.inc
Here we attach a "Edit" submit button.
entityconnect_add_form_element_new in includes/entityconnect.form.inc
Here we attach a "Add" submit button.

File

includes/entityconnect.form.inc, line 278
Handles all form alters and submit functions for entityconnect.

Code

function entityconnect_add_edit_button_submit(&$form, &$form_state) {
  $cache_id = "entityconnect-" . $form['#build_id'];
  $field = $form_state['clicked_button']['#field'];
  $key = $form_state['clicked_button']['#key'];
  $entity_type = $form_state['clicked_button']['#entity_type_target'];
  $acceptable_types = isset($form_state['clicked_button']['#acceptable_types']) ? $form_state['clicked_button']['#acceptable_types'] : NULL;
  $field_info = field_info_field($field);

  // Get the list of all parents element for the clicked button.
  $parents = isset($form_state['clicked_button']['#parents']) ? $form_state['clicked_button']['#parents'] : NULL;
  $key_exists = NULL;
  $field_container = entityconnect_array_get_nested_value($form_state['input'], $parents, $key_exists);

  // Initialize target_id.
  $target_id = '';
  if ($field_info['module'] == 'entityreference') {
    if ($key_exists) {
      if (isset($field_container['_weight'])) {
        $target_id = isset($field_container['target_id']) ? $field_container['target_id'] : '';
      }
      elseif (is_array($field_container)) {
        foreach ($field_container as $key => $value) {
          if (is_array($value)) {
            foreach ($value as $key2 => $value2) {
              if (!is_null($value2)) {
                $target_id[$key2] = $value2;
              }
            }
          }
          else {
            $target_id = $value;
          }
        }
      }
    }
    if (is_array($target_id) && count($target_id) == 1) {
      $target_id = array_shift($target_id);
    }

    // TODO : Find the original problem of the entity connect use with
    // Field Collection.
    if (!empty($target_id) && !is_array($target_id)) {

      // Take "label (entity id)', match the id from parenthesis.
      if (preg_match("/.+\\((\\d+)\\)/", $target_id, $matches)) {
        $target_id = $matches[1];
      }
    }
  }
  elseif ($field_info['module'] == 'node_reference') {
    if ($key_exists) {
      $target_id = isset($field_container['nid']) ? $field_container['nid'] : '';
      if ($target_id == '' && is_array($field_container)) {
        foreach ($field_container as $key => $value) {
          if (is_array($value)) {
            foreach ($value as $key2 => $value2) {
              if (!is_null($value2)) {
                $target_id[$key2] = $value2;
              }
            }
          }
          else {
            $target_id = $value;
          }
        }
      }
    }

    // TODO : Find the original problem of the entity connect use with
    // Field Collection.
    if (!empty($target_id)) {

      // Take "label (entity id)', match the id from parenthesis.
      if (preg_match("/.+\\[nid:(\\d+)\\]/", $target_id, $matches)) {
        $target_id = $matches[1];
      }
    }
  }
  elseif ($field_info['module'] == 'user_reference') {
    if ($key_exists) {
      $target_id = isset($field_container['uid']) ? $field_container['uid'] : '';
      if ($target_id == '' && is_array($field_container)) {
        foreach ($field_container as $key => $value) {
          if (is_array($value)) {
            foreach ($value as $key2 => $value2) {
              if (!is_null($value2)) {
                $target_id[$key2] = $value2;
              }
            }
          }
          else {
            $target_id = $value;
          }
        }
      }
    }

    // TODO : Find the original problem of the entity connect use with
    // Field Collection.
    if (!empty($target_id)) {

      // Take "label (entity id)', match the id from parenthesis.
      if (preg_match("/.+\\[uid:(\\d+)\\]/", $target_id, $matches)) {
        $target_id = $matches[1];
      }
    }
  }

  // If no entity has been chosen to edit, redirect to the original node.
  if (!$form_state['clicked_button']['#add_child'] && $target_id == '_none') {
    drupal_set_message(t('You must select at least one entity to update.'), 'error');
    $form_state['redirect'] = $_GET['q'];
    return;
  }
  $params = array();
  foreach ($_GET as $key => $value) {
    if ($key == 'q') {
      continue;
    }
    $params[$key] = $value;
  }
  $data = array(
    'form' => $form,
    'form_state' => $form_state,
    'dest' => $_GET['q'],
    'params' => $params,
    'field' => $field,
    'field_info' => $field_info,
    'key' => $key,
    'add_child' => $form_state['clicked_button']['#add_child'],
    'target_id' => $target_id,
    'target_entity_type' => $entity_type,
    'acceptable_types' => $acceptable_types,
  );
  entityconnect_cache_set($cache_id, $data);
  unset($_GET['destination']);
  if ($data['add_child']) {
    $form_state['redirect'] = "admin/entityconnect/add/{$cache_id}";
  }
  else {
    if ($data['target_id']) {
      $form_state['redirect'] = "admin/entityconnect/edit/{$cache_id}";
    }
  }
}