1

I'm using Drupal 7 and I have made a custom module in which there is this function.

    function product_search_by_pin_form($form, &$form_state) {
           // Provide a text field.
           $form['pin'] = array(
           '#method'=> 'POST',
           '#action' => '$come_back',
           '#title' => t('Input your PIN'),
           '#type' => 'textfield',
           '#attributes' => array('size' => '20', 'maxlength' => '20'),
           '#placeholder' => 'Type your PIN',
           '#required' => TRUE,);

           // Provide a submit button.
           $form['submit'] = array(
           '#type' => 'image_button',
           '#required' => TRUE,
           '#src' => "/images/upload/search.png",
           '#attributes' => array('id' => 'submit'),
           '#value' => 'Search',);

           return $form;
    } // END of Function

The form is being used from the following code.

    $arr = drupal_get_form('product_search_by_pin_form');
    print drupal_render($arr);

The problem is that Drupal displays the Array instead of showing the form.

How can I solve it?

EDIT As asked, and after have followed suggestions but without result, please here the code of the module

<?php
/**
 * @file
 * a test file
 */

/**
 * Implements hook_menu().
 */


function myform_menu() {
  $items = array();

  $items['/examples/myform-examples'] = array(
    'title' => 'Example myform',
    'description' => 'Configuration for TEST module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('product_search_by_pin_form'),
    'access callback' => TRUE
  );

  return $items;
}


/**
 * Implements hook_help().
 *
 * Displays help and module information.
 *
 * @param path 
 *   Which path of the site we're using to display help
 * @param arg 
 *   Array that holds the current path as returned from arg() function
 */

function myform_help($path, $arg) {
  switch ($path) {
    case "admin/help#myform":
      return '' . t("<b>Myform</b> to test a drupal form") . '';
      break;
  }
}


function product_search_by_pin_form($form, &$form_state) {
       // Provide a text field.
       $form['pin'] = array(
       '#method'=> 'POST',
       '#action' => '$come_back',
       '#title' => t('Input your PIN'),
       '#type' => 'textfield',
       '#attributes' => array('size' => '20', 'maxlength' => '20'),
       '#placeholder' => 'Type your PIN',
       '#required' => TRUE,);

       // Provide a submit button.
       $form['submit'] = array(
       '#type' => 'image_button',
       '#required' => TRUE,
       '#src' => "/images/upload/search.png",
       '#attributes' => array('id' => 'submit'),
       '#value' => 'Search',);

       return drupal_get_form($form);
} // END of Function
  • Where are you exactly printing that? It is randomly in your .module file? – Neograph734 Mar 26 '17 at 16:05
  • in this moment I include this code into a node. Once I see it working I will put it into the .module file – Tormy Van Cool Mar 26 '17 at 16:18
  • 1
    Please do it properly from a .module file so debugging is effective. – Kevin Mar 26 '17 at 17:06
  • 1
    As an aside, textfields don't have action or method properties. Those are for the form only. Submit buttons can't be required either – Clive Mar 26 '17 at 17:10
  • 1
    @zhilevan done. It works. I didn't understand your point about Drupal_get_form(). That's why I persisted on my error. Second solution, now, works – Tormy Van Cool Mar 28 '17 at 11:42
1

First try Render instead of drupal_render

$arr = drupal_get_form('product_search_by_pin_form');
print render($arr);

From Clive answer on what is the difference between render() and drupal_render()?

The docs for render() explain it pretty well:

This function renders an element using drupal_render(). The top level element is shown with show() before rendering, so it will always be rendered even if hide() had been previously used on it. render() is basically just a wrapper for drupal_render(), except that it makes sure the element passed in is set to be shown when passed through to drupal_render().

It also makes sure the element passed in is an array (which is all drupal_render() can accept). If it isn't, the element is return back as-is. So it provides a bit of a safety net if you like.

If for any reason you have a variable you need to render, and you don't know if it's a string or a render array, you can pass it straight through render() without having to test the variable type yourself.

and another solution is, use drupal_get_form as you call back function and pass your form to it as an argument. ( take a look at https://www.drupal.org/docs/7/howtos/how-to-make-a-simple-module-with-a-form-and-menu-link )

function YOURMODULE-NAME_menu() {
  $items = array();

  $items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Example Form', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('product_search_by_pin_form'), //put the name of the form here
    'access callback' => TRUE
  );

  return $items;
}

function product_search_by_pin_form($form, &$form_state) {
         // Provide a text field.
       $form['pin'] = array(
       '#method'=> 'POST',
       '#action' => '$come_back',
       '#title' => t('Input your PIN'),
       '#type' => 'textfield',
       '#attributes' => array('size' => '20', 'maxlength' => '20'),
       '#placeholder' => 'Type your PIN',
       '#required' => TRUE,);

       // Provide a submit button.
       $form['submit'] = array(
       '#type' => 'image_button',
       '#required' => TRUE,
       '#src' => "/images/upload/search.png",
       '#attributes' => array('id' => 'submit'),
       '#value' => 'Search',);

  return $form;
}

and third is another way of implementation second solution

function product_search_by_pin_form($form, &$form_state) {
       // Provide a text field.
       $form['pin'] = array(
       '#method'=> 'POST',
       '#action' => '$come_back',
       '#title' => t('Input your PIN'),
       '#type' => 'textfield',
       '#attributes' => array('size' => '20', 'maxlength' => '20'),
       '#placeholder' => 'Type your PIN',
       '#required' => TRUE,);

       // Provide a submit button.
       $form['submit'] = array(
       '#type' => 'image_button',
       '#required' => TRUE,
       '#src' => "/images/upload/search.png",
       '#attributes' => array('id' => 'submit'),
       '#value' => 'Search',);

       return drupal_get_form($form);
} // END of Function
  • also render returns just the word: Array – Tormy Van Cool Mar 27 '17 at 8:31
  • @TormyVanCool did you try all three solutions? – Yusef Mar 27 '17 at 8:50
  • You have got to get this out of a body field on a node. All three of these solutions should work. – Kevin Mar 27 '17 at 11:22
  • @zhilevan yes, the first returns "array", the second: doesn't work. There is not even a page at localhost/examples/form-example. As consequence also the 3rd doesn't work – Tormy Van Cool Mar 28 '17 at 10:02
  • @TormyVanCool second solution is the standard way of drupal, after any changes on any hook_menu you should clear the cache , and if you didn't any page on that url it because cache not cleared. please the cache and infrom me about the result. – Yusef Mar 28 '17 at 10:04
1

Don't return drupal_get_form() from your function. Just return the $form variable. Your menu item is already calling drupal_get_form(), you don't need to from your form builder.

When calling it programmatically, use the render() function to turn a form array to HTML output.

  • Kevin is correct - your problem lies with calling drupal_get_form() twice. You don't need render() or drupal_render(). – Jaypan Mar 28 '17 at 11:23
  • He updates the question according to my comment, in last comments I mentioned him to remove drupal_get_form from function and also remove / from the beginning of the route. routes not started with '/' in drupal 7. @Jaypan : read my last comment below the question. – Yusef Mar 28 '17 at 11:24
0

Instead of printing the form directly, You should be returning the form array. like

  $output['form'] = drupal_get_form('product_search_by_pin_form');
    return $output;

if you have to handle it seperately, You should be creating a template and print the form varibale in that temlate.

  • it returns always just the word "Array" – Tormy Van Cool Mar 27 '17 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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