2

I have requirement in that I want to show a dropdown values. So to achieve this I created a form function with select type and returning the form. I want to display it in a html page. Here is what I have tried:

function cart_confirmation_form($form, &$formstate){

  $opt = array('Hi','Bye'); 
  $form['dropdown'] = array(
    '#title' => 'somethinfg',
    '#type' => 'select',
    '#options' => $opt,
  );
  return $form;
}
   function testing(){
     $message .= '<div class="popup-related-tabs" style="float: left;" >' . cart_confirmation_form($form, $formstate) . '</div>';
      $message .= '<div class="popup-related-titles" style="padding:15px;" id="popup-related-block">'.$form_message.'</div>';
    drupal_set_message('<div class="message-inner" style="padding:15px;">' . $message . '</div>' );

}

This is not working, It says array to string conversion, form and formstate is undefined something like this. I know this is not correct way please suggest me the correct way to print values in dropdown.

2 Answers 2

3

Assuming testing() is a page callback, you want this:

function testing()
{
  $form = drupal_get_form('cart_confirmation_form')
  $output = '<div class="popup-related-tabs" style="float: left;" >' . render($form) . '</div>';

  return $output;
}

Though personally I'd built it as a render array:

function testing()
{
  $page = array
  (
    '#prefix' => '<div class="popup-related-tabs" style="float: left;" >',
    '#suffix' => '</div>',
    'form' => drupal_get_form('cart_confirmation_form')
  );

  return $page;
}
0

Above answer also true but you can also do very easily using drupal_render like this

function testing() { $output = drupal_render(drupal_get_form('cart_confirmation_form')); return $output; }

1
  • Note that calling drupal_render() around drupal_get_form() will throw a PHP warning. The value passed to drupal_render() is supposed to be a variable, not the output of a function, as the value passed to drupal_render() is a reference. This is why I declared $form first in my answer, to prevent this PHP warning.
    – Jaypan
    Sep 19, 2016 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.