ctools_context_ajax_item_edit

  1. drupal
    1. 6 context-admin.inc function
    2. 7 context-admin.inc function
Drupal 7 ctools_context_ajax_item_edit($mechanism = NULL, $type = NULL, $cache_key = NULL, $position = NULL, $step = NULL)

Ajax entry point to edit an item

1 string reference to 'ctools_context_ajax_item_edit'

File

sites/all/modules/ctools/includes/context-admin.inc, line 487
includes/common-context.inc Provide API for adding contexts for modules that embed displays.

Code

function ctools_context_ajax_item_edit($mechanism = NULL, $type = NULL, $cache_key = NULL, $position = NULL, $step = NULL) {
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('context');
  ctools_include('cache');
  ctools_include('plugins-admin');

  if (!isset($position)) {
    return ctools_ajax_render_error();
  }

  // Load stored object from cache.
  if (!($object = ctools_cache_get($mechanism, $cache_key))) {
    ctools_ajax_render_error(t('Invalid object name.'));
  }

  $type_info = ctools_context_info($type);

  // Create a reference to the place our context lives. Since this is fairly
  // generic, this is the easiest way to get right to the place of the
  // object without knowing precisely what data we're poking at.
  $ref = &$object->{$type_info['key']};

  if (empty($step) || empty($object->temporary)) {
    // Create the basis for our new context.
    $conf = $object->{$type_info['key']}[$position];
    $object->temporary = &$conf;
  }
  else {
    $conf = &$object->temporary;
  }

  $name = $ref[$position]['name'];
  if (empty($name)) {
    ctools_ajax_render_error();
  }

  // load the plugin definition
  $plugin_definition = ctools_context_get_plugin($type, $name);
  if (empty($plugin_definition)) {
    ctools_ajax_render_error(t('Invalid context type'));
  }

  // Load the contexts
  $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array();
  $contexts = ctools_context_load_contexts($object, TRUE, $base_contexts);

  $form_state = array(
    'ajax' => TRUE, 
    'modal' => TRUE, 
    'modal return' => TRUE, 
    'object' => &$object, 
    'conf' => &$conf, 
    'position' => $position, 
    'plugin' => $plugin_definition, 
    'type' => $type, 
    'contexts' => $contexts, 
    'title' => t('Edit @type "@context"', array('@type' => $type_info['singular title'], '@context' => $plugin_definition['title'])), 
    'type info' => $type_info, 
    'op' => 'add', 
    'step' => $step,
  );

  $form_info = array(
    'path' => "ctools/context/ajax/configure/$mechanism/$type/$cache_key/$position/%step", 
    'show cancel' => TRUE, 
    'default form' => 'ctools_edit_context_form_defaults', 
    'auto caching' => TRUE, 
    'cache mechanism' => $mechanism, 
    'cache key' => $cache_key,
    // This is stating what the cache will be referred to in $form_state 
    'cache storage' => 'object',
  );

  if ($type == 'requiredcontext') {
    $form_info += array(
      'add form name' => 'required context add form', 
      'edit form name' => 'required context edit form',
    );
  }

  $output = ctools_plugin_configure_form($form_info, $form_state);

  if (!empty($form_state['cancel'])) {
    $output = array(ctools_modal_command_dismiss());
  }
  else if (!empty($form_state['complete'])) {
    // successful submit
    $ref[$position] = $conf;
    if (isset($object->temporary)) {
      unset($object->temporary);
    }

    ctools_cache_operation($mechanism, $cache_key, 'finalize', $object);

    $output = array();
    $output[] = ctools_modal_command_dismiss();

    $arg_form = array(
      '#post' => array(), 
      '#programmed' => FALSE, 
      '#tree' => FALSE,
    );

    // Build a chunk of the form to merge into the displayed form
    $arg_form[$type] = array(
      '#tree' => TRUE,
    );
    $arg_form[$type][$position] = array(
      '#tree' => TRUE,
    );

    ctools_context_add_item_to_form($mechanism, $type, $cache_key, $arg_form[$type][$position], $position, $ref[$position]);
    $arg_form = form_builder('ctools_context_form', $arg_form, $arg_form_state);

    $theme_vars = array();
    $theme_vars['type'] = $type;
    $theme_vars['form'] = $arg_form[$type][$position];
    $theme_vars['position'] = $position;
    $theme_vars['count'] = $position;
    $output[] = ajax_command_replace('#' . $type . '-row-' . $position, theme('ctools_context_item_row', $theme_vars));
    $output[] = ajax_command_changed('#' . $type . '-row-' . $position, '.title');
  }
  else {
    $output = ctools_modal_form_render($form_state, $output);
  }
  print ajax_render($output);
  exit;
}