0

In D7 I'm creating a form for a custom module. I would like one of the form fields to provide a wysiwyg editor (these work and render properly elsewhere around the site). In my custom form though, the text_format field doesn't render. If I change the type to 'textarea' it shows up, but as text_format, it's just non-existent in the rendered output.

My code is like this:

function mymodule_menu() {
  $items = array();
  $items['admin/mymodule'] = array(
    'title' => 'My Module Form',
    'page callback' => 'mymodule_form',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function mymodule_form($form_state) {
  $form = array(
    'field1' => array(
        '#type' => 'textfield',
        '#title' => t('Field 1'),
        '$required' => true,
    ),
    'field2' => array(
        '#type' => 'text_format',
        '#title' => 'Filed 2',
        '#default_value' => "",
        '#format' => 'filtered_html',
    ),
  );   
  return $form;
} 

Can anyone spot a problem with this? What else should I be looking for to understand why the field doesn't render when the type is 'text_format'?

1 Answer 1

0

Well I finally figured this one out. Hopefully this helps someone else out there:

function mymodule_menu() {
  $items = array();
  $items['admin/mymodule'] = array(
    'title' => 'My Module Form',
    'page callback' => 'mymodule_callback',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function mymodule_callback() {
  $form = drupal_get_form('mymodule_form');
  return $form;
}

function mymodule_form($form_state) {
  $form = array(
    'field1' => array(
      '#type' => 'textfield',
      '#title' => t('Field 1'),
      '#required' => true,
    ),
    'field2' => array(
      '#type' => 'text_format',
      '#title' => 'Filed 2',
      '#default_value' => "",
      '#format' => 'filtered_html',
    ),
  );   
  return $form;
} 

For whatever reason, having the menu callback not point directly to the form builder function, but instead at an intermediary function that calls drupal_get_form() caused the text_format type to work properly.

1
  • 1
    your menu entry should have been page callback => 'drupal_get_form', page arguments => array('mymodule_form'). May 16, 2014 at 14:52

Your Answer

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

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