Index: formupdater.info =================================================================== RCS file: formupdater.info diff -N formupdater.info --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ formupdater.info 17 Jul 2008 05:00:48 -0000 @@ -0,0 +1,3 @@ +; $Id$ +name = Form Updater +description = Regex engine to help with module conversion. \ No newline at end of file Index: formupdater.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/formupdater/formupdater.module,v retrieving revision 1.7 diff -u -r1.7 formupdater.module --- formupdater.module 28 Nov 2005 23:09:37 -0000 1.7 +++ formupdater.module 17 Jul 2008 05:00:48 -0000 @@ -1,57 +1,77 @@ Form Updater will search through your old Drupal code to find calls to old form functions (such as form_textfield, form_radios, etc). It will then reformat these functions as arrays for use with the new Drupal Forms API.

'); } } -function formupdater_menu($may_cache){ +/** + * Implementation of hook_menu(). + */ +function formupdater_menu() { $items = array(); - if ($may_cache){ - $items[] = array('path'=>'formupdater', 'title'=> t('form updater'), 'access' => user_access('access content'), 'callback' => 'formupdater_page'); - } + $items[] = array( + 'path' => 'formupdater', + 'title' => t('Form updater'), + 'access' => user_access('access content'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('formupdater_form') + ); return $items; } -function formupdater_page(){ - if($edit = $_REQUEST['edit']){ - return formupdater_process($edit); - } - $form['help'] = array('#type' => 'markup', '#value' => t('

Form Updater will search through your old drupal code to find calls to old form functions (such as form_textfield, form_radios, etc). It will then reformat these functions as arrays for use with the new Drupal 4.7 formapi.

')); - $form['input'] = array('#type' => 'textarea', '#title'=> t('Drupal PHP code containing old form function calls'), '#rows' => 20); - $form['submit'] = array('#type' => 'submit', '#value' => 'submit'); - $output = drupal_get_form('formupdater_input', $form); - $output .= str_replace(array('$RCSf'.'ile:', ',v', ',', '$Re'.'vision: ', '$Da'.'te: ', '$'), '', '

$RCSfile: formupdater.module,v $ version: $Revision: 1.7 $, $Date: 2005/11/28 23:09:37 $

'); - return $output; +function formupdater_form() { + $form = array(); + $form['code'] = array( + '#type' => 'textarea', + '#title' => t('Drupal PHP code containing old form function calls'), + '#rows' => 20 + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Process code') + ); + return $form; } -function formupdater_process($edit){ - //var_dump($form_values); - $code = $edit['input']; +function formupdater_form_submit($form_id, $form_values) { + $code = $form_values['code']; $regex = '/\s(form_(.*?)|form())\(([\w\W]*?)\);/i'; preg_match_all($regex, $code, $matches, PREG_SET_ORDER); $i = 1; - if (is_array($matches)){ - foreach ($matches as $match){ + if (is_array($matches)) { + foreach ($matches as $match) { //print($match[1]); - if($array = formupdater_convert($match[2], $match[4])) { + if ($array = formupdater_convert($match[2], $match[4])) { $note = $array['note']; unset($array['note']); - $form['x_'.$i] = array( + $form['x_'. $i] = array( '#type' => 'fieldset', - '#title' => $match[2] ? 'form_'. $match[2].'()' : 'form()', + '#title' => $match[2] ? 'form_'. $match[2] .'()' : 'form()', '#collapsible' => true ); - $form['x_'.$i]['before'] = array('#type' => 'item', '#title' => t('Drupal 4.6'), '#value' => htmlentities($match[0])); - foreach ($array as $name => $args){ - if(strlen($match[2])){ - $code = "\$form[".str_replace('][', "']['", $name)."] = array(\n "; + $form['x_'. $i]['before'] = array( + '#type' => 'item', + '#title' => t('Drupal 4.6'), + '#value' => htmlentities($match[0]), + '#weight' => 0 + ); + foreach ($array as $name => $args) { + if (drupal_strlen($match[2])) { + $code = "\$form[". str_replace('][', "']['", $name) ."] = array(\n "; $argsout = array(); - foreach($args as $key => $val){ + foreach ($args as $key => $val) { $argsout[] = "'$key' => $val"; } $code .= implode(",\n ", $argsout); @@ -60,18 +80,23 @@ else { // this is a form() call $code = ''; - foreach($args as $key => $val){ - if ($key != '#type' && $key != '#title'){ + foreach ($args as $key => $val) { + if ($key != '#type' && $key != '#title') { $code .= "\$form['$key'] = $val;\n"; } } - $code .= "\$output = drupal_get_form('your_form_id', \$form);"; + $code .= "\$output = drupal_render(\$form);"; } - $output .= $code; - } $lines = substr_count($code, "\n") + 1; - $form['x_'.$i]['after'] = array('#type' => 'textarea', '#title' => t('Drupal 4.7'), '#default_value' => $code, '#rows' => $lines); + //var_dump($code); + $form['x_'. $i]['after'] = array( + '#type' => 'textarea', + '#title' => t('Forms API'), + '#rows' => $lines, + '#value' => $code, + '#weight' => 1 + ); /* //notes for different types $d = ''; @@ -82,24 +107,25 @@ break; } */ - $form['x_'.$i]['after']['#description'] = $note; + $form['x_'. $i]['after']['#description'] = $note; $i++; } } } - $output = drupal_get_form('formupdater', $form); - return $output; + $output = drupal_render($form); + print theme('page', $output); + exit; } -function formupdater_convert($type, $args){ +function formupdater_convert($type, $args) { $type = trim($type); //temporarily replace commas in sub-arguments //split by parenthasis $parensplit = preg_split('/([\(\)])/', $args, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $open = 0; - foreach((array)$parensplit as $parenchunk){ - switch($parenchunk){ + foreach ((array)$parensplit as $parenchunk) { + switch ($parenchunk) { case '(': $open++; $reargs .= $parenchunk; @@ -109,7 +135,7 @@ $reargs .= $parenchunk; break; default: - if ($open > 0){ + if ($open > 0) { // if we're inside a set of parenthasis, "protect" the commas $parenchunk = str_replace(',', '^%^%^', $parenchunk); } @@ -120,7 +146,7 @@ // make args into an array $args = explode(',', $reargs); $trimedargs = array(); - foreach((array)$args as $arg){ + foreach ((array)$args as $arg) { // trim out white space // and put commas back in $trimedargs[] = trim(str_replace('^%^%^', ',', $arg)); @@ -132,13 +158,13 @@ unset($a['note']); //var_dump($a); - if (!$a['na']){ + if (!$a['na']) { $form['#type'] = "'". $a['#type'] ."'"; unset($a['na']); unset($a['#type']); ksort($a); - foreach($a as $key => $value) { - if (isset($args[$key])){ + foreach ($a as $key => $value) { + if (isset($args[$key])) { $form[$value] = $args[$key]; } } @@ -156,7 +182,7 @@ function formupdater_get_argnames($type) { $a = array(); - switch($type){ + switch ($type) { case '': // no type = form(); $a = array(1 => '#method', 2 => '#action', 3 => '#attributes'); @@ -252,12 +278,9 @@ default: $a['na'] = true; } - if (!isset($a['#type'])){ + if (!isset($a['#type'])) { $a['#type'] = $type; } $a['na'] = $a['na'] ? TRUE : FALSE; return $a; } - - -?> \ No newline at end of file