You are here

function page_manager_page_add_subtask in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 page_manager/plugins/tasks/page.admin.inc \page_manager_page_add_subtask()

Page callback to add a subtask.

1 string reference to 'page_manager_page_add_subtask'
page_manager_page_menu in page_manager/plugins/tasks/page.admin.inc
Delegated implementation of hook_menu().

File

page_manager/plugins/tasks/page.admin.inc, line 197
Administrative functions for the page subtasks.

Code

function page_manager_page_add_subtask($task_name = NULL, $step = NULL) {
  ctools_include('context');
  $task = page_manager_get_task('page');
  $task_handler_plugins = page_manager_get_task_handler_plugins($task);
  if (empty($task_handler_plugins)) {
    drupal_set_message(t('There are currently no variants available and a page may not be added. Perhaps you need to install the Panels module to get a variant?'), 'error');
    return ' ';
  }
  $form_info = array(
    'id' => 'page_manager_add_page',
    'show trail' => TRUE,
    'show back' => TRUE,
    'show return' => FALSE,
    'next callback' => 'page_manager_page_add_subtask_next',
    'finish callback' => 'page_manager_page_add_subtask_finish',
    'return callback' => 'page_manager_page_add_subtask_finish',
    'cancel callback' => 'page_manager_page_add_subtask_cancel',
    'add order' => array(
      'basic' => t('Basic settings'),
      'argument' => t('Argument settings'),
      'access' => t('Access control'),
      'menu' => t('Menu settings'),
    ),
    'forms' => array(
      'basic' => array(
        'form id' => 'page_manager_page_form_basic',
      ),
      'access' => array(
        'form id' => 'page_manager_page_form_access',
      ),
      'menu' => array(
        'form id' => 'page_manager_page_form_menu',
      ),
      'argument' => array(
        'form id' => 'page_manager_page_form_argument',
      ),
    ),
  );
  if ($task_name) {
    $page = page_manager_get_page_cache($task_name);
    if (empty($page)) {
      return MENU_NOT_FOUND;
    }
    $form_info['path'] = "admin/structure/pages/add/{$task_name}/%step";
  }
  else {
    $new_page = page_manager_page_new();
    $new_page->name = NULL;
    $page = new stdClass();
    page_manager_page_new_page_cache($new_page, $page);
    $form_info['path'] = 'admin/structure/pages/add/%task_name/%step';
  }
  if ($step && $step != 'basic') {
    $handler_plugin = page_manager_get_task_handler($page->handler);
    $form_info['forms'] += $handler_plugin['forms'];
    if (isset($page->forms)) {
      foreach ($page->forms as $id) {
        if (isset($form_info['add order'][$id])) {
          $form_info['order'][$id] = $form_info['add order'][$id];
        }
        elseif (isset($handler_plugin['add features'][$id])) {
          $form_info['order'][$id] = $handler_plugin['add features'][$id];
        }
        elseif (isset($handler_plugin['required forms'][$id])) {
          $form_info['order'][$id] = $handler_plugin['required forms'][$id];
        }
      }
    }
    else {
      $form_info['order'] = $form_info['add order'];
    }

    // This means we just submitted our form from the default list
    // of steps, which we've traded in for a newly generated list of
    // steps above. We need to translate this 'next' step into what
    // our questions determined would be next.
    if ($step == 'next') {
      $keys = array_keys($form_info['order']);

      // Get rid of 'basic' from the list of forms.
      array_shift($keys);
      $step = array_shift($keys);

      // If $step == 'basic' at this point, we were not presented with any
      // additional forms at all. Let's just save and go!
      if ($step == 'basic') {
        page_manager_save_page_cache($page);

        // Redirect to the new page's task handler editor.
        drupal_goto(page_manager_edit_url($page->task_name));
      }
    }
  }
  else {
    $form_info['show trail'] = FALSE;
    $form_info['order'] = array(
      'basic' => t('Basic settings'),
      'next' => t('A meaningless second page'),
    );
  }
  ctools_include('wizard');
  $form_state = array(
    'task' => $task,
    'subtask' => $page->subtask,
    'page' => &$page,
    'type' => 'add',
    'task_id' => 'page',
    'task_name' => $page->task_name,
    'creating' => TRUE,
  );
  if (!empty($page->handlers)) {
    $keys = array_keys($page->handlers);
    $key = array_shift($keys);
    $form_state['handler'] =& $page->handlers[$key];
    $form_state['handler_id'] = $key;
  }
  $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
  if (!$output) {

    // redirect.
    drupal_redirect_form(array(), $form_state['redirect']);
  }
  return $output;
}