0

I'm currently working on a drupal module and I want to print a specific form called by a shortcode. So I've use this tuto and here is my code :

Info :

    function custom_shortcodes_shortcode_info() {
      $shortcodes['form'] = array(
        'title' => t('Link form'), 
        'description' => t('A simple form.'), 
        'process callback' => 'custom_shortcodes_shortcode_form',
      );
      return $shortcodes;
    }

Callback:

    function custom_shortcodes_shortcode_form($attrs, $text) {
      $attrs = drupal_get_form('custom_shortcodes_shortcode_form_attributes');
      return $attrs;
    }

Form :

    function custom_shortcodes_shortcode_form_attributes() {
      $form = array();
      $form['PrjLogement'] = array(
        '#type' => 'textfield',
        '#title' => t('Field1'),
        '#required' => TRUE
      );

      $form['MenageRevenuM'] = array(
        '#type' => 'textfield',
        '#title' => t('Field2'),
        '#required' => TRUE
      );

      $form['MenageApport'] = array(
        '#type' => 'textfield',
        '#title' => t('Field3'),
        '#required' => TRUE
      );

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

      return $form;
    }

Now the thing is that every time I'm calling the form with the specific shortcode the output is just "Array". Is there any way to print this form ? Thanks in advance for your help.

0

Basically, drupal_get_form doesn't render the array - it just returns the renderable array from the form function. You need to render it yourself.

Try this:

function custom_shortcodes_shortcode_form($attrs, $text) {
  $form = drupal_get_form('custom_shortcodes_shortcode_form_attributes');
  $attrs = drupal_render($form);
  return $attrs;
}
  • Thanks for your help, I've tried this, its better : I see my form but it's not rendered as a form but more like a text. Do you know how to see it as a form ? – Barbrousse Dec 4 '15 at 20:09
  • Drupal might be removing tags from the shortcode. Make sure that shortcodes is under 'Limit Allowed Html Tags' in your Filter Processing Order. – EmGee Dec 4 '15 at 20:38
  • Thanks you very much, that was the issue, it's working great now. – Barbrousse Dec 4 '15 at 20:57

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.