hi guys

i,ve searched around but couldnt find out how to accomplish this with D7 in D6 i had


function mymodule_form_award ($form, &$form_state) {
  
  $checkboxes = array();
   //changed below to meet D7 way   
  $rows = "SELECT * FROM awards WHERE awardtype='small'"; // this is a typical query from the node table to get some info about nodes
  $result = db_query($rows);
  foreach($result as $row)// = db_fetch_array($rows)) // we loop through the results
  {
    $checkboxes[$row->AWARD_ID] = ''; // a blank value is given so that the checkboxes have no title and are just rendered as a blank checkbox
    $form['DESCRIPTION_display'][$row->AWARD_ID] = array('#value' => $row->DESCRIPTION); 
    $form['DESCRIPTION_' . $row->AWARD_ID] = array ("#type" => "value", "#value" => $row->DESCRIPTION);
    $form['award_imag_display'][$row->AWARD_ID] = array ("#value" => $row->award_imag);
    $form['award_imag_' . $row->AWARD_ID] = array ("#type" => "value", "#value" => $row->award_imag);
    $form['reason'][$row->AWARD_ID] = array('#type' => 'textfield', '#required' => True, '#maxlength' => 255, '#size' => 100, '#value' => $row->reason,'#default_value' => '');
  };
  
$form['date'] = array(
    '#title' => t('Date of award'),
    '#type' => 'date',
    '#description' => t('Enter the Date of awardl'),
    '#required' => TRUE,
    '#default_value' => array(
    'year' => format_date(time(), 'custom', 'Y'),
    'month' => format_date(time(), 'custom', 'n'),
    'day' => format_date(time(), 'custom', 'j')
)
);
 
 $form['user']['feed_item_length']= array (
                '#title' => t ( 'Assign to User'),
                '#type' => 'select',
                 '#required' => TRUE,
                '#default_value' => variable_get('feed_item_length','teaser'),
                '#options' => array(
                 'teaser'=> t('--------'),
                 ''=>_users(),
                )
       );

  

$form['checkboxes'] = array
  (
    '#type' => 'checkboxes',
    '#options' => $checkboxes,
    '#required' => True,
  );


$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add AWARD'),
  );


$form['#theme'] = 'mymodule_theme';
return $form;

}

function mymodule_theme()
{
  return array
  (
    'mymodule_theme' => array
    (
      'arguments' => array('form' => NULL),
    ),
  );
}

function theme_mymodule_theme($form)
{

$has_posts = isset($form['DESCRIPTION_display']) && is_array($form['DESCRIPTION_display']);
$select_header = $has_posts ? theme('table_select_header_cell') : '';
$header = array(theme($select_header) , t('Description'), t('IMAGE'), t('Default Reason')); 
$output = '';
  if ($has_posts)
  {
$rows = array();

  foreach(element_children($form['checkboxes']) as $award_id)
  {
    $row = array(); 
    $row[] = drupal_render($form['checkboxes'][$award_id]);
    $row[] = drupal_render($form['DESCRIPTION_display'][$award_id]);
    $row[] = drupal_render($form['award_imag_display'][$award_id]);
    $row[] = drupal_render($form['reason'][$award_id]);
    $rows[] = $row;

  }
  }
 
  else
  {
    $header = array(t(''), t(''));
    $row = array();
    $row[] = array
    (
      'data' => t('No awards were found'),
      'colspan' => 2,
      'style' => 'text-align:center',
    );
    $rows[] = $row;
  }
  
  $output .= theme('table', $header, $rows); // this builds our table
  $output .= drupal_render($form); //this calls some last rendering functions on the $form from the form definition in the original function
  return $output;


}

and i get

Notice: Undefined index: render element in theme()

can any one point me where i went wrong please

Comments

maori’s picture

