5 * Administrative functions for the page subtasks.
7 * These are attached to the menu system in page.inc via the hook_menu
8 * delegation. They are included here so that this code is loaded
13 * Delegated implementation of hook_menu().
15 function page_manager_page_menu(&$items, $task) {
16 // Set up access permissions.
17 $access_callback = isset($task['admin access callback']) ? $task['admin access callback'] : 'user_access';
18 $access_arguments = isset($task['admin access arguments']) ? $task['admin access arguments'] : array('administer page manager');
21 'access callback' => $access_callback,
22 'access arguments' => $access_arguments,
23 'file' => 'plugins/tasks/page.admin.inc',
26 $items['admin/structure/pages/add'] = array(
27 'title' => 'Add custom page',
28 'page callback' => 'page_manager_page_add_subtask',
29 'page arguments' => array(),
30 'type' => MENU_LOCAL_ACTION,
33 $items['admin/structure/pages/import'] = array(
34 'title' => 'Import page',
35 'page callback' => 'drupal_get_form',
36 'page arguments' => array('page_manager_page_import_subtask', 'page'),
37 'type' => MENU_LOCAL_ACTION,
39 if ($access_callback == 'user_access') {
40 $items['admin/structure/pages/import']['access callback'] = 'ctools_access_multiperm';
41 $items['admin/structure/pages/import']['access arguments'][] = 'use ctools import';
44 // AJAX callbacks for argument modal.
45 $items['admin/structure/pages/argument'] = array(
46 'page callback' => 'page_manager_page_subtask_argument_ajax',
47 'type' => MENU_CALLBACK,
50 // Add menu entries for each subtask
51 foreach (page_manager_page_load_all() as $subtask_id => $subtask) {
52 if (!empty($subtask->disabled)) {
56 if (!isset($subtask->access['type'])) {
57 $subtask->access['type'] = 'none';
59 if (!isset($subtask->access['settings'])) {
60 $subtask->access['settings'] = NULL;
64 $page_arguments = array((string) $subtask_id);
65 $access_arguments = array($subtask->access);
66 $load_arguments = array($subtask_id, '%index', '%map');
68 // Replace named placeholders with our own placeholder to load contexts.
71 foreach (explode('/', $subtask->path) as $bit) {
72 // Remove things like double slashes completely.
73 if (!isset($bit) || $bit === '') {
77 if ($bit[0] == '%' && $bit != '%') {
78 $placeholder = '%pm_arg';
81 $name = substr($bit, 1);
83 // Check to see if the argument plugin wants to use a different
84 // placholder. This will allow to_args.
85 if (!empty($subtask->arguments[$name])) {
86 ctools_include('context');
87 if (!empty($subtask->arguments[$name]['name'])) {
88 $plugin = ctools_get_argument($subtask->arguments[$name]['name']);
89 if (isset($plugin['path placeholder'])) {
90 if (function_exists($plugin['path placeholder'])) {
91 $placeholder = $plugin['path placeholder']($subtask->arguments[$name]);
94 $placeholder = $plugin['path placeholder'];
99 // If an argument, swap it out with our argument loader and make sure
100 // the argument gets passed through to the page callback.
101 $path[] = $placeholder;
102 $page_arguments[] = $position;
103 $access_arguments[] = $position;
105 else if ($bit[0] != '!') {
109 // Increment position. We do it like this to skip empty items that
110 // could happen from erroneous paths like: this///that
114 $menu_path = implode('/', $path);
116 $items[$menu_path] = page_manager_page_menu_item($task, $subtask->menu, $access_arguments, $page_arguments, $load_arguments);
118 // Add a parent menu item if one is configured.
119 if (isset($subtask->menu['type']) && $subtask->menu['type'] == 'default tab') {
121 $parent_path = implode('/', $path);
122 $items[$parent_path] = page_manager_page_menu_item($task, $subtask->menu['parent'], $access_arguments, $page_arguments, $load_arguments);
128 * Create a menu item for page manager pages.
131 * The configuration to use. It will contain a type, and depending on the
132 * type may also contain weight, title and name. These are presumed to have
133 * been configured from the UI.
134 * @param $access_arguments
135 * Arguments that go with ctools_access_menu; it should be loaded with
136 * the access plugin type, settings, and positions of any arguments that
137 * may produce contexts.
138 * @param $page_arguments
139 * This should be seeded with the subtask name for easy loading and like
140 * the access arguments above should contain positions of arguments so
141 * that the menu system passes contexts through.
142 * @param $load_arguments
143 * Arguments to send to the arg loader; should be the subtask id and '%index'.
145 function page_manager_page_menu_item($task, $menu, $access_arguments, $page_arguments, $load_arguments) {
147 'access callback' => 'ctools_access_menu',
148 'access arguments' => $access_arguments,
149 'page callback' => 'page_manager_page_execute',
150 'page arguments' => $page_arguments,
151 'load arguments' => $load_arguments,
152 'file' => 'plugins/tasks/page.inc',
155 if (isset($menu['title'])) {
156 $item['title'] = $menu['title'];
158 if (isset($menu['weight'])) {
159 $item['weight'] = $menu['weight'];
162 if (empty($menu['type'])) {
163 $menu['type'] = 'none';
166 switch ($menu['type']) {
169 $item['type'] = MENU_CALLBACK;
173 $item['type'] = MENU_NORMAL_ITEM;
174 // Insert item into the proper menu
175 $item['menu_name'] = $menu['name'];
179 $item['type'] = MENU_LOCAL_TASK;
183 $item['type'] = MENU_LOCAL_ACTION;
187 $item['type'] = MENU_DEFAULT_LOCAL_TASK;
195 * Page callback to add a subtask.
197 function page_manager_page_add_subtask($task_name = NULL, $step = NULL) {
198 ctools_include('context');
199 $task = page_manager_get_task('page');
200 $task_handler_plugins = page_manager_get_task_handler_plugins($task);
201 if (empty($task_handler_plugins)) {
202 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');
207 'id' => 'page_manager_add_page',
208 'show trail' => TRUE,
210 'show return' => FALSE,
211 'next callback' => 'page_manager_page_add_subtask_next',
212 'finish callback' => 'page_manager_page_add_subtask_finish',
213 'return callback' => 'page_manager_page_add_subtask_finish',
214 'cancel callback' => 'page_manager_page_add_subtask_cancel',
215 'add order' => array(
216 'basic' => t('Basic settings'),
217 'argument' => t('Argument settings'),
218 'access' => t('Access control'),
219 'menu' => t('Menu settings'),
223 'form id' => 'page_manager_page_form_basic',
226 'form id' => 'page_manager_page_form_access',
229 'form id' => 'page_manager_page_form_menu',
232 'form id' => 'page_manager_page_form_argument',
238 $page = page_manager_get_page_cache($task_name);
240 return MENU_NOT_FOUND;
243 $form_info['path'] = "admin/structure/pages/add/$task_name/%step";
246 $new_page = page_manager_page_new();
247 $new_page->name = NULL;
249 $page = new stdClass();
250 page_manager_page_new_page_cache($new_page, $page);
251 $form_info['path'] = 'admin/structure/pages/add/%task_name/%step';
254 if ($step && $step != 'basic') {
255 $handler_plugin = page_manager_get_task_handler($page->handler);
257 $form_info['forms'] += $handler_plugin['forms'];
259 if (isset($page->forms)) {
260 foreach ($page->forms as $id) {
261 if (isset($form_info['add order'][$id])) {
262 $form_info['order'][$id] = $form_info['add order'][$id];
264 else if (isset($handler_plugin['add features'][$id])) {
265 $form_info['order'][$id] = $handler_plugin['add features'][$id];
267 else if (isset($handler_plugin['required forms'][$id])) {
268 $form_info['order'][$id] = $handler_plugin['required forms'][$id];
273 $form_info['order'] = $form_info['add order'];
276 // This means we just submitted our form from the default list
277 // of steps, which we've traded in for a newly generated list of
278 // steps above. We need to translate this 'next' step into what
279 // our questions determined would be next.
280 if ($step == 'next') {
281 $keys = array_keys($form_info['order']);
282 // get rid of 'basic' from the list of forms.
284 $step = array_shift($keys);
286 // If $step == 'basic' at this point, we were not presented with any
287 // additional forms at all. Let's just save and go!
288 if ($step == 'basic') {
289 page_manager_save_page_cache($page);
290 // Redirect to the new page's task handler editor.
291 drupal_goto(page_manager_edit_url($page->task_name));
296 $form_info['show trail'] = FALSE;
297 $form_info['order'] = array(
298 'basic' => t('Basic settings'),
299 'next' => t('A meaningless second page'),
303 ctools_include('wizard');
306 'subtask' => $page->subtask,
310 'task_name' => $page->task_name,
314 if (!empty($page->handlers)) {
315 $keys = array_keys($page->handlers);
316 $key = array_shift($keys);
317 $form_state['handler'] = &$page->handlers[$key];
318 $form_state['handler_id'] = $key;
321 $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
325 drupal_redirect_form(array(), $form_state['redirect']);
332 * Callback generated when the add page process is finished.
334 function page_manager_page_add_subtask_finish(&$form_state) {
335 $page = &$form_state['page'];
336 // Update the cache with changes.
337 page_manager_set_page_cache($page);
339 $handler = $form_state['handler'];
340 $handler_plugin = page_manager_get_task_handler($handler->handler);
342 // Redirect to the new page's task handler editor.
343 if (isset($handler_plugin['add finish'])) {
344 $form_state['redirect'] = page_manager_edit_url($page->task_name, array('handlers', $handler->name, $handler_plugin['add finish']));
347 $form_state['redirect'] = page_manager_edit_url($page->task_name);
353 * Callback generated when the 'next' button is clicked.
355 * All we do here is store the cache.
357 function page_manager_page_add_subtask_next(&$form_state) {
358 if (empty($form_state['task_name']) || $form_state['task_name'] == 'page') {
359 // We may not have known the path to go next, because we didn't yet know the
360 // task name. This fixes that.
361 $form_state['form_info']['path'] = str_replace('%task_name', $form_state['page']->task_name, $form_state['form_info']['path']);
363 $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['clicked_button']['#next']);
366 // Update the cache with changes.
367 page_manager_set_page_cache($form_state['page']);
371 * Callback generated when the 'cancel' button is clicked.
373 * All we do here is clear the cache.
375 function page_manager_page_add_subtask_cancel(&$form_state) {
376 // Wipe all our stored changes.
377 if (isset($form_state['page']->task_name)) {
378 page_manager_clear_page_cache($form_state['page']->task_name);
383 * Basic settings form for a page manager page.
385 function page_manager_page_form_basic($form, &$form_state) {
386 $page = &$form_state['page']->subtask['subtask'];
387 $task = $form_state['task'];
389 $form['admin_title'] = array(
390 '#type' => 'textfield',
391 '#title' => t('Administrative title'),
392 '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'),
393 '#default_value' => $page->admin_title,
396 $form['name'] = array(
397 '#type' => 'machine_name',
398 '#title' => t('Machine name'),
399 '#machine_name' => array(
400 'exists' => 'page_manager_page_load',
401 'source' => array('admin_title'),
403 '#description' => t('The machine readable name of this page. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
404 '#default_value' => $page->name,
407 if (isset($page->pid) || empty($form_state['creating'])) {
408 $form['name']['#disabled'] = TRUE;
409 $form['name']['#value'] = $page->name;
412 $form['admin_description'] = array(
413 '#type' => 'textarea',
414 '#title' => t('Administrative description'),
415 '#description' => t('A description of what this page is, does or is for, for administrative use.'),
416 '#default_value' => $page->admin_description,
420 $form['path'] = array(
421 '#type' => 'textfield',
422 '#title' => t('Path'),
423 '#description' => t('The URL path to get to this page. You may create named placeholders for variable parts of the path by using %name for required elements and !name for optional elements. For example: "node/%node/foo", "forum/%forum" or "dashboard/!input". These named placeholders can be turned into contexts on the arguments form.'),
424 '#default_value' => $page->path,
425 '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
428 $frontpage = variable_get('site_frontpage', 'node');
432 foreach (explode('/', $page->path) as $bit) {
433 if ($bit[0] != '!') {
439 $path = implode('/', $path);
441 if (empty($path) || $path != $frontpage) {
442 $form['frontpage'] = array(
443 '#type' => 'checkbox',
444 '#default_value' => !empty($page->make_frontpage),
445 '#title' => t('Make this your site home page.'),
446 '#description' => t('To set this panel as your home page you must create a unique path name with no % placeholders in the path. The site home page is currently set to %homepage on the !siteinfo configuration form.', array('!siteinfo' => l(t('Site Information'), 'admin/config/system/site-information'), '%homepage' => '/' . $frontpage)),
448 $form['admin_paths'] = array(
449 '#type' => 'checkbox',
450 '#default_value' => !empty($page->conf['admin_paths']),
451 '#title' => t('Use this page in an admin overlay.'),
452 '#description' => t('Admin overlays are used in many places in Drupal 7 and administrative custom pages should probably utilize this feature.'),
455 else if ($path == $frontpage) {
456 $form['frontpage_markup'] = array(
457 '#value' => '<b>' . t('This page is currently set to be your site home page. This can be modified on the !siteinfo configuration form.', array('!siteinfo' => l(t('Site Information'), 'admin/settings/site-information'))) . '</b>',
460 $form['frontpage'] = array(
466 if (!isset($page->pid) && !empty($form_state['creating'])) {
467 $features['default'] = array(
468 'access' => t('Access control'),
469 'menu' => t('Visible menu item'),
472 module_load_include('inc', 'page_manager', 'page_manager.admin');
473 $form = page_manager_handler_add_form($form, $form_state, $features);
479 function page_manager_page_form_basic_validate_filter($value) {
480 return $value === -1;
484 * Validate the basic form.
486 function page_manager_page_form_basic_validate(&$form, &$form_state) {
487 // Ensure path is unused by other pages.
488 $page = $form_state['page']->subtask['subtask'];
489 $name = !empty($form_state['values']['name']) ? $form_state['values']['name'] : $page->name;
491 form_error($form['name'], t('Name is required.'));
494 // If this is new, make sure the name is unique:
495 if (empty($page->name)) {
496 $test = page_manager_page_load($name);
498 form_error($form['name'], t('That name is used by another page: @page', array('@page' => $test->admin_title)));
501 // Ensure name fits the rules:
502 if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
503 form_error($form['name'], t('Page name must be alphanumeric or underscores only.'));
507 $pages = page_manager_page_load_all();
508 foreach ($pages as $test) {
509 if ($test->name != $name && $test->path == $form_state['values']['path'] && empty($test->disabled)) {
510 form_error($form['path'], t('That path is used by another page: @page', array('@page' => $test->admin_title)));
514 // Ensure path is unused by things NOT pages. We do the double check because
515 // we're checking against our page callback.
517 if (empty($form_state['values']['path'])) {
518 form_error($form['path'], t('Path is required.'));
519 // stop processing here if there is no path.
525 foreach (explode('/', $form_state['values']['path']) as $position => $bit) {
526 if (!isset($bit) || $bit === '') {
530 if ($bit == '%' || $bit == '!') {
531 form_error($form['path'], t('You cannot have an unnamed placeholder (% or ! by itself). Please name your placeholder by adding a short piece of descriptive text to the % or !, such as %user or %node.'));
534 if ($bit[0] == '%') {
536 form_error($form['path'], t('You cannot have a dynamic path element after an optional path element.'));
539 if ($position == 0) {
540 form_error($form['path'], t('The first element in a path may not be dynamic.'));
545 else if ($bit[0] == '!') {
550 form_error($form['path'], t('You cannot have a static path element after an optional path element.'));
556 // Check to see if something that isn't a page manager page is using the path.
557 $path = implode('/', $path);
558 $result = db_query('SELECT * FROM {menu_router} WHERE path = :path', array(':path' => $path));
559 foreach ($result as $router) {
560 if ($router->page_callback != 'page_manager_page_execute') {
561 form_error($form['path'], t('That path is already in use. This system cannot override existing paths.'));
565 // Ensure the path is not already an alias to something else.
566 if (strpos($path, '%') === FALSE) {
567 $alias = db_query('SELECT alias, source FROM {url_alias} WHERE alias = :path', array(':path' => $path))->fetchObject();
569 form_error($form['path'], t('That path is currently assigned to be an alias for @alias. This system cannot override existing aliases.', array('@alias' => $alias->source)));
573 if (!empty($form_state['values']['frontpage'])) {
574 form_error($form['path'], t('You cannot make this page your site home page if it uses % placeholders.'));
578 // Ensure path is properly formed.
579 $args = page_manager_page_get_named_arguments($form_state['values']['path']);
580 if ($invalid_args = array_filter($args, 'page_manager_page_form_basic_validate_filter')) {
581 foreach ($invalid_args as $arg => $position) {
582 form_error($form['path'], t('Duplicated argument %arg', array('%arg' => $arg)));
586 if (isset($args['%'])) {
587 form_error($form['path'], t('Invalid arg <em>%</em>. All arguments must be named with keywords.'));
590 $form_state['arguments'] = $args;
594 * Store the values from the basic settings form.
596 function page_manager_page_form_basic_submit(&$form, &$form_state) {
597 $page = &$form_state['page']->subtask['subtask'];
598 $cache = &$form_state['page'];
600 // If this is a new thing, then we have to do a bunch of setup to create
601 // the cache record with the right ID and some basic data that we could
602 // not know until we asked the user some questions.
603 if (!isset($page->pid) && !empty($form_state['creating'])) {
604 // Update the data with our new name.
605 $page->name = $form_state['values']['name'];
606 $form_state['page']->task_name = page_manager_make_task_name($form_state['task_id'], $page->name);
607 $cache->handler = $form_state['values']['handler'];
608 $cache->subtask_id = $page->name;
609 $plugin = page_manager_get_task_handler($cache->handler);
611 // If they created and went back, there might be old, dead handlers
612 // that are not going to be added.
615 $cache->handlers = array();
616 $cache->handler_info = array();
618 // Create a new handler.
619 $handler = page_manager_new_task_handler($plugin);
620 $title = !empty($form_state['values']['title']) ? $form_state['values']['title'] : $plugin['title'];
621 page_manager_handler_add_to_page($cache, $handler, $title);
623 // Figure out which forms to present them with
624 $cache->forms = array();
625 $cache->forms[] = 'basic'; // This one is always there.
626 if (!empty($form_state['arguments'])) {
627 $cache->forms[] = 'argument';
630 $features = $form_state['values']['features'];
631 $cache->forms = array_merge($cache->forms, array_keys(array_filter($features['default'])));
632 if (isset($features[$form_state['values']['handler']])) {
633 $cache->forms = array_merge($cache->forms, array_keys(array_filter($features[$form_state['values']['handler']])));
636 if (isset($plugin['required forms'])) {
637 $cache->forms = array_merge($cache->forms, array_keys($plugin['required forms']));
641 $page->admin_title = $form_state['values']['admin_title'];
642 $cache->subtask['admin title'] = check_plain($form_state['values']['admin_title']);
644 $page->admin_description = $form_state['values']['admin_description'];
645 $cache->subtask['admin description'] = filter_xss_admin($form_state['values']['admin_description']);
647 if ($page->path != $form_state['values']['path']) {
648 $page->path = $form_state['values']['path'];
649 page_manager_page_recalculate_arguments($page);
650 $cache->path_changed = TRUE;
653 $page->make_frontpage = !empty($form_state['values']['frontpage']);
654 $page->conf['admin_paths'] = !empty($form_state['values']['admin_paths']);
658 * Form to handle menu item controls.
660 function page_manager_page_form_menu($form, &$form_state) {
661 ctools_include('dependent');
662 $form['menu'] = array(
663 '#prefix' => '<div class="clearfix">',
664 '#suffix' => '</div>',
668 $menu = $form_state['page']->subtask['subtask']->menu;
674 'name' => 'navigation',
679 'name' => 'navigation',
684 $form['menu']['type'] = array(
685 '#title' => t('Type'),
688 'none' => t('No menu entry'),
689 'normal' => t('Normal menu entry'),
690 'tab' => t('Menu tab'),
691 'default tab' => t('Default menu tab'),
692 'action' => t('Local action'),
694 '#default_value' => $menu['type'],
697 $form['menu']['title'] = array(
698 '#title' => t('Title'),
699 '#type' => 'textfield',
700 '#default_value' => $menu['title'],
701 '#description' => t('If set to normal or tab, enter the text to use for the menu item.'),
702 '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab', 'action')),
705 list($major, $minor) = explode('.', VERSION, 2);
707 // Only display the menu selector if menu module is enabled.
708 if (module_exists('menu')) {
709 $form['menu']['name'] = array(
710 '#title' => t('Menu'),
712 '#options' => menu_get_menus(),
713 '#default_value' => $menu['name'],
714 '#description' => t('Insert item into an available menu.'),
715 '#dependency' => array('radio:menu[type]' => array('normal')),
719 $form['menu']['name'] = array(
721 '#value' => $menu['name'],
723 $form['menu']['markup'] = array(
724 '#value' => t('Menu selection requires the activation of menu module.'),
727 $form['menu']['weight'] = array(
728 '#title' => t('Weight'),
729 '#type' => 'textfield',
730 '#default_value' => isset($menu['weight']) ? $menu['weight'] : 0,
731 '#description' => t('The lower the weight the higher/further left it will appear.'),
732 '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab', 'action')),
735 $form['menu']['parent']['type'] = array(
736 '#prefix' => '<div id="edit-menu-parent-type-wrapper">',
737 '#suffix' => '</div>',
738 '#title' => t('Parent menu item'),
740 '#options' => array('none' => t('No menu entry'), 'normal' => t('Normal menu item'), 'tab' => t('Menu tab')),
741 '#default_value' => $menu['parent']['type'],
742 '#description' => t('When providing a menu item as a default tab, Drupal needs to know what the parent menu item of that tab will be. Sometimes the parent will already exist, but other times you will need to have one created. The path of a parent item will always be the same path with the last part left off. i.e, if the path to this view is <em>foo/bar/baz</em>, the parent path would be <em>foo/bar</em>.'),
743 '#dependency' => array('radio:menu[type]' => array('default tab')),
745 $form['menu']['parent']['title'] = array(
746 '#title' => t('Parent item title'),
747 '#type' => 'textfield',
748 '#default_value' => $menu['parent']['title'],
749 '#description' => t('If creating a parent menu item, enter the title of the item.'),
750 '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('normal', 'tab')),
751 '#dependency_count' => 2,
753 // Only display the menu selector if menu module is enabled.
754 if (module_exists('menu')) {
755 $form['menu']['parent']['name'] = array(
756 '#title' => t('Parent item menu'),
758 '#options' => menu_get_menus(),
759 '#default_value' => $menu['parent']['name'],
760 '#description' => t('Insert item into an available menu.'),
761 '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('normal')),
762 '#dependency_count' => 2,
766 $form['menu']['parent']['name'] = array(
768 '#value' => $menu['parent']['name'],
771 $form['menu']['parent']['weight'] = array(
772 '#title' => t('Parent weight'),
773 '#type' => 'textfield',
774 '#default_value' => $menu['parent']['weight'],
776 '#description' => t('Enter the weight of the parent item. The lower the number, the more to the left it will be.'),
777 '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('tab', 'normal')),
778 '#dependency_count' => 2,
785 * Validate handler for the menu form for add/edit page task.
787 function page_manager_page_form_menu_validate(&$form, &$form_state) {
788 // If setting a 'normal' menu entry, make sure that any placeholders
789 // support the to_arg stuff.
791 if ($form_state['values']['menu']['type'] == 'normal') {
792 $page = $form_state['page']->subtask['subtask'];
794 foreach (explode('/', $page->path) as $bit) {
795 if (!isset($bit) || $bit === '') {
799 if ($bit[0] == '%') {
801 $name = substr($bit, 1);
803 // Check to see if the argument plugin allows to arg:
804 if (!empty($page->arguments[$name])) {
805 ctools_include('context');
806 $plugin = ctools_get_argument($page->arguments[$name]['name']);
807 if (!empty($plugin['path placeholder to_arg'])) {
812 form_error($form['menu']['type'], t('Paths with non optional placeholders cannot be used as normal menu items unless the selected argument handler provides a default argument to use for the menu item.'));
820 * Submit handler for the menu form for add/edit page task.
822 function page_manager_page_form_menu_submit(&$form, &$form_state) {
823 $form_state['page']->subtask['subtask']->menu = $form_state['values']['menu'];
824 $form_state['page']->path_changed = TRUE;
828 * Form to handle menu item controls.
830 function page_manager_page_form_access($form, &$form_state) {
831 ctools_include('context');
832 $form_state['module'] = 'page_manager_page';
833 $form_state['callback argument'] = $form_state['page']->task_name;
834 $form_state['access'] = $form_state['page']->subtask['subtask']->access;
835 $form_state['no buttons'] = TRUE;
836 $form_state['contexts'] = array();
838 // Load contexts based on argument data:
839 if ($arguments = _page_manager_page_get_arguments($form_state['page']->subtask['subtask'])) {
840 $form_state['contexts'] = ctools_context_get_placeholders_from_argument($arguments);
843 ctools_include('context-access-admin');
844 $form = ctools_access_admin_form($form, $form_state);
850 * Submit handler to deal with access control changes.
852 function page_manager_page_form_access_submit(&$form, &$form_state) {
853 $form_state['page']->subtask['subtask']->access['logic'] = $form_state['values']['logic'];
854 $form_state['page']->path_changed = TRUE;
858 * Form to handle assigning argument handlers to named arguments.
860 function page_manager_page_form_argument($form, &$form_state) {
861 $page = &$form_state['page']->subtask['subtask'];
864 $arguments = page_manager_page_get_named_arguments($path);
866 $form['table'] = array(
867 '#theme' => 'page_manager_page_form_argument_table',
868 '#page-manager-path' => $path,
869 'argument' => array(),
872 $task_name = $form_state['page']->task_name;
873 foreach ($arguments as $keyword => $position) {
876 if (isset($page->temporary_arguments[$keyword]) && !empty($form_state['allow temp'])) {
877 $conf = $page->temporary_arguments[$keyword];
879 else if (isset($page->arguments[$keyword])) {
880 $conf = $page->arguments[$keyword];
883 $context = t('No context assigned');
886 if ($conf && isset($conf['name'])) {
887 ctools_include('context');
888 $plugin = ctools_get_argument($conf['name']);
890 if (isset($plugin['title'])) {
891 $context = $plugin['title'];
895 $form['table']['argument'][$keyword]['#keyword'] = $keyword;
896 $form['table']['argument'][$keyword]['#position'] = $position;
897 $form['table']['argument'][$keyword]['#context'] = $context;
899 // The URL for this ajax button
900 $form['table']['argument'][$keyword]['change-url'] = array(
901 '#attributes' => array('class' => array("page-manager-context-$keyword-change-url")),
903 '#value' => url("admin/structure/pages/argument/change/$task_name/$keyword", array('absolute' => TRUE)),
905 $form['table']['argument'][$keyword]['change'] = array(
907 '#value' => t('Change'),
908 '#attributes' => array('class' => array('ctools-use-modal')),
909 '#id' => "page-manager-context-$keyword-change",
912 $form['table']['argument'][$keyword]['settings'] = array();
914 // Only show the button if this has a settings form available:
915 if (!empty($plugin)) {
916 // The URL for this ajax button
917 $form['table']['argument'][$keyword]['settings-url'] = array(
918 '#attributes' => array('class' => array("page-manager-context-$keyword-settings-url")),
920 '#value' => url("admin/structure/pages/argument/settings/$task_name/$keyword", array('absolute' => TRUE)),
922 $form['table']['argument'][$keyword]['settings'] = array(
924 '#value' => t('Settings'),
925 '#attributes' => array('class' => array('ctools-use-modal')),
926 '#id' => "page-manager-context-$keyword-settings",
935 * Theme the table for this form.
937 function theme_page_manager_page_form_argument_table($vars) {
938 $form = $vars['form'];
940 array('data' => t('Argument'), 'class' => array('page-manager-argument')),
941 array('data' => t('Position in path'), 'class' => array('page-manager-position')),
942 array('data' => t('Context assigned'), 'class' => array('page-manager-context')),
943 array('data' => t('Operations'), 'class' => array('page-manager-operations')),
948 ctools_include('modal');
949 ctools_modal_add_js();
950 foreach (element_children($form['argument']) as $key) {
952 $row[] = '%' . check_plain($form['argument'][$key]['#keyword']);
953 $row[] = check_plain($form['argument'][$key]['#position']);
954 $row[] = $form['argument'][$key]['#context'] . ' ' . drupal_render($form['argument'][$key]['change']);;
955 $row[] = drupal_render($form['argument'][$key]['settings']) . drupal_render($form['argument'][$key]);
957 $rows[] = array('data' => $row);
961 $rows[] = array(array('data' => t('The path %path has no arguments to configure.', array('%path' => $form['#page-manager-path'])), 'colspan' => 4));
965 'id' => 'page-manager-argument-table',
968 $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
973 * Ajax entry point to edit an item
975 function page_manager_page_subtask_argument_ajax($step = NULL, $task_name = NULL, $keyword = NULL) {
976 ctools_include('ajax');
977 ctools_include('modal');
978 ctools_include('context');
979 ctools_include('wizard');
982 return ctools_ajax_render_error();
985 if (!$cache = page_manager_get_page_cache($task_name)) {
986 return ctools_ajax_render_error(t('Invalid object name.'));
989 $page = &$cache->subtask['subtask'];
991 $arguments = page_manager_page_get_named_arguments($path);
993 // Load stored object from cache.
994 if (!isset($arguments[$keyword])) {
995 return ctools_ajax_render_error(t('Invalid keyword.'));
998 // Set up wizard info
1000 'id' => 'page_manager_page_argument',
1001 'path' => "admin/structure/pages/argument/%step/$task_name/$keyword",
1002 'show cancel' => TRUE,
1003 'next callback' => 'page_manager_page_argument_next',
1004 'finish callback' => 'page_manager_page_argument_finish',
1005 'cancel callback' => 'page_manager_page_argument_cancel',
1007 'change' => t('Change context type'),
1008 'settings' => t('Argument settings'),
1012 'title' => t('Change argument'),
1013 'form id' => 'page_manager_page_argument_form_change',
1015 'settings' => array(
1016 'title' => t('Argument settings'),
1017 'form id' => 'page_manager_page_argument_form_settings',
1022 $form_state = array(
1024 'keyword' => $keyword,
1027 'modal return' => TRUE,
1028 'commands' => array(),
1031 $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
1032 if (!empty($form_state['cancel'])) {
1033 $commands = array(ctools_modal_command_dismiss());
1035 else if (!empty($form_state['complete'])) {
1036 if (isset($page->temporary_arguments[$keyword])) {
1037 $page->arguments[$keyword] = $page->temporary_arguments[$keyword];
1040 if (isset($page->temporary_arguments)) {
1041 unset($page->temporary_arguments);
1044 // Update the cache with changes.
1045 page_manager_set_page_cache($cache);
1047 // Rerender the table so we can ajax it back in.
1048 // Go directly to the form and retrieve it using a blank form and
1049 // a clone of our current form state. This is an abbreviated
1050 // drupal_get_form that is halted prior to render and is never
1051 // fully processed, but is guaranteed to produce the same form we
1052 // started with so we don't have to do crazy stuff to rerender
1055 // @todo should there be a tool to do this?
1057 $clone_state = $form_state;
1058 $clone_state['allow temp'] = TRUE;
1059 $form = drupal_build_form('page_manager_page_form_argument', $form_state);
1061 // Render just the table portion.
1062 $output = drupal_render($form['table']);
1064 ajax_command_replace('#page-manager-argument-table', $output),
1065 ctools_modal_command_dismiss(),
1069 $commands = ctools_modal_form_render($form_state, $output);
1071 print ajax_render($commands);
1077 * Callback generated when the add page process is finished.
1079 function page_manager_page_argument_finish(&$form_state) {
1083 * Callback generated when the 'next' button is clicked.
1085 * All we do here is store the cache.
1087 function page_manager_page_argument_next(&$form_state) {
1088 // Update the cache with changes.
1089 page_manager_set_page_cache($form_state['page']);
1093 * Callback generated when the 'cancel' button is clicked.
1095 * We might have some temporary data lying around. We must remove it.
1097 function page_manager_page_argument_cancel(&$form_state) {
1098 $page = &$form_state['page']->subtask['subtask'];
1099 if (isset($page->temporary_arguments)) {
1100 unset($page->temporary_arguments);
1101 // Update the cache with changes.
1102 page_manager_set_page_cache($page);
1107 * Basic settings form for a page manager page.
1109 function page_manager_page_argument_form_change($form, &$form_state) {
1110 $page = &$form_state['page']->subtask['subtask'];
1111 $keyword = &$form_state['keyword'];
1113 ctools_include('context');
1114 $plugins = ctools_get_arguments();
1117 foreach ($plugins as $id => $plugin) {
1118 if (empty($plugin['no ui'])) {
1119 $options[$id] = $plugin['title'];
1125 $options = array('' => t('No context selected')) + $options;
1128 if (isset($page->arguments[$keyword]) && isset($page->arguments[$keyword]['name'])) {
1129 $argument = $page->arguments[$keyword]['name'];
1132 $form['argument'] = array(
1133 '#type' => 'radios',
1134 '#options' => $options,
1135 '#default_value' => $argument,
1142 * Submit handler to change an argument.
1144 function page_manager_page_argument_form_change_submit(&$form, &$form_state) {
1145 $page = &$form_state['page']->subtask['subtask'];
1146 $keyword = &$form_state['keyword'];
1147 $argument = $form_state['values']['argument'];
1149 // If the argument is not changing, we do not need to do anything.
1150 if (isset($page->arguments[$keyword]['name']) && $page->arguments[$keyword]['name'] == $argument) {
1151 // Set the task to cancel since no change means do nothing:
1152 $form_state['clicked_button']['#wizard type'] = 'cancel';
1156 ctools_include('context');
1158 // If switching to the no context, just wipe out the old data.
1159 if (empty($argument)) {
1160 $form_state['clicked_button']['#wizard type'] = 'finish';
1161 $page->temporary_arguments[$keyword] = array(
1162 'settings' => array(),
1163 'identifier' => t('No context'),
1168 $plugin = ctools_get_argument($argument);
1170 // Acquire defaults.
1171 $settings = array();
1173 if (isset($plugin['default'])) {
1174 if (is_array($plugin['default'])) {
1175 $settings = $plugin['default'];
1177 else if (function_exists($plugin['default'])) {
1178 $settings = $plugin['default']();
1182 $id = ctools_context_next_id($page->arguments, $argument);
1183 $title = isset($plugin['title']) ? $plugin['title'] : t('No context');
1185 // Set the new argument in a temporary location.
1186 $page->temporary_arguments[$keyword] = array(
1188 'identifier' => $title . ($id > 1 ? ' ' . $id : ''),
1189 'name' => $argument,
1190 'settings' => $settings,
1195 * Basic settings form for a page manager page.
1197 function page_manager_page_argument_form_settings($form, &$form_state) {
1198 $page = &$form_state['page']->subtask['subtask'];
1199 $keyword = &$form_state['keyword'];
1201 if (isset($page->temporary_arguments[$keyword])) {
1202 $conf = $page->temporary_arguments[$keyword];
1204 else if (isset($page->arguments[$keyword])) {
1205 $conf = $page->temporary_arguments[$keyword] = $page->arguments[$keyword];
1208 if (!isset($conf)) {
1209 // This should be impossible and thus never seen.
1210 $form['error'] = array('#value' => t('Error: missing argument.'));
1214 ctools_include('context');
1215 $plugin = ctools_get_argument($conf['name']);
1217 $form['settings'] = array(
1221 $form['identifier'] = array(
1222 '#type' => 'textfield',
1223 '#title' => t('Context identifier'),
1224 '#description' => t('This is the title of the context used to identify it later in the administrative process. This will never be shown to a user.'),
1225 '#default_value' => $conf['identifier'],
1229 // This should be impossible and thus never seen.
1230 $form['error'] = array('#value' => t('Error: missing or invalid argument plugin %argument.', array('%argument', $argument)));
1234 if ($function = ctools_plugin_get_function($plugin, 'settings form')) {
1235 $function($form, $form_state, $conf['settings']);
1238 $form_state['plugin'] = $plugin;
1243 * Validate handler for argument settings.
1245 function page_manager_page_argument_form_settings_validate(&$form, &$form_state) {
1246 if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form validate')) {
1247 $function($form, $form_state);
1252 * Submit handler for argument settings.
1254 function page_manager_page_argument_form_settings_submit(&$form, &$form_state) {
1255 if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form submit')) {
1256 $function($form, $form_state);
1259 $page = &$form_state['page']->subtask['subtask'];
1260 $keyword = &$form_state['keyword'];
1261 // Copy the form to our temporary location which will get moved again when
1262 // finished. Yes, finished is always next but finish can happen from other
1263 // locations so we funnel through that path rather than duplicate.
1264 $page->temporary_arguments[$keyword]['identifier'] = $form_state['values']['identifier'];
1265 if (isset($form_state['values']['settings'])) {
1266 $page->temporary_arguments[$keyword]['settings'] = $form_state['values']['settings'];
1269 $page->temporary_arguments[$keyword]['settings'] = array();
1274 * Import a task handler from cut & paste
1276 function page_manager_page_import_subtask($form, &$form_state, $task_name) {
1277 $form_state['task'] = page_manager_get_task($task_name);
1279 drupal_set_title(t('Import page'));
1280 $form['name'] = array(
1281 '#type' => 'textfield',
1282 '#title' => t('Page name'),
1283 '#description' => t('Enter the name to use for this page if it is different from the source page. Leave blank to use the original name of the page.'),
1286 $form['path'] = array(
1287 '#type' => 'textfield',
1288 '#title' => t('Path'),
1289 '#description' => t('Enter the path to use for this page if it is different from the source page. Leave blank to use the original path of the page.'),
1292 $form['overwrite'] = array(
1293 '#type' => 'checkbox',
1294 '#title' => t('Allow overwrite of an existing page'),
1295 '#description' => t('If the name you selected already exists in the database, this page will be allowed to overwrite the existing page.'),
1298 $form['object'] = array(
1299 '#type' => 'textarea',
1300 '#title' => t('Paste page code here'),
1304 $form['submit'] = array(
1305 '#type' => 'submit',
1306 '#value' => t('Import'),
1312 * Ensure we got a valid page.
1314 function page_manager_page_import_subtask_validate(&$form, &$form_state) {
1316 eval($form_state['values']['object']);
1319 if (!isset($page) || !is_object($page)) {
1320 $errors = ob_get_contents();
1321 if (empty($errors)) {
1322 $errors = t('No handler found.');
1324 form_error($form['object'], t('Unable to get a page from the import. Errors reported: @errors', array('@errors' => $errors)));
1327 if (empty($form_state['values']['name'])) {
1328 $form_state['values']['name'] = $page->name;
1331 $task_name = page_manager_make_task_name('page', $form_state['values']['name']);
1332 $form_state['cache'] = page_manager_get_page_cache($task_name);
1334 if ($form_state['cache'] && $form_state['cache']->locked) {
1335 form_error($form['name'], t('That page name is in use and locked by another user. You must <a href="!break">break the lock</a> on that page before proceeding, or choose a different name.', array('!break' => url(page_manager_edit_url($task_name, array('actions', 'break-lock'))))));
1339 if (empty($form_state['values']['path'])) {
1340 $form_state['values']['path'] = $page->path;
1343 if (empty($form_state['values']['overwrite'])) {
1347 $form_state['page'] = new stdClass();
1348 $form_state['page']->subtask['subtask'] = $page;
1349 page_manager_page_form_basic_validate($form, $form_state);
1353 * Submit the import page to create the new page and redirect.
1355 function page_manager_page_import_subtask_submit($form, &$form_state) {
1356 $page = &$form_state['page']->subtask['subtask'];
1357 $page->name = $form_state['values']['name'];
1358 $page->path = $form_state['values']['path'];
1360 $task_name = page_manager_make_task_name('page', $page->name);
1361 $cache = page_manager_get_page_cache($task_name);
1363 $cache = new stdClass();
1366 page_manager_page_new_page_cache($page, $cache);
1367 page_manager_set_page_cache($cache);
1369 $form_state['redirect'] = page_manager_edit_url($task_name);
1373 * Entry point to export a page.
1375 function page_manager_page_form_export($form, &$form_state) {
1376 $page = $form_state['page']->subtask['subtask'];
1378 $export = page_manager_page_export($page, $form_state['page']->handlers);
1380 $lines = substr_count($export, "\n");
1381 $form['code'] = array(
1382 '#type' => 'textarea',
1383 '#default_value' => $export,
1387 unset($form['buttons']);
1392 * Entry point to clone a page.
1394 function page_manager_page_form_clone($form, &$form_state) {
1395 $page = &$form_state['page']->subtask['subtask'];
1397 // This provides its own button because it does something totally different.
1398 unset($form['buttons']);
1400 $form['admin_title'] = array(
1401 '#type' => 'textfield',
1402 '#title' => t('Administrative title'),
1403 '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'),
1404 '#default_value' => $page->admin_title,
1407 $form['name'] = array(
1408 '#type' => 'machine_name',
1409 '#title' => t('Page name'),
1410 '#machine_name' => array(
1411 'exists' => 'page_manager_page_load',
1412 'source' => array('admin_title'),
1414 '#description' => t('Enter the name to the new page It must be unique and contain only alphanumeric characters and underscores.'),
1418 $form['path'] = array(
1419 '#type' => 'textfield',
1420 '#title' => t('Path'),
1421 '#description' => t('The URL path to get to this page. You may create named placeholders for variable parts of the path by using %name for required elements and !name for optional elements. For example: "node/%node/foo", "forum/%forum" or "dashboard/!input". These named placeholders can be turned into contexts on the arguments form. You cannot use the same path as the original page.'),
1422 '#default_value' => $page->path,
1425 $form['handlers'] = array(
1426 '#type' => 'checkbox',
1427 '#title' => t('Clone variants'),
1428 '#description' => t('If checked all variants associated with the page will be cloned as well. If not checked the page will be cloned without variants.'),
1429 '#default_value' => TRUE,
1432 $form['submit'] = array(
1433 '#type' => 'submit',
1434 '#value' => t('Clone'),
1441 * Validate clone page form.
1443 function page_manager_page_form_clone_validate(&$form, &$form_state) {
1444 $page = &$form_state['page']->subtask['subtask'];
1446 $page->old_name = $page->name;
1448 page_manager_page_form_basic_validate($form, $form_state);
1452 * submit clone page form.
1454 * Load the page, change the name(s) to protect the innocent, and if
1455 * requested, load all the task handlers so that they get saved properly too.
1457 function page_manager_page_form_clone_submit(&$form, &$form_state) {
1458 $original = $form_state['page']->subtask['subtask'];
1460 $original->name = $form_state['values']['name'];
1461 $original->admin_title = $form_state['values']['admin_title'];
1462 $original->path = $form_state['values']['path'];
1464 $handlers = !empty($form_state['values']['handlers']) ? $form_state['page']->handlers : FALSE;
1465 // Export the handler, which is a fantastic way to clean database IDs out of it.
1466 $export = page_manager_page_export($original, $handlers);
1471 $task_name = page_manager_make_task_name('page', $page->name);
1472 $cache = new stdClass();
1474 page_manager_page_new_page_cache($page, $cache);
1475 page_manager_set_page_cache($cache);
1477 $form_state['redirect'] = page_manager_edit_url($task_name);
1481 * Entry point to export a page.
1483 function page_manager_page_form_delete($form, &$form_state) {
1484 $page = &$form_state['page']->subtask['subtask'];
1486 if ($page->type == t('Overridden')) {
1487 $text = t('Reverting the page will delete the page that is in the database, reverting it to the original default page. Any changes you have made will be lost and cannot be recovered.');
1490 $text = t('Are you sure you want to delete this page? Deleting a page cannot be undone.');
1492 $form['markup'] = array(
1493 '#value' => '<p>' . $text . '</p>',
1496 if (empty($form_state['page']->locked)) {
1497 unset($form['buttons']);
1498 $form['delete'] = array(
1499 '#type' => 'submit',
1500 '#value' => $page->type == t('Overridden') ? t('Revert') : t('Delete'),
1508 * Submit handler to delete a view.
1510 function page_manager_page_form_delete_submit(&$form, &$form_state) {
1511 $page = $form_state['page']->subtask['subtask'];
1512 page_manager_page_delete($page);
1513 if ($page->type != t('Overridden')) {
1514 $form_state['redirect'] = 'admin/structure/pages';
1515 drupal_set_message(t('The page has been deleted.'));
1518 $form_state['redirect'] = page_manager_edit_url($form_state['page']->task_name, array('summary'));
1519 drupal_set_message(t('The page has been reverted.'));