| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file 5 * Provides vertical tabs capability for fieldsets in forms. 6 */ 7 8 /** 9 * Implements hook_menu(). 10 */ 11 function vertical_tabs_menu() { 12 $items['admin/settings/vertical-tabs'] = array( 13 'title' => t('Vertical Tabs'), 14 'page callback' => 'drupal_get_form', 15 'page arguments' => array('vertical_tabs_settings_form'), 16 'access arguments' => array('administer site configuration'), 17 'file' => 'vertical_tabs.admin.inc', 18 ); 19 20 return $items; 21 } 22 23 /** 24 * Implements hook_theme(). 25 */ 26 function vertical_tabs_theme() { 27 return array( 28 'vertical_tabs' => array( 29 'arguments' => array('element' => NULL), 30 ), 31 ); 32 } 33 34 /** 35 * Implements hook_form_alter(). 36 */ 37 function vertical_tabs_form_alter(&$form, $form_state, $form_id) { 38 // Add the support and default groupings for core forms. 39 vertical_tabs_add_core_support($form, $form_id); 40 41 $config = vertical_tabs_get_config($form_id); 42 43 // Skip programmed or excluded forms. 44 if (!empty($form['#programmed']) || $config === FALSE) { 45 vertical_tabs_remove_vertical_tabs($form); 46 } 47 elseif ($config) { 48 // Merge in the vertical tabs settings in case they already existed. 49 $form += array('#vertical_tabs' => array()); 50 if (is_array($config)) { 51 $form['#vertical_tabs'] += $config; 52 } 53 $form['#pre_render'][] = 'vertical_tabs_form_pre_render'; 54 } 55 } 56 57 /** 58 * Implements hook_form_FORM_ID_alter(). 59 */ 60 function vertical_tabs_form_node_type_form_alter(&$form, $form_state) { 61 if (empty($form['#programmed']) && $form['#node_type']->type && variable_get('vertical_tabs_node_type_settings', 0)) { 62 module_load_include('inc', 'vertical_tabs', 'vertical_tabs.admin'); 63 vertical_tabs_add_node_type_options($form, $form['#node_type']->type); 64 } 65 } 66 67 /** 68 * Add core module fieldset support. 69 * 70 * @param $form 71 * The form array, passed by reference. 72 * @param $fomr_id 73 * The ID of the form. 74 */ 75 function vertical_tabs_add_core_support(&$form, $form_id) { 76 $fieldsets = array(); 77 $group = ''; 78 79 if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) { 80 $fieldsets['revision_information'] = 'node'; 81 $fieldsets['author'] = 'node'; 82 $fieldsets['options'] = 'node'; 83 $fieldsets['book'] = 'book'; 84 $fieldsets['menu'] = 'menu'; 85 $fieldsets['comment_settings'] = 'comment'; 86 $fieldsets['attachments'] = 'upload'; 87 $fieldsets['path'] = 'path'; 88 $fieldsets['taxonomy'] = 'taxonomy'; 89 $group = 'additional_settings'; 90 } 91 elseif ($form_id == 'node_type_form') { 92 $fieldsets['submission'] = 'content_types'; 93 $fieldsets['workflow'] = 'content_types'; 94 $fieldsets['comment'] = 'comment'; 95 $group = 'additional_settings'; 96 } 97 elseif ($form_id == 'block_admin_configure' || strpos($form_id, 'block_add_block_form') !== FALSE) { 98 $fieldsets['user_vis_settings'] = 'block'; 99 $fieldsets['role_vis_settings'] = 'block'; 100 $fieldsets['page_vis_settings'] = 'block'; 101 $group = 'visibility_settings'; 102 } 103 104 foreach ($fieldsets as $key => $module) { 105 if (isset($form[$key])) { 106 $form[$key] += array('#group' => $group, '#attached' => array()); 107 $file = drupal_get_path('module', 'vertical_tabs') . '/core/' . $module . '.js'; 108 if (is_file($file)) { 109 $form[$key]['#attached'] += array('js' => array()); 110 $form[$key]['#attached']['js'] += array('vertical-tabs' => $file); 111 } 112 } 113 } 114 } 115 116 /** 117 * Implements hook_form_FORM_ID_alter(). 118 * 119 * Add our color-handling submit handler to the color settings form. 120 */ 121 function vertical_tabs_form_system_theme_settings_alter(&$form, $form_state) { 122 // Add our color-handling submit handler to the color settings form. 123 if (isset($form['color']) && function_exists('gd_info')) { 124 $form['#submit'][] = 'vertical_tabs_system_theme_settings_submit'; 125 } 126 } 127 128 /** 129 * Submit handler for the theme settings page. 130 */ 131 function vertical_tabs_system_theme_settings_submit($form, &$form_state) { 132 $theme = $form_state['values']['theme']; 133 $info = $form_state['values']['info']; 134 135 // Resolve palette 136 $palette = $form_state['values']['palette']; 137 if ($form_state['values']['scheme'] != '') { 138 $scheme = explode(',', $form_state['values']['scheme']); 139 foreach ($palette as $k => $color) { 140 $palette[$k] = array_shift($scheme); 141 } 142 } 143 144 vertical_tabs_generate_stylesheet($theme, $info, $palette); 145 } 146 147 /** 148 * Color module support for Garland and Vertical Tabs. 149 * 150 * Most of this function is direct copy/paste from color_scheme_form_submit(). 151 */ 152 function vertical_tabs_generate_stylesheet($theme, $info, $palette) { 153 // Current CSS files. 154 $css = variable_get('color_'. $theme .'_stylesheets', array()); 155 $files = variable_get('color_'. $theme .'_files', array()); 156 157 // Template file. 158 $file = 'vertical_tabs.garland.css'; 159 160 // Prepare target locations for generated files. 161 $paths['color'] = file_directory_path() .'/color'; 162 $paths['source'] = drupal_get_path('module', 'vertical_tabs') .'/garland/'; 163 $paths['files'] = $paths['map'] = array(); 164 165 if (count($css)) { 166 $paths['id'] = preg_replace('/^.*?('. $theme .'-[a-zA-Z0-9]+).*?$/', '$1', $css[0]); 167 $paths['target'] = $paths['color'] .'/'. $paths['id'] .'/'; 168 169 $style = drupal_load_stylesheet($paths['source'] . $file, FALSE); 170 171 // Rewrite stylesheet with new colors. 172 $style = _color_rewrite_stylesheet($theme, $info, $paths, $palette, $style); 173 $base_file = basename($file); 174 $css[] = $paths['target'] . $base_file; 175 $files[] = $paths['target'] . $base_file; 176 _color_save_stylesheet($paths['target'] . $base_file, $style, $paths); 177 178 // Update the list of files. 179 variable_set('color_'. $theme .'_stylesheets', $css); 180 variable_set('color_'. $theme .'_files', $files); 181 } 182 } 183 184 /** 185 * Implements hook_node_type(). 186 */ 187 function vertical_tabs_node_type($op, $info) { 188 if ($op == 'update' && !empty($info->old_type) && $info->old_type != $info->type) { 189 vertical_tabs_rename_config($info->type . '_node_form', $info->old_type . '_node_form'); 190 } 191 elseif ($op == 'delete') { 192 vertical_tabs_delete_config($info->type . '_node_form'); 193 } 194 } 195 196 /** 197 * Implements hook_fieldgroup_form(). 198 * 199 * Apply group value to CCK fieldsets with the 'vertical tab' form display. 200 */ 201 function vertical_tabs_fieldgroup_form(&$form, $form_state, $form_id, $group) { 202 if (in_array($group['settings']['form']['style'], array('fieldset_vertical_tab'))) { 203 $form[$group['group_name']]['#group'] = 'additional_settings'; 204 } 205 } 206 207 /** 208 * Implements hook_form_FORM_ID_alter(). 209 * 210 * Adds a 'vertical tab' form option on CCK fieldset settings. 211 */ 212 function vertical_tabs_form_fieldgroup_group_edit_form_alter(&$form, $form_state) { 213 $form['settings']['form']['style']['#options']['fieldset_vertical_tab'] = t('vertical tab'); 214 } 215 216 function vertical_tabs_get_config($form_id = NULL) { 217 static $config; 218 219 if (!isset($config)) { 220 $config = array(); 221 $config += variable_get('vertical_tabs_forms', array()); 222 $config += vertical_tabs_get_default_config(); 223 } 224 225 if (isset($form_id)) { 226 return isset($config[$form_id]) ? $config[$form_id] : NULL; 227 } 228 else { 229 return $config; 230 } 231 } 232 233 /** 234 * Get the default supported forms. 235 */ 236 function vertical_tabs_get_default_config() { 237 $forms = array(); 238 $node_types = array_keys(node_get_types('names')); 239 foreach ($node_types as $node_type) { 240 $forms[$node_type . '_node_form'] = TRUE; 241 } 242 $forms['node_type_form'] = TRUE; 243 $forms['block_admin_configure'] = TRUE; 244 $forms['block_add_block_form'] = TRUE; 245 return $forms; 246 } 247 248 function vertical_tabs_save_config($form_id, $fieldsets) { 249 $config = variable_get('vertical_tabs_forms', array()); 250 $config[$form_id] = $fieldsets; 251 variable_set('vertical_tabs_forms', $config); 252 } 253 254 function vertical_tabs_rename_config($old_form_id, $new_form_id) { 255 $config = variable_get('vertical_tabs_forms', array()); 256 if (isset($config[$form_id])) { 257 $config[$new_form_id] = $config[$old_form_id]; 258 unset($config[$old_form_id]); 259 variable_set('vertical_tabs_forms', $config); 260 } 261 } 262 263 function vertical_tabs_delete_config($form_id) { 264 $config = variable_get('vertical_tabs_forms', array()); 265 unset($config[$form_id]); 266 variable_set('vertical_tabs_forms', $config); 267 } 268 269 /** 270 * Get all the fieldset elements from a form. 271 */ 272 function vertical_tabs_get_form_elements(&$form) { 273 $elements = array(); 274 foreach (element_children($form) as $key) { 275 if (!isset($form[$key]['#type'])) { 276 // Ignore non-type elements. 277 continue; 278 } 279 elseif (!in_array($form[$key]['#type'], array('fieldset'))) { 280 // Ignore non-fieldset elements. 281 continue; 282 } 283 elseif (isset($form[$key]['#access']) && !$form[$key]['#access']) { 284 // Ignore elements the user cannot access. 285 continue; 286 } 287 288 $elements[$key] = &$form[$key]; 289 } 290 return $elements; 291 } 292 293 /** 294 * Form pre-render callback; add vertical tabs to the form. 295 */ 296 function vertical_tabs_form_pre_render($form) { 297 if (!isset($form['#vertical_tabs']) || !is_array($form['#vertical_tabs'])) { 298 $form['#vertical_tabs'] = array(); 299 } 300 vertical_tabs_add_vertical_tabs($form, $form['#vertical_tabs']); 301 return $form; 302 } 303 304 /** 305 * Add a vertical tab form element to a form. 306 * 307 * @param $form 308 * A form array to be altered. 309 * @param $config 310 * An array of fieldsets to use in the vertical tabs. If no array is provided, 311 * all the fieldsets in the $form array will be used. 312 * @return 313 * TRUE if the vertical tabs were added to the form, otherwise FALSE. 314 */ 315 function vertical_tabs_add_vertical_tabs(&$form, $config = array()) { 316 global $theme; 317 318 $settings = array(); 319 $weight = $delta = 0; 320 321 // Iterate through the form's fieldset elements. 322 $elements = vertical_tabs_get_form_elements($form); 323 foreach (array_keys($elements) as $key) { 324 $element = &$elements[$key]; 325 $element += array('#group' => variable_get('vertical_tabs_default', 1)); 326 327 // If there is a specific config set, override the default group setting. 328 if (isset($config[$key]) && (bool) $config[$key] != (bool) $element['#group']) { 329 $element['#group'] = $config[$key]; 330 } 331 332 // Skip any non-grouped elements. 333 if (empty($element['#group'])) { 334 continue; 335 } 336 337 // Process the element. 338 vertical_tabs_process_element($element, $key); 339 vertical_tabs_process_attached($element); 340 341 $settings[$key] = array( 342 'name' => $element['#title'], 343 'weight' => isset($element['#weight']) ? $element['#weight'] : 0, 344 'callback' => isset($element['#summary_callback']) ? $element['#summary_callback'] : $key, 345 'args' => isset($element['#summary_arguments']) ? $element['#summary_arguments'] : array(), 346 ); 347 348 // Track the maximum weight for the vertical tabs element. 349 $weight = max($weight, $settings[$key]['weight']); 350 351 $settings[$key]['weight'] += 0.001 * $delta++; 352 } 353 354 // The JavaScript and CSS specific for this form. 355 if (count($settings) >= variable_get('vertical_tabs_minimum', 1)) { 356 $js = $css = array(); 357 358 // Add theme-specific CSS. 359 if (isset($theme)) { 360 $theme_stylesheets = variable_get('color_' . $theme . '_stylesheets', array()); 361 if (!$theme_stylesheets || !module_exists('color')) { 362 // The theme-specific CSS will be only included by drupal_get_css() if 363 // it exists so we do not need to check file_exists() here. 364 $css[] = drupal_get_path('module', 'vertical_tabs') . '/' . $theme . '/vertical_tabs.' . $theme . '.css'; 365 } 366 else { 367 foreach ($theme_stylesheets as $path) { 368 if (strpos($path, 'vertical_tabs.' . $theme . '.css') !== FALSE) { 369 $css[] = $path; 370 } 371 } 372 } 373 } 374 375 // User sort orders by the "weight" key. 376 uasort($settings, '_user_sort'); 377 378 $form['vertical_tabs'] = array( 379 '#type' => 'markup', 380 '#value' => '', 381 '#theme' => 'vertical_tabs', 382 '#attributes' => array('class' => 'vertical-tabs clear-block'), 383 '#weight' => $weight, 384 '#attached' => array( 385 'js' => $js, 386 'css' => $css, 387 ), 388 ); 389 $form['vertical_tabs']['#attached']['js'][] = array('data' => array('verticalTabs' => $settings), 'type' => 'setting'); 390 391 // Resort the form since we've added a new element after it's been sorted. 392 uasort($form, 'element_sort'); 393 394 return TRUE; 395 } 396 } 397 398 /** 399 * Process an element for vertical tabs. 400 */ 401 function vertical_tabs_process_element(&$element, $key) { 402 // Merge defaults. 403 $element += array('#attributes' => array()); 404 $element['#attributes'] += array('class' => ''); 405 406 // Add a class to identify the fieldset. 407 $element['#attributes']['class'] .= ' vertical-tabs-fieldset vertical-tabs-' . $key; 408 409 return $element; 410 } 411 412 function vertical_tabs_process_attached($element) { 413 $element += array('#attached' => array()); 414 $element['#attached'] += array('js' => array(), 'css' => array()); 415 416 // Add any attached vertical tabs JavaScript. 417 // Copied from form_process_attached() in Drupal 7. 418 foreach (array('js', 'css') as $type) { 419 foreach ($element['#attached'][$type] as $data => $options) { 420 // If the value is not an array, it's a filename and passed as first 421 // (and only) argument. 422 if (!is_array($options)) { 423 $data = $options; 424 $options = array(); 425 } 426 // In some cases, the first parameter ($data) is an array. Arrays can't be 427 // passed as keys in PHP, so we have to get $data from the value array. 428 if (is_numeric($data)) { 429 $data = $options['data']; 430 unset($options['data']); 431 } 432 433 $options += array('type' => 'module'); 434 if ($type == 'js') { 435 drupal_add_js($data, $options['type']); 436 } 437 else { 438 drupal_add_css($data, $options['type']); 439 } 440 } 441 } 442 } 443 444 /** 445 * Remove vertical tabs elements and processing from a form. 446 */ 447 function vertical_tabs_remove_vertical_tabs(&$form) { 448 unset($form['#vertical_tabs']); 449 if (isset($form['vertical_tabs']['#vertical_tabs_settings'])) { 450 unset($form['vertical_tabs']); 451 } 452 if (isset($form['#pre_render'])) { 453 $form['#pre_render'] = array_diff($form['#pre_render'], array('vertical_tabs_form_pre_render')); 454 } 455 } 456 457 /** 458 * After build function to add vertical tabs JS and CSS to the form. 459 */ 460 function theme_vertical_tabs($element) { 461 static $added = FALSE; 462 463 if (!$added) { 464 drupal_add_js(drupal_get_path('module', 'vertical_tabs') . '/vertical_tabs.js'); 465 drupal_add_css(drupal_get_path('module', 'vertical_tabs') . '/vertical_tabs.css'); 466 $added = TRUE; 467 } 468 vertical_tabs_process_attached($element); 469 470 return '<div class="'. $element['#attributes']['class'] .'"> </div>'; 471 } 472 473 /** 474 * Implements hook_form_controller_info(). 475 */ 476 function vertical_tabs_form_info() { 477 $alterations['vertical_tabs_form'] = array( 478 'title' => t('Vertical tabs'), 479 'description' => t('Enables fieldsets to be collapsed into a vertical tab display.'), 480 'form callback' => 'vertical_tabs_form_configure', 481 ); 482 return $alterations; 483 } 484 485 function vertical_tabs_form_configure(&$complete_form, $form_id, $context) { 486 if ($elements = vertical_tabs_get_form_elements($context['form'])) { 487 // Fetch the configuration. 488 $config = vertical_tabs_get_config($form_id); 489 490 // Check if this form is currently vertical tabified. 491 $is_form_tabified = isset($context['form']['vertical_tabs']) || (isset($context['form']['#pre_render']) && in_array('vertical_tabs_form_pre_render', $context['form']['#pre_render'])); 492 493 $form['status'] = array( 494 '#type' => 'checkbox', 495 '#title' => t('Vertical tabify this form.'), 496 '#default_value' => isset($config) ? $config !== FALSE : $is_form_tabified, 497 ); 498 499 // Merge default values and correct non-key-matching values. 500 // @todo Keep settings for non-accessible fieldsets set by more priviledged users? 501 $defaults = is_array($config) ? $config : array(); 502 $options = array(); 503 foreach ($elements as $key => $element) { 504 $options[$key] = $element['#title']; 505 if (!isset($defaults[$key]) && !empty($element['#group'])) { 506 $defaults[$key] = $key; 507 } 508 } 509 510 $form['fieldsets'] = array( 511 '#type' => 'checkboxes', 512 '#title' => t('Include the following elements in the vertical tabs'), 513 '#options' => $options, 514 '#default_value' => $defaults, 515 ); 516 517 if (module_exists('ctools')) { 518 ctools_include('dependent'); 519 $form['fieldsets'] += array( 520 '#process' => array('expand_checkboxes', 'ctools_dependent_process'), 521 '#dependency' => array('edit-vertical-tabs-form-status' => array(TRUE)), 522 '#prefix' => '<div><div id="edit-vertical-tabs-form-fieldsets-wrapper">', 523 '#suffix' => '</div></div>', 524 ); 525 } 526 527 $complete_form['#submit'][] = 'vertical_tabs_form_configure_submit'; 528 return $form; 529 } 530 } 531 532 function vertical_tabs_form_configure_submit($form, &$form_state) { 533 $form_id = $form_state['form']['form_id']; 534 if ($form_state['values']['vertical_tabs_form']['status']) { 535 vertical_tabs_save_config($form_id, $form_state['values']['vertical_tabs_form']['fieldsets']); 536 } 537 else { 538 vertical_tabs_save_config($form_id, FALSE); 539 } 540 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |