You are here

function slickgrid_callback_clone in Slickgrid 7

Callback function - clone an entity

File

includes/slickgrid.callbacks.inc, line 94

Code

function slickgrid_callback_clone() {
  $entities = entity_load($_POST['entity_type'], $_POST['entity_ids']);
  $entity_keys = slickgrid_get_entity_keys($_POST['entity_type'], array(
    'bundle',
    'label',
  ));
  foreach ($entities as $entity) {
    $clone = clone $entity;

    // Unset all entity keys to create a new entity
    foreach ($entity_keys as $entity_key) {
      if (property_exists($clone, $entity_key)) {
        unset($clone->{$entity_key});
      }
    }
    entity_save($_POST['entity_type'], $clone);
  }
  drupal_set_message(format_plural(count($_POST['entity_ids']), 'Cloned 1 item.', 'Cloned @count items.'));

  // Reload the view
  $view = slickgrid_get_view($_POST['view'], $_POST['display_id']);

  // Return the view data to be reloaded in the grid
  return array(
    'data' => $view->data,
  );
}