ok
seems theres another problem aswell :(

if i rem out $form['#theme'] = 'mymodule_theme';

i get this error

Notice: Undefined property: stdClass::$AWARD_ID

which comes from

$checkboxes[$row->AWARD_ID] = '';

well aculy i get a simmiler error for the

Notice: Undefined property: stdClass::$DESCRIPTION

aswell

maori’s picture

ok fixed the

Notice: Undefined property: stdClass::$AWARD_ID

problems seems the code made them lower case :D

but still getting the orginal problem of

Notice: Undefined index: render element in theme()

and also i get

Fatal error: Maximum function nesting level of '100' reached, aborting!

maori’s picture

solved the

Fatal error: Maximum function nesting level of '100' reached, aborting!

problem D7 changed the render part

ie

//D6 way 
$output .= drupal_render($form); 

//now in D7 way solved the Fatal error: Maximum function nesting level of '100' reached, aborting!
$output .= drupal_render_children($form); 

and that gives me no more errors however the whole sites theme is messed up and everything is displayed down the left side :(

looking here http://drupal.org/update/modules/6/7#element_theme_properties

helped solve the problem however i cant really understand about the #theme_wrappers ie how to use them to solve my issue

maori’s picture

ok

and that gives me no more errors however the whole sites theme is messed up and everything is displayed down the left side :(

i think i solved that issue changed

function mymodule_theme() {
  return array
  (
    'mymodule_theme' => array
    (
      'arguments' => array('form' => NULL),
    ),
  );
}

to

function mymodule_theme() {
  return array
  (
    'mymodule_theme' => array
    (
      'variables' => array('form' => NULL),
    ),
  );
}

and it looks like it sorted out the problem of everything being shown on the left hand side ie sites main theme is left intact but it just steps over the foreach statement in the
function theme_mymodule_theme($form)

so nothing is displayed on the page :(

i see in netbeans that $form is showing as NULL so that explains why but not how as stepping though the form function it all goes as it should and if i rem out the $form['#theme'] = 'mymodule_theme';

it displays all the form elements and the txt pulled from the database so something is up with my theme sections but i'am lost :(

any hints ??

maori’s picture

okidoki

i got it displaying now :) heres what i have so far



function mymodule_form_awards () {

$checkboxes = array();

  $rows = "SELECT * FROM {award_types} WHERE awardtype='award'";
 $result = db_query($rows);
  foreach ($result as $row) {
    $checkboxes[$row->award_id] = '';
    $form[$row->award_id]['DESCRIPTION_display'] = array('#markup' => $row->description);
    $form['DESCRIPTION_' . $row->award_id] = array("#type" => "value", "#markup" => $row->description);
    $form[$row->award_id]['award_imag_display'] = array("#markup" => $row->award_imag);
    $form['award_imag_' . $row->award_id] = array("#type" => "value", "#markup" => $row->award_imag);
   $form[$row->award_id]['reason_display'] = array('#type' => 'textfield','#required' => True, '#value' => $row->reason);
   $form['reason_' . $row->award_id] = array("#type" => "value", '#required' => True, '#value' => $row->reason);
  };

$form['date'] = array(
    '#title' => t('Date of award'),
    '#type' => 'date',
    '#description' => t('Enter the Date of award'),
    '#required' => TRUE,
    '#default_value' => array(
    'year' => format_date(REQUEST_TIME, 'custom', 'Y'),
    'month' => format_date(REQUEST_TIME, 'custom', 'n'),
    'day' => format_date(REQUEST_TIME, 'custom', 'j')
)
);

 $form['user']['feed_item_length']= array (
                '#title' => t ( 'Assign to User'),
                '#type' => 'select',
                 '#required' => TRUE,
                '#default_value' => variable_get('feed_item_length','teaser'),
                '#options' => array(
                 'teaser'=> t('--------'),
                 ''=>_getUsers(),
                )
       );



$form['checkboxes'] = array
  (
    '#type' => 'checkboxes',
    '#options' => $checkboxes,
    '#required' => True,
  );


$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add AWARD'),
  );


$form['#theme'] = 'mymodule_theme';
return $form;
}


function mymodule_theme() {
  return array
  (
    'mymodule_theme' => array
    (
     'arguments' => array('form' => NULL),
     'render element' => 'form',

    ),

  );
}

function theme_mymodule_theme($variables) {

  $form = $variables['form'];
  $rows = array();
  foreach(element_children($form['checkboxes']) as $award)
  {
    $row = array();
    $row[] = drupal_render($form['checkboxes'][$award]);
    $row[] = drupal_render($form[$award]['DESCRIPTION_display']);
    $row[] = drupal_render($form[$award]['award_imag_display']);
    $row[] = drupal_render($form[$award]['reason_display']);
    $rows[] = $row;
  }


  if(count($rows)) {
    $header = array(theme('data') , t('Description'), t('Image'), t('Default Reason'));
  }

  else {
    $header = array(t('Description'), t('Image'), t('Default Reason'));
    $row = array();
    $row[] = array
    (
      'data' => t('No Awards were found'),
      'colspan' => 2,
      'style' => 'text-align:center'
    );
    $rows[] = $row;
  }



    $output = theme('table', array('header' => $header, 'rows' => $rows));

   return $output . drupal_render_children($form);


}

shreyas shetty’s picture

yes this works

'arguments' => array('form' => NULL),this line is not required as the
'render element' => 'form', suplies the required form element to your theme

Gekiboy’s picture

drupal_render_children() is a function I needed to find. I was upgrading my module to Drupal 7 and when I called drupal_render() in my theme function I was receiving a PHP memory exceeded error.

emackn’s picture

How are you calling your function from the template file?

print theme('your_theme', drupal_get_form('my_form'));
or
print drupal_render(drupal_get_form('my_form'))

roam2345’s picture

removed...

kkatusic’s picture

use:

drupal_render(drupal_get_form('my_form'));
dolilmao’s picture

Here is another

function theme_bookmark_author_percentage_role($form) {
  $output = '';
  $elements = element_children($form);
  if (!empty($elements)) {
    $header = array(t('Role'), t('Percentage'));
    foreach ($elements as $key) {
      $row = array();
      $row[] = $form[$key]['#title'];
      unset($form[$key]['#title']);
      $row[] = drupal_render($form[$key]);
      $rows[] = $row;
    }
    $output = theme('table', array('header' => $header, 'rows' => $rows));
  }

  $output .= drupal_render_children($form);
  return $output;
}

But i get the error
Notice: Undefined index: render element in theme() (line 835 of C:\xampp\htdocs\drupal\includes\theme.inc).
Notice: Undefined index: #title in theme_bookmark_author_percentage_role()

roynilanjan’s picture

Templating/Theming form should be in the way,

  function hook_theme() {
    return [
      'mytheme' => [
        'render element' => 'form',
        'template' => 'sometemplate',
      ],
    ]
  }

/**
  * Form callback
  */
funtion mytheme($form, $form_state) {
  // Here is the form structure
  return $form;
}

/**
  * Implements template_preprocess_HOOK.
  */
function template_preprocess_mytheme(&$variables) {
  // Push required elements to form templates from $variables
}

Instead of using the theme key(#theme) directly in form