0

I have implemented a hook_form in my custom module and I can't figure out how I can get the form to display.

Here is my hook form:

   function data_retrieval_form($node, &$form_state)
    {
        $type = node_type_get_type($node);

        $form ['title'] = array(
            '#type' => 'textfield',
            '#title' => check_plain($type->title_label),
            '#default_value' => !empty($node->title) ? $node->title : '',
            '#required' => TRUE, '#weight' => -5
        );

        $form ['field1'] = array(
            '#type' => 'textfield',
            '#title' => t('Custom field'),
            '#default_value' => $node->field1,
            '#maxlength' => 127,
        );
        $form ['selectbox'] = array(
            '#type' => 'select',
            '#title' => t('Select box'),
            '#default_value' => $node->selectbox,
            '#options' => array(
                1 => 'Option A',
                2 => 'Option B',
                3 => 'Option C',
            ),
            '#description' => t('Choose an option.'),
        );

        return $form;
    }

I have also implemented hook_menu which registers a url with a callback to the form:

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

    $items ['examples/form_example'] = array(
        'title' => 'Example Page',
        'page callback' => 'drupal_get_form',
        'access arguments' => array('access content'),
        'type' => MENU_SUGGESTED_ITEM,
    );

    return $items;
}

The problem I am having is that I cant understand how to get the form to display, the url does not work, how exactly do I display the form?

2 Answers 2

0

It doesn't work because you set the page callback, but not the arguments to pass to that function. Without that, it will not know which form you want to render, nor which form builder should be called.

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

    $items ['examples/form_example'] = array(
        'title' => 'Example Page',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('your_form_builder'),
        'access callback' => 'user_permission',
        'access arguments' => array('access content'),
        'type' => MENU_SUGGESTED_ITEM,
    );

    return $items;
}

The other error that you are doing is not implementing all the hooks that Drupal requires from a module implementing a content type: hook_form() is just one of them, but you need other hooks. See Node API hooks for a list of all the hooks you need. Mostly, you need hook_node_info().

If you are not implementing a content type, but you want to simply show a form, then what you need is a form builder, which is not an implementation of hook_form(). Probably you need something similar to this.

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

    $items ['examples/form_example'] = array(
        'title' => 'Example Page',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('data_retrieval_form'),
        'access callback' => 'user_permission',
        'access arguments' => array('access content'),
        'type' => MENU_SUGGESTED_ITEM,
    );

    return $items;
}

function data_retrieval_form($form, &$form_state) {
  //...
  return $form;
}
3
  • Then how do I access my form in the URL? Sep 1, 2015 at 8:07
  • Are you defining a new content type, in your module, or do you want just to show a form?
    – apaderno
    Sep 1, 2015 at 9:02
  • I've managed to display it now, the URL was localhost.com/drupal/?q=example/form_example and I was trying to access it with a clean URL like localhost/drupal/example/form_example Sep 1, 2015 at 9:07
0

Please check hook_menu() documentation.

You need to specify page callback arguments like:

'title' => 'Example Page',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_form_id'),
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,

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.