You are here

function replicate_ui_confirm_submit in Replicate UI 7

Confirm submission.

File

./replicate_ui.module, line 170
Provide a user interface for the Replicate API.

Code

function replicate_ui_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
    $entity_type = $form_state['values']['entity_type'];
    $entity_id = $form_state['values']['entity_id'];

    // special case for field collection
    if ($entity_type == 'field_collection_item') {
      $entity = entity_load_single($entity_type, $entity_id);
      $new = replicate_clone_entity('field_collection_item', $entity);
      $new
        ->setHostEntity($entity
        ->hostEntityType(), $entity
        ->hostEntity());
      $new
        ->save();
      $path = 'field-collection/' . str_replace('_', '-', $new->field_name) . '/' . $new->item_id;
      $id = $new->item_id;
    }
    else {

      // load entity for replicate
      $id = replicate_entity_by_id($entity_type, $entity_id);
      $entities = entity_load($entity_type, array(
        $id,
      ));
      $entity_uri = entity_uri($entity_type, $entities[$id]);
      $path = $entity_uri['path'];
    }
    if ($id) {

      // Add replica id/type to form state.
      $form_state['replica_id'] = $id;
      $form_state['replica_type'] = $entity_type;

      // redirect to the new item
      drupal_set_message(t('%type (%id) has been replicated to id %new!', array(
        '%type' => $entity_type,
        '%id' => $entity_id,
        '%new' => $id,
      )));
      $form_state['redirect'] = $path;
      if (module_exists('rules')) {

        // Rules needs the entity in a wrapper, since we don't know beforehand
        // what type of entity we have.
        $original = entity_load_single($entity_type, $entity_id);
        $original_wrapped = entity_metadata_wrapper($entity_type, $original);
        $replica = entity_load_single($entity_type, $id);
        $replica_wrapped = entity_metadata_wrapper($entity_type, $replica);
        rules_invoke_event('replicate_ui_after_replication', $replica_wrapped, $entity_type, $original_wrapped);
      }
    }
  }
}