Recently I had to build this. It’s a simple little app that uses the forms API. Think w3schools.com “try it yourself.”

  1. function sg_games_menu(){
  2.  
  3.         $items[‘html-sandbox’] = array(
  4.              ‘title’ => t(‘HTML sandbox’),
  5.              ‘page callback’ => ‘sg_games_html’,
  6.              ‘access arguments’ => array(‘access content’),
  7.              ‘type’ => PAGE_CALLBACK,
  8.         );
  9.  
  10.         return $items;
  11. }
  12.  
  13. function sg_games_html(){
  14.         $output = drupal_get_form(‘sg_games_html_form’);
  15.  
  16.         return $output;
  17. }
  18. function sg_games_html_form( $form_state ){
  19.  
  20.         if ( !$output = $form_state[‘storage’][‘values’][‘editor’] )
  21.                 $output =
  22. <h1>Try entering some html!</h1>
  23. ;
  24.  
  25.         $form[‘editor’] = array(
  26.           ‘#type’ => ‘textarea’,
  27.           ‘#title’ => t(‘Enter your HTML code here’),
  28.           ‘#default_value’ => $output
  29.         );
  30.  
  31.         $form[‘submit’] = array(
  32.           ‘#type’ => ‘submit’,
  33.           ‘#value’ => ‘Try it!’
  34.         );
  35.  
  36.         $form[‘output’] = array(
  37.           ‘#value’ => $output,
  38.           ‘#prefix’ =>
  39. <div id="sg-games-output">’,
  40.           ‘#suffix’ => ‘</div>
  41. ,
  42.         );
  43.  
  44.         return $form;
  45. }
  46.  
  47. function sg_games_html_form_submit($form, &amp;$form_state) {
  48.         $form_state[‘storage’][‘values’] = $form_state[‘values’];
  49.         $form_state[‘rebuild’] = TRUE;
  50. }

All it really needs to do is hand off the form_state back into storage, rebuild the same form, and use part of the storage array as the default value of the editor field.

This one is really going to send shockwaves through the Drupal community :)

Leave a Reply