You are here

function slickgrid_callback_add in Slickgrid 7

Same name and namespace in other branches
  1. 7.2 includes/slickgrid.callbacks.inc \slickgrid_callback_add()

Callback function - add an entity

File

includes/slickgrid.callbacks.inc, line 123

Code

function slickgrid_callback_add($entity_type, $bundle_type, $view_name, $display_id) {
  global $user;
  $entity_info = entity_get_info($entity_type);

  // We've defined add settings for this in slickgrid_entity_info_alter()
  $form_id = $entity_info['bundles'][$bundle_type]['add']['callback'];
  if ($entity_info['bundles'][$bundle_type]['add']['file']) {
    require_once $entity_info['bundles'][$bundle_type]['add']['file'];
  }
  ctools_include('modal');
  ctools_include('ajax');
  $form_state = array(
    'title' => t('Add @label', array(
      '@label' => $entity_info['bundles'][$bundle_type]['label'],
    )),
    'ajax' => TRUE,
  );
  if (function_exists($entity_info['creation callback'])) {
    $entity = call_user_func($entity_info['creation callback'], array(
      'type' => $bundle_type,
      'name' => $user->name,
    ));
    $form_state['build_info']['args'] = array(
      $entity,
    );
  }
  $output = ctools_modal_form_wrapper($form_id, $form_state);
  if (!empty($form_state['executed'])) {

    // We'll just overwrite the form output if it was successful.
    $output = array();
    $output[] = ctools_modal_command_dismiss();
    $result = array();
    slickgrid_callback_get_messages($result);

    // Reload the view
    $view = slickgrid_get_view($view_name, $display_id);
    $result['data'] = $view->data;
    $output[] = array(
      'command' => 'slickgrid',
      'response' => array(
        'result' => $result,
      ),
    );
  }
  print ajax_render($output);
  exit;
}