You are here

function entityconnect_form_alter in Entity connect 7

Same name and namespace in other branches
  1. 8.2 entityconnect.module \entityconnect_form_alter()
  2. 7.2 entityconnect.module \entityconnect_form_alter()

Implements hook_form_alter().

If we are adding a new entity we pass of to entityconnect_add_form_alter if we are returning to the parent form we hand off to entityconnect_return_form_alter.

File

./entityconnect.module, line 165
Handles the main hooks used by entityconnect.

Code

function entityconnect_form_alter(&$form, &$form_state, $form_id) {
  $child = isset($_REQUEST['child']);

  // We can get the cid two different ways
  // first we try the $_REQUEST param.  if we do not getting it from there we
  // try the arg(3) if we are on a add form.  Also if we are on an add form we
  // know that we are a child page.
  if (isset($_REQUEST['build_cache_id']) && ($cid = $_REQUEST['build_cache_id']) || arg(1) == 'add' && ($cid = arg(3)) && ($child = TRUE) || arg(1) == 'people' && arg(2) == 'create' && ($cid = arg(3)) && ($child = TRUE) || arg(2) == 'taxonomy' && arg(4) == 'add' && ($cid = arg(5)) && ($child = TRUE) || arg(2) == 'taxonomy' && arg(3) == 'add' && ($cid = arg(4)) && ($child = TRUE) || arg(2) == 'entity-type' && arg(5) == 'add' && ($cid = arg(6)) && ($child = TRUE)) {
    $cache = entityconnect_cache_get($cid);
    entityconnect_include_form();
    if ($child) {
      $form_state['#entityconnect_child_form'] = $cache;
    }
    if (isset($_REQUEST['return']) && $cache->data['form']['#form_id'] == $form_id) {
      unset($_REQUEST['build_cache_id']);
      entityconnect_return_form_alter($form, $form_state, $form_id, $cid, $cache);
    }
  }

  // If this form is a child form let's add alter for that purpose
  // Note that we are doing this here becuase when we retrun to a form it gets
  // rebuilt so this will get caught in the rebuilt.
  if (isset($form_state['#entityconnect_child_form']) && $form_state['#entityconnect_child_form']) {
    $cache = $form_state['#entityconnect_child_form'];
    module_load_include('inc', 'entityconnect', 'includes/entityconnect.form');
    entityconnect_child_form_alter($form, $form_state, $form_id, $cache->cid, $cache);
  }
  if ($form_id == 'field_ui_field_edit_form') {

    // Use to add choice field.
    if ($form['#field']['type'] == 'entityreference' || $form['#field']['type'] == 'node_reference' || $form['#field']['type'] == 'user_reference') {
      $instance = $form['#instance'];
      $field = $form['#field'];
      $additions = module_invoke('entityconnect', 'field_instance_settings_form', $field, $instance);
      if (is_array($additions) && isset($form['instance'])) {
        $form['instance'] += $additions;
      }
    }
  }
}