| 1 |
<?php
|
| 2 |
// $Id: deadwood.formapi.inc,v 1.9 2009/02/22 16:00:33 solotandem Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Generate version upgrade code from 5.x to 6.x.
|
| 7 |
*
|
| 8 |
* The functions in this file match up with the 18 topics in the roadmap at
|
| 9 |
* http://drupal.org/node/144132.
|
| 10 |
*
|
| 11 |
* Copyright 2008 by Jim Berry ("solotandem", http://drupal.org/user/240748)
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Convert calls to hook_validate and submit() for parameter changes.
|
| 16 |
*
|
| 17 |
* @param string $file The file to convert.
|
| 18 |
*/
|
| 19 |
function deadwood_convert_formAPI(&$file) {
|
| 20 |
$hook = 'formAPI';
|
| 21 |
deadwood_debug_print('Start Form API conversions');
|
| 22 |
deadwood_convert_validate_submit($file);
|
| 23 |
deadwood_convert_form_alter2($file);
|
| 24 |
deadwood_convert_submit($file);
|
| 25 |
deadwood_convert_submit_params($file);
|
| 26 |
deadwood_convert_set_value($file);
|
| 27 |
deadwood_convert_multistep($file);
|
| 28 |
deadwood_convert_form_base($file);
|
| 29 |
deadwood_convert_button_handlers($file);
|
| 30 |
deadwood_convert_op_element($file);
|
| 31 |
deadwood_convert_passing_information($file);
|
| 32 |
deadwood_convert_retrieve_form($file);
|
| 33 |
deadwood_convert_hook_forms($file);
|
| 34 |
deadwood_convert_pre_render($file);
|
| 35 |
// Add these from observations of API changes not documented in roadmap.
|
| 36 |
deadwood_convert_form_builder($file);
|
| 37 |
deadwood_convert_get_form($file);
|
| 38 |
deadwood_convert_form_callback($file);
|
| 39 |
deadwood_debug_print('Finish Form API conversions');
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Convert calls to form-id_validate and submit() for parameter changes.
|
| 44 |
* Also convert calls to hook_validate and submit().
|
| 45 |
*
|
| 46 |
* @param string $file The file to convert.
|
| 47 |
*/
|
| 48 |
function deadwood_convert_validate_submit(&$file) {
|
| 49 |
/*
|
| 50 |
* TODO Could these module functions have added additional parameters from the core signature?
|
| 51 |
* We need to append any additional parameters to the end of the required parameters.
|
| 52 |
* Should we search for the string '_form_alter' and place a notification message?
|
| 53 |
* This might be present if the module called the hook itself.
|
| 54 |
* Are there variables inside this function we could/should rename?
|
| 55 |
*
|
| 56 |
* There could be multiple occurrences of these functions -- we need a loop.
|
| 57 |
*/
|
| 58 |
$hooks = array('validate', 'submit');
|
| 59 |
foreach ($hooks as $hook) {
|
| 60 |
// Process file in chunks.
|
| 61 |
$chunks = deadwood_find_hook($hook, $file, TRUE);
|
| 62 |
foreach ($chunks as $chunk) {
|
| 63 |
$cur = $chunk;
|
| 64 |
$new = $cur;
|
| 65 |
$new = deadwood_fix_validate($new, $hook);
|
| 66 |
deadwood_save_changes($cur, $new, $file, 'hook_' . $hook);
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Convert calls to form-id_validate and submit() for parameter changes.
|
| 73 |
* See http://api.drupal.org/api/function/form_execute_handlers/6.
|
| 74 |
* Are both parameters being passed by reference?
|
| 75 |
*
|
| 76 |
* @param string $file The file to convert.
|
| 77 |
* @return string The revised file.
|
| 78 |
*/
|
| 79 |
function deadwood_fix_validate(&$file, $hook) {
|
| 80 |
$cur = $file;
|
| 81 |
$new = $cur;
|
| 82 |
|
| 83 |
$from = array();
|
| 84 |
$to = array();
|
| 85 |
|
| 86 |
/*
|
| 87 |
* Ignore the hook_validate that applies to a node (because it has different
|
| 88 |
* parameters and they did not change from 5.x to 6.x). Because hook_submit
|
| 89 |
* is obsolete for nodes, any such function becomes a regular form-id_submit
|
| 90 |
* function with the same parameters as any other form. The node values are
|
| 91 |
* then part of form_state['values'].
|
| 92 |
*/
|
| 93 |
global $_deadwood_filename;
|
| 94 |
$module = pathinfo($_deadwood_filename, PATHINFO_FILENAME);
|
| 95 |
if ($hook == 'validate') {
|
| 96 |
$pattern = '/^function\s*' . $module . '_' . $hook . '\s*\(/m';
|
| 97 |
if (preg_match($pattern, $file, $matches)) {
|
| 98 |
return $cur;
|
| 99 |
}
|
| 100 |
}
|
| 101 |
elseif ($hook == 'submit') {
|
| 102 |
$pattern = '/^function\s*' . $module . '_' . $hook . '\s*\(/m';
|
| 103 |
if (preg_match($pattern, $file, $matches)) {
|
| 104 |
// Replace node with new form_state.
|
| 105 |
$from[] = '/\$node->([\w_]+)/';
|
| 106 |
$to[] = '$form_state[\'values\'][\'$1\']';
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
// Replace parameters in function signature.
|
| 111 |
$from[] = '/^(function.*)(\(.*\))/m';
|
| 112 |
$to[] = "$1(\$form, &\$form_state)";
|
| 113 |
// Replace form_values with new form_state.
|
| 114 |
$from[] = '/\$form_values(|\[)/';
|
| 115 |
$to[] = "\$form_state['values']$1";
|
| 116 |
// Replace form_id with new form_state.
|
| 117 |
$from[] = '/\$form_id/';
|
| 118 |
$to[] = '$form_state[\'values\'][\'form_id\']';
|
| 119 |
// Replace form[<element>]['#value'] with new form_state['#value'][<element>].
|
| 120 |
$from[] = '/\$form(\[\'\w+\'\])\[\'#value\'\]/';
|
| 121 |
$to[] = '$form_state[\'values\']$1';
|
| 122 |
|
| 123 |
deadwood_do_conversions($from, $to, $new);
|
| 124 |
return $new;
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Add note about new hook_form_$form-id_alter() functions.
|
| 129 |
*
|
| 130 |
* @param string $file The file to convert.
|
| 131 |
*/
|
| 132 |
function deadwood_convert_form_alter2(&$file) {
|
| 133 |
$hook = 'form_alter2';
|
| 134 |
$cur = $file;
|
| 135 |
$new = $cur;
|
| 136 |
|
| 137 |
// Check for form_alter functions.
|
| 138 |
$pattern = '/(?<!hook)_form_alter\s*(\(.*\))/';
|
| 139 |
if (!preg_match($pattern, $file, $matches)) {
|
| 140 |
return;
|
| 141 |
}
|
| 142 |
|
| 143 |
$msg =
|
| 144 |
"/* TODO You may want to take advantage of new form-specific alter hooks.
|
| 145 |
The hook_form_alter() is complemented by hook_form_\$form-id_alter().
|
| 146 |
Optionally, modules can implement form-specific alteration functions rather
|
| 147 |
than a single hook_form_alter() with many conditional switch statements.
|
| 148 |
This is optional, and is most useful for tidying the code of modules that
|
| 149 |
alter many forms to customize a site's operations.
|
| 150 |
The new function names are built using the following format:
|
| 151 |
[your-module-name]_form_[\$form_id]_alter. */";
|
| 152 |
|
| 153 |
$from = array();
|
| 154 |
$to = array();
|
| 155 |
// Find the Id line in the file.
|
| 156 |
$from[] = '/^(\/\/ \$Id.*\$\s*$)/m';
|
| 157 |
$to[] = "$1\n$msg\n";
|
| 158 |
|
| 159 |
deadwood_do_conversions($from, $to, $new);
|
| 160 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* Convert return values in calls to form-id_submit() for API changes.
|
| 165 |
*
|
| 166 |
* @param string $file The file to convert.
|
| 167 |
*/
|
| 168 |
function deadwood_convert_submit(&$file) {
|
| 169 |
$hook = 'submit';
|
| 170 |
$cur = deadwood_find_hook($hook, $file);
|
| 171 |
$new = $cur;
|
| 172 |
|
| 173 |
$from = array();
|
| 174 |
$to = array();
|
| 175 |
// Change return statement to $form_state['redirect'].
|
| 176 |
$from[] = '/^(.*)return\s*(.+);/m';
|
| 177 |
$to[] = "$1\$form_state['redirect'] = $2;";
|
| 178 |
|
| 179 |
deadwood_do_conversions($from, $to, $new);
|
| 180 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 181 |
}
|
| 182 |
|
| 183 |
/**
|
| 184 |
* Remove parameters to $form['#submit'], $form['#validate'], and
|
| 185 |
* $form['#process'].
|
| 186 |
*
|
| 187 |
* @param string $file The file to convert.
|
| 188 |
*/
|
| 189 |
function deadwood_convert_submit_params(&$file) {
|
| 190 |
$cur = $file;
|
| 191 |
$new = $cur;
|
| 192 |
|
| 193 |
$hooks = array('validate', 'submit', 'process');
|
| 194 |
foreach ($hooks as $hook) {
|
| 195 |
// Remove parameters to $form['#hook'].
|
| 196 |
$search = '/^(\s*)(\$\w+)' . "(\['#$hook'\])\[(.*)\]\s*=\s*array\s*\((.*)\);/m";
|
| 197 |
$callback = 'deadwood_fix_submit_params1';
|
| 198 |
$chg = preg_replace_callback($search, $callback, $new);
|
| 199 |
$new = $chg != '' ? $chg : $new;
|
| 200 |
// See fivestar_elements function that defines #process outside of $form
|
| 201 |
$search = "/^(\s*)(\'#$hook')\s*=>\s*array\s*\((.*)\)(,|;)$/m";
|
| 202 |
$callback = 'deadwood_fix_submit_params2';
|
| 203 |
$chg = preg_replace_callback($search, $callback, $new);
|
| 204 |
$new = $chg != '' ? $chg : $new;
|
| 205 |
}
|
| 206 |
$hook = 'submit_params';
|
| 207 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Callback function for converting parameters.
|
| 212 |
* $form['#process']['element_process'] = array('abc' => 'xyz', 'def' => '123')
|
| 213 |
* $form['#process']['element_process'] = array('xyz', '123')
|
| 214 |
* $form['#process']['element_process'] = array()
|
| 215 |
*
|
| 216 |
* @param array $matches Function arguments.
|
| 217 |
* @return Text of converted function call.
|
| 218 |
*/
|
| 219 |
function deadwood_fix_submit_params1($matches) {
|
| 220 |
$new = array();
|
| 221 |
$new[] = $matches[1] . $matches[2] . $matches[3] . '[] = ' . $matches[4] . ';';
|
| 222 |
|
| 223 |
$i = 1;
|
| 224 |
$params = explode(',', $matches[5]);
|
| 225 |
foreach ($params as $param) {
|
| 226 |
// TODO Instruct the user to name each parameter.
|
| 227 |
if (strlen(trim($param))) {
|
| 228 |
if (strpos($param, '=>')) {
|
| 229 |
$tokens = explode('=>', $param);
|
| 230 |
$new[] = $matches[1] . $matches[2] . "['#" . trim($tokens[0], "' ") . "'] = " . trim($tokens[1]) . ',';
|
| 231 |
}
|
| 232 |
else {
|
| 233 |
$new[] = $matches[1] . $matches[2] . "['#" . trim($matches[4], "' ") . "_param_$i'] = " . trim($param) . ',';
|
| 234 |
}
|
| 235 |
$i++;
|
| 236 |
}
|
| 237 |
}
|
| 238 |
$new = implode("\n", $new);
|
| 239 |
return $new;
|
| 240 |
}
|
| 241 |
|
| 242 |
/**
|
| 243 |
* Callback function for converting parameters.
|
| 244 |
* Code assumes only one $key => array() inside the outer array.
|
| 245 |
* '#process' => array('element_process' => array('abc' => 'xyz', 'def' => '123'))
|
| 246 |
* '#process' => array('element_process' => array('xyz', '123'))
|
| 247 |
* '#process' => array('element_process' => array())
|
| 248 |
* '#process' => array('element_process')
|
| 249 |
*
|
| 250 |
* @param array $matches Function arguments.
|
| 251 |
* @return Text of converted function call.
|
| 252 |
*/
|
| 253 |
function deadwood_fix_submit_params2($matches) {
|
| 254 |
$new = array();
|
| 255 |
|
| 256 |
// Fourth case above -- nothing to change.
|
| 257 |
if (!strpos($matches[3], 'array(')) {
|
| 258 |
return $matches[0];
|
| 259 |
}
|
| 260 |
|
| 261 |
// Get function name.
|
| 262 |
$function = substr($matches[3], 0, strpos($matches[3], 'array(') - 1);
|
| 263 |
$function = trim($function, '=> ');
|
| 264 |
$new[] = $matches[1] . $matches[2] . ' => array(' . $function . ')' . $matches[4];
|
| 265 |
|
| 266 |
// Parse parameters.
|
| 267 |
$i = 1;
|
| 268 |
$params = substr($matches[3], strpos($matches[3], 'array(') + strlen('array('));
|
| 269 |
$params = trim($params, ')');
|
| 270 |
$params = explode(',', $params);
|
| 271 |
foreach ($params as $param) {
|
| 272 |
if (strlen(trim($param))) {
|
| 273 |
if (strpos($param, '=>')) {
|
| 274 |
$tokens = explode('=>', $param);
|
| 275 |
$new[] = $matches[1] . "'#" . trim($tokens[0], "' ") . "' => " . trim($tokens[1]) . ',';
|
| 276 |
}
|
| 277 |
else {
|
| 278 |
$new[] = $matches[1] . "'#" . trim($function, "' ") . "_param_$i' => " . trim($param) . ',';
|
| 279 |
}
|
| 280 |
$i++;
|
| 281 |
}
|
| 282 |
}
|
| 283 |
$new = implode("\n", $new);
|
| 284 |
return $new;
|
| 285 |
}
|
| 286 |
|
| 287 |
/**
|
| 288 |
* Revise paramters in calls to form_set_value().
|
| 289 |
*
|
| 290 |
* @param string $file The file to convert.
|
| 291 |
*/
|
| 292 |
function deadwood_convert_set_value(&$file) {
|
| 293 |
$hook = 'set_value';
|
| 294 |
$cur = $file;
|
| 295 |
$new = $cur;
|
| 296 |
|
| 297 |
$from = array();
|
| 298 |
$to = array();
|
| 299 |
// Add $form_state to paramters.
|
| 300 |
$from[] = '/(form_set_value)\s*\((.*)\)/';
|
| 301 |
$to[] = "$1($2, \$form_state)";
|
| 302 |
|
| 303 |
deadwood_do_conversions($from, $to, $new);
|
| 304 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 305 |
}
|
| 306 |
|
| 307 |
/**
|
| 308 |
* Replace multistep parameter with rebuild paramter.
|
| 309 |
*
|
| 310 |
* @param string $file The file to convert.
|
| 311 |
*/
|
| 312 |
function deadwood_convert_multistep(&$file) {
|
| 313 |
$hook = 'multistep';
|
| 314 |
$cur = $file;
|
| 315 |
$new = $cur;
|
| 316 |
|
| 317 |
$msg =
|
| 318 |
"/* TODO #multistep is gone, use \$form_state instead.
|
| 319 |
Move the rebuild statement to the submit function for this form. */";
|
| 320 |
|
| 321 |
$from = array();
|
| 322 |
$to = array();
|
| 323 |
// Replace multistep parameters.
|
| 324 |
$from[] = '/^(.*\$form\[\'#multistep\'\].*$)/m';
|
| 325 |
$to[] = "$msg\n\t\$form_state['rebuild'] = TRUE;\n//$1";
|
| 326 |
// Comment out redirect parameters.
|
| 327 |
$from[] = '/^(.*\$form\[\'#redirect\'\].*$)/m';
|
| 328 |
$to[] = "//$1";
|
| 329 |
|
| 330 |
deadwood_do_conversions($from, $to, $new);
|
| 331 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 332 |
}
|
| 333 |
|
| 334 |
// Not sure how to isolate for #element_validate changes?
|
| 335 |
|
| 336 |
/**
|
| 337 |
* Add note about elimination of $form['#base'].
|
| 338 |
*
|
| 339 |
* @param string $file The file to convert.
|
| 340 |
*/
|
| 341 |
function deadwood_convert_form_base(&$file) {
|
| 342 |
$hook = 'form_base';
|
| 343 |
$cur = $file;
|
| 344 |
$new = $cur;
|
| 345 |
|
| 346 |
// Check for form_alter functions.
|
| 347 |
$pattern = '/\'#base\'/';
|
| 348 |
if (!preg_match($pattern, $file, $matches)) {
|
| 349 |
return;
|
| 350 |
}
|
| 351 |
|
| 352 |
$msg =
|
| 353 |
"/* TODO \$form['#base'] is gone
|
| 354 |
In FormAPI, many forms with different form_ids can share the same validate,
|
| 355 |
submit, and theme handlers. This can be done by manually populating the
|
| 356 |
\$form['#submit'], \$form['#validate'], and \$form['#theme'] elements with
|
| 357 |
the proper function names. */";
|
| 358 |
|
| 359 |
$from = array();
|
| 360 |
$to = array();
|
| 361 |
// Find the Id line in the file.
|
| 362 |
$from[] = '/^(\/\/ \$Id.*\$\s*$)/m';
|
| 363 |
$to[] = "$1\n$msg\n";
|
| 364 |
|
| 365 |
deadwood_do_conversions($from, $to, $new);
|
| 366 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 367 |
}
|
| 368 |
|
| 369 |
// #post_render complements #pre_render
|
| 370 |
|
| 371 |
/**
|
| 372 |
* Add note about custom handlers on form buttons.
|
| 373 |
*
|
| 374 |
* @param string $file The file to convert.
|
| 375 |
*/
|
| 376 |
function deadwood_convert_button_handlers(&$file) {
|
| 377 |
$hook = 'form_base';
|
| 378 |
$cur = $file;
|
| 379 |
$new = $cur;
|
| 380 |
|
| 381 |
// Check for form_alter functions.
|
| 382 |
$pattern = '/(\$form_values|\$form_state\[\'values\'\])\[\'op\'\]/';
|
| 383 |
if (!preg_match($pattern, $file, $matches)) {
|
| 384 |
return;
|
| 385 |
}
|
| 386 |
|
| 387 |
$msg =
|
| 388 |
"/* TODO Form buttons can define custom #submit and #validate handlers.
|
| 389 |
All forms can have #validate and #submit properties containing lists of
|
| 390 |
validation and submission handlers to be executed when a user submits data.
|
| 391 |
Previously, if a form featured multiple submission buttons to initiate
|
| 392 |
different actions (updating a record versus deleting, for example), it was
|
| 393 |
necessary to check the incoming form_values['op'] for the name of the
|
| 394 |
clicked button, then execute different code based on its value. Now, it is
|
| 395 |
possible to define #validate and #submit properties on each individual form
|
| 396 |
button if desired. */";
|
| 397 |
|
| 398 |
$from = array();
|
| 399 |
$to = array();
|
| 400 |
// Find the Id line in the file.
|
| 401 |
$from[] = '/^(\/\/ \$Id.*\$\s*$)/m';
|
| 402 |
$to[] = "$1\n$msg\n";
|
| 403 |
|
| 404 |
deadwood_do_conversions($from, $to, $new);
|
| 405 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 406 |
}
|
| 407 |
|
| 408 |
/**
|
| 409 |
* Add note about deprecation of the op element from form_values.
|
| 410 |
*
|
| 411 |
* @param string $file The file to convert.
|
| 412 |
*/
|
| 413 |
function deadwood_convert_op_element(&$file) {
|
| 414 |
$hook = 'op_element';
|
| 415 |
$cur = $file;
|
| 416 |
$new = $cur;
|
| 417 |
|
| 418 |
$msg =
|
| 419 |
"/* TODO The 'op' element in the form values is deprecated.
|
| 420 |
Each button can have #validate and #submit functions associated with it.
|
| 421 |
Thus, there should be one button that submits the form and which invokes
|
| 422 |
the normal form_id_validate and form_id_submit handlers. Any additional
|
| 423 |
buttons which need to invoke different validate or submit functionality
|
| 424 |
should have button-specific functions. */";
|
| 425 |
|
| 426 |
$from = array();
|
| 427 |
$to = array();
|
| 428 |
// Comment on deprecated element.
|
| 429 |
$from[] = '/^(.*\$form_values\[\'op\'\].*$)/m';
|
| 430 |
$to[] = "$msg\n$1";
|
| 431 |
// Comment on deprecated element.
|
| 432 |
$from[] = '/^(.*\$form_state\[\'values\'\]\[\'op\'\].*$)/m';
|
| 433 |
$to[] = "$msg\n$1";
|
| 434 |
|
| 435 |
deadwood_do_conversions($from, $to, $new);
|
| 436 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 437 |
}
|
| 438 |
|
| 439 |
/**
|
| 440 |
* Add note about validation handlers passing information to submit handlers.
|
| 441 |
*
|
| 442 |
* @param string $file The file to convert.
|
| 443 |
*/
|
| 444 |
function deadwood_convert_passing_information(&$file) {
|
| 445 |
$hook = 'validate';
|
| 446 |
$cur = $file;
|
| 447 |
$new = $cur;
|
| 448 |
|
| 449 |
$msg =
|
| 450 |
"/* TODO Validation handlers can pass information to submit handlers
|
| 451 |
Previously, it was necessary to embed validated results in the \$form_values
|
| 452 |
to carry them over from validation to submission. Now, though, validation
|
| 453 |
handlers can simply place them into \$form_state. This keeps the new
|
| 454 |
\$form_state['values'] clean, and ensures that it always represents the data
|
| 455 |
submitted by the web user. */";
|
| 456 |
|
| 457 |
$from = array();
|
| 458 |
$to = array();
|
| 459 |
// Add note about passing information.
|
| 460 |
$from[] = '/^(function.*)$/m';
|
| 461 |
$to[] = "$1\n$msg\n";
|
| 462 |
|
| 463 |
// Process file in chunks.
|
| 464 |
$chunks = deadwood_find_hook($hook, $file, TRUE);
|
| 465 |
foreach ($chunks as $chunk) {
|
| 466 |
$cur = $chunk;
|
| 467 |
$new = $cur;
|
| 468 |
|
| 469 |
$pattern = '/form_set_value\s*\(/';
|
| 470 |
if (preg_match($pattern, $cur, $matches)) {
|
| 471 |
deadwood_do_conversions($from, $to, $new);
|
| 472 |
deadwood_save_changes($cur, $new, $file, 'passing_information');
|
| 473 |
}
|
| 474 |
}
|
| 475 |
}
|
| 476 |
|
| 477 |
/**
|
| 478 |
* Change parameters of drupal_retrieve_form() and add a note.
|
| 479 |
*
|
| 480 |
* @param string $file The file to convert.
|
| 481 |
*/
|
| 482 |
function deadwood_convert_retrieve_form(&$file) {
|
| 483 |
$hook = 'retrieve_form';
|
| 484 |
$cur = $file;
|
| 485 |
$new = $cur;
|
| 486 |
|
| 487 |
$msg =
|
| 488 |
"/* TODO drupal_retrieve_form() now accepts a form_state parameter. */";
|
| 489 |
|
| 490 |
$from = array();
|
| 491 |
$to = array();
|
| 492 |
// Comment on deprecated element.
|
| 493 |
// Handle extra form parameters.
|
| 494 |
$from[] = '/^(.*drupal_retrieve_form)\s*\((.+?)(,.+){0,}?\)(.*)$/m';
|
| 495 |
$to[] = "$msg\n$1($2, \$form_state$3)$4";
|
| 496 |
|
| 497 |
deadwood_do_conversions($from, $to, $new);
|
| 498 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 499 |
}
|
| 500 |
|
| 501 |
/**
|
| 502 |
* Change parameters of hook_forms().
|
| 503 |
*
|
| 504 |
* @param string $file The file to convert.
|
| 505 |
*/
|
| 506 |
function deadwood_convert_hook_forms(&$file) {
|
| 507 |
$hook = 'forms';
|
| 508 |
$cur = deadwood_find_hook($hook, $file);
|
| 509 |
$new = $cur;
|
| 510 |
$hook = 'hook_forms';
|
| 511 |
|
| 512 |
$from = array();
|
| 513 |
$to = array();
|
| 514 |
|
| 515 |
/*
|
| 516 |
* The fivestar module has no parameter in its function signature and calls
|
| 517 |
* $args = func_get_args();
|
| 518 |
* The $args array seems to have another level to it with $args[0][0] being
|
| 519 |
* $form_id.
|
| 520 |
* So handle two cases.
|
| 521 |
*/
|
| 522 |
|
| 523 |
// Check for function with no parameters in its signature.
|
| 524 |
$pattern = '/function.*\((\s*)\)/';
|
| 525 |
if (preg_match_all($pattern, $cur, $matches)) {
|
| 526 |
// Add note about reducing index values by one.
|
| 527 |
$msg =
|
| 528 |
"/* TODO Your function did not have \$args in its signature.
|
| 529 |
Any \$args[0][n] values have been converted to \$args[n].
|
| 530 |
You may need to reduce these indices by one. */";
|
| 531 |
$from[] = '/^(function.*\(\s*\).*$)/m';
|
| 532 |
$to[] = "$1\n$msg";
|
| 533 |
|
| 534 |
// Replace parameters in function signature.
|
| 535 |
$from[] = '/^(function.*)\((\s*)\)/m';
|
| 536 |
$to[] = "$1(\$form_id, \$args)";
|
| 537 |
// Replace args[0][0] with form_id.
|
| 538 |
$from[] = '/(\$args\[0\]\[0\])/m';
|
| 539 |
$to[] = "\$form_id";
|
| 540 |
// Replace args[0][n] with args[n-1]. Should we leave a note about this?
|
| 541 |
$from[] = '/(\$args\[0\]\[(.+?)\])/m';
|
| 542 |
$to[] = "\$args[$2]";
|
| 543 |
}
|
| 544 |
else {
|
| 545 |
// Replace parameters in function signature.
|
| 546 |
$from[] = '/^(function.*)\((.+)\)/m';
|
| 547 |
$to[] = "$1(\$form_id, $2)";
|
| 548 |
// Replace args[0] with form_id.
|
| 549 |
$from[] = '/(\$args\[0\])/m';
|
| 550 |
$to[] = "\$form_id";
|
| 551 |
}
|
| 552 |
|
| 553 |
deadwood_do_conversions($from, $to, $new);
|
| 554 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 555 |
}
|
| 556 |
|
| 557 |
/**
|
| 558 |
* Change parameters to a $form['#pre_render'] function.
|
| 559 |
*
|
| 560 |
* @param string $file The file to convert.
|
| 561 |
*/
|
| 562 |
function deadwood_convert_pre_render(&$file) {
|
| 563 |
$hook = 'pre_render';
|
| 564 |
$cur = $file;
|
| 565 |
$new = $cur;
|
| 566 |
|
| 567 |
/*
|
| 568 |
* Steps.
|
| 569 |
* First, check for ['#pre_render']['my_function'].
|
| 570 |
* Second, convert pre_render function from array key to array value.
|
| 571 |
* Third, find my_function.
|
| 572 |
* Replace parameters in function signature.
|
| 573 |
* Replace $form_id with $form['#id'].
|
| 574 |
*/
|
| 575 |
|
| 576 |
// Check for form['#hook'] statements.
|
| 577 |
$pattern = '/\$form' . "\['#$hook']\['(.*)'\]/";
|
| 578 |
if (!preg_match_all($pattern, $file, $matches)) {
|
| 579 |
return;
|
| 580 |
}
|
| 581 |
$functions = $matches[1];
|
| 582 |
|
| 583 |
$from = array();
|
| 584 |
$to = array();
|
| 585 |
// Convert pre_render function from array key to array value.
|
| 586 |
$from[] = '/(\$form' . "\['#$hook'])\[(.*)\].*=\s*array\s*\((.*)\);/";
|
| 587 |
$to[] = "$1[] = $2;";
|
| 588 |
|
| 589 |
deadwood_do_conversions($from, $to, $new);
|
| 590 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 591 |
|
| 592 |
// Find each pre_render function.
|
| 593 |
foreach ($functions as $function) {
|
| 594 |
$cur = deadwood_find_hook($function, $file, FALSE, FALSE);
|
| 595 |
$new = $cur;
|
| 596 |
$new = deadwood_fix_pre_render($new);
|
| 597 |
deadwood_save_changes($cur, $new, $file, $hook . ' = ' . $function);
|
| 598 |
}
|
| 599 |
}
|
| 600 |
|
| 601 |
/**
|
| 602 |
* Convert calls to pre_render function for parameter changes.
|
| 603 |
*
|
| 604 |
* @param string $file The file to convert.
|
| 605 |
* @return string The revised file.
|
| 606 |
*/
|
| 607 |
function deadwood_fix_pre_render(&$file) {
|
| 608 |
$cur = $file;
|
| 609 |
$new = $cur;
|
| 610 |
|
| 611 |
$from = array();
|
| 612 |
$to = array();
|
| 613 |
// Replace parameters in function signature.
|
| 614 |
$from[] = '/^(function.*)(\(.*\))/m';
|
| 615 |
$to[] = "$1(\$form)";
|
| 616 |
// Replace form_id with form[].
|
| 617 |
$from[] = '/\$form_id/';
|
| 618 |
$to[] = '$form[\'#id\']';
|
| 619 |
|
| 620 |
deadwood_do_conversions($from, $to, $new);
|
| 621 |
return $new;
|
| 622 |
}
|
| 623 |
|
| 624 |
/*
|
| 625 |
* The following changes are not specifically mentioned in the roadmap.
|
| 626 |
*/
|
| 627 |
|
| 628 |
/**
|
| 629 |
* Convert calls to form_builder function for parameter changes.
|
| 630 |
*
|
| 631 |
* @param string $file The file to convert.
|
| 632 |
*/
|
| 633 |
function deadwood_convert_form_builder(&$file) {
|
| 634 |
$hook = 'form_builder';
|
| 635 |
$cur = $file;
|
| 636 |
$new = $cur;
|
| 637 |
|
| 638 |
$msg =
|
| 639 |
"/* TODO form_builder() now accepts a form_state parameter. */";
|
| 640 |
|
| 641 |
$from = array();
|
| 642 |
$to = array();
|
| 643 |
// Add $form_state to parameters of function call.
|
| 644 |
$from[] = '/^(.*)(form_builder)\s*\((.*)\)(.*)$/m';
|
| 645 |
$to[] = "$msg\n\t\$form_state = array();\n$1$2($3, \$form_state)$4";
|
| 646 |
|
| 647 |
deadwood_do_conversions($from, $to, $new);
|
| 648 |
deadwood_save_changes($cur, $new, $file, $hook);
|
| 649 |
}
|
| 650 |
|
| 651 |
/**
|
| 652 |
* Convert calls to drupal_get_form function for parameter changes.
|
| 653 |
*
|
| 654 |
* @param string $file The file to convert.
|
| 655 |
*/
|
| 656 |
function deadwood_convert_get_form(&$file) {
|
| 657 |
$hook = 'get_form';
|
| 658 |
$cur = $file;
|
| 659 |
$new = $cur;
|
| 660 |
|
| 661 |
/*
|
| 662 |
* Steps.
|
| 663 |
* First, check for calls to drupal_get_form('my_function', ...).
|
| 664 |
* Second, add $form_state to parameters of these function calls.
|
| 665 |
* Third, find my_function.
|
| 666 |
* Add $form_state to parameters in function signature.
|
| 667 |
*/
|
| 668 |
|
| 669 |
// Check for calls to drupal_get_form.
|
| 670 |
$pattern = '/drupal_get_form\s*\(\s*(\'|")(\w+)(\'|").*\)/'; // '/drupal_get_form\s*\(\s*\'(\w+)\'\s*,.*\)/';
|
| 671 |
if (!preg_match_all($pattern, $file, $matches)) {
|
| 672 |
return;
|
| 673 |
}
|
| 674 |
$functions = array_unique($matches[2]);
|
| 675 |
|
| 676 |
// Find each form function.
|
| 677 |
foreach ($functions as $function) {
|
| 678 |
$cur = $file;
|
| 679 |
$new = $cur;
|
| 680 |
$from = array();
|
| 681 |
$to = array();
|
| 682 |
// Add $form_state to parameters in function calls and function signature.
|
| 683 |
// Omit functions with prefixes to the name, e.g. theme_$function.
|
| 684 |
// Handle items with parameters.
|
| 685 |
$from[] = "/(?<!\w_)($function)\s*\((.+)\)/";
|
| 686 |
$to[] = "$1(\$form_state, $2)";
|
| 687 |
// Handle items without parameters.
|
| 688 |
$from[] = "/(?<!\w_)($function)\s*\(\s*\)/";
|
| 689 |
$to[] = "$1(\$form_state)";
|
| 690 |
// Add '&' before $form_state in function signature.
|
| 691 |
$from[] = "/^(function\s*$function)\s*\((.*)\)/m";
|
| 692 |
$to[] = "$1(&$2)";
|
| 693 |
|
| 694 |
deadwood_do_conversions($from, $to, $new);
|
| 695 |
deadwood_save_changes($cur, $new, $file, $hook . ' = ' . $function);
|
| 696 |
}
|
| 697 |
}
|
| 698 |
|
| 699 |
/**
|
| 700 |
* Convert calls to form functions for parameter changes.
|
| 701 |
*
|
| 702 |
* @param string $file The file to convert.
|
| 703 |
*/
|
| 704 |
function deadwood_convert_form_callback(&$file) {
|
| 705 |
$hook = 'form_callback';
|
| 706 |
$cur = $file;
|
| 707 |
$new = $cur;
|
| 708 |
|
| 709 |
/*
|
| 710 |
* Steps.
|
| 711 |
* First, check for form callback functions with parameters.
|
| 712 |
* Second, add $form_state to parameters in these function signatures.
|
| 713 |
*/
|
| 714 |
|
| 715 |
// Check for form callback functions called by drupal_get_form or related.
|
| 716 |
// This excludes functions defined as a page callback in the menu hook.
|
| 717 |
$callbacks = '(page arguments|callback)';
|
| 718 |
$inbetween = '(]\s*=\s*|\s*=>\s*)';
|
| 719 |
$pattern = "/'$callbacks'$inbetween'(\w+)'/";
|
| 720 |
if (!preg_match_all($pattern, $file, $matches)) {
|
| 721 |
return;
|
| 722 |
}
|
| 723 |
$functions = array_unique($matches[3]);
|
| 724 |
|
| 725 |
// Find each form function.
|
| 726 |
foreach ($functions as $function) {
|
| 727 |
$cur = $file;
|
| 728 |
$new = $cur;
|
| 729 |
|
| 730 |
$from = array();
|
| 731 |
$to = array();
|
| 732 |
// Add $form_state to parameters in function signature.
|
| 733 |
$from[] = "/^(function\s*$function)\s*\((.+)\)/m";
|
| 734 |
$to[] = "$1(&\$form_state, $2)";
|
| 735 |
|
| 736 |
deadwood_do_conversions($from, $to, $new);
|
| 737 |
deadwood_save_changes($cur, $new, $file, $hook . ' = ' . $function);
|
| 738 |
}
|
| 739 |
}
|