2

I am writing my own PHP program on root or inside a module which is suppose to return the registration form. The content of the abc.php program is:

<?php
  $form1=drupal_get_form('user_register_form');
  $output1 = drupal_render($form1);
  echo $output1;
?>

this is called by a jQuery function actually and the result is to be inserted in a popup DIV. But it gives me the the error:

undefind function drupal_get_form.

What do I need to include in the file so this error disappears?

thanks in advance

2

The answer depends on where you put the PHP file. Generally speaking, in Drupal you don't create a file that bootstrap Drupal, but you normally create a module or a theme, which is invoked by Drupal after it has been bootstrapped, or when the boot is still not completed (that is the case of the hook_boot() implementations).

  • Inside the directory of a module: As you have a module, all you have to do is be sure the file is included when the module is running. That is something Drupal automatically do, if the hook_menu() implementation is similar to the following one (it is the actual implementation made from the Book module). When, the users access "admin/content/book/settings," Drupal loads the book.admin.inc contained in the directory containing the book.module file.
function book_menu() {
  $items['admin/content/book'] = array(
    'title' => 'Books', 
    'description' => "Manage your site's book outlines.", 
    'page callback' => 'book_admin_overview', 
    'access arguments' => array('administer book outlines'), 
    'type' => MENU_LOCAL_TASK, 
    'file' => 'book.admin.inc',
  );
  $items['admin/content/book/list'] = array(
    'title' => 'List', 
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/content/book/settings'] = array(
    'title' => 'Settings', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('book_admin_settings'), 
    'access arguments' => array('administer site configuration'), 
    'type' => MENU_LOCAL_TASK, 
    'weight' => 8, 
    'file' => 'book.admin.inc',
  );

  // …
}
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
echo drupal_render(drupal_get_form('user_register_form'));

There are few cases where creating a file similar to cron.php or update.php is really necessary, such as when you create a soap.php file similar to xmlrpc.php, but for SOAP requests. For cases like yours, you just need to create a module, and implement the necessary hooks.

2

You should not write code in stand alone php files and call them (Unless you know what you are doing and do it for performance reason, to avoid Drupal bootstrap on a pure external web service call could be one such example).

What you should do instead is to write a module. Use hook_menu to define the path that you want to listen for, and create a callback function that will handle incoming requests. Doing it like this will allow you to actually use the Drupal APIs.

You can lookup the hooks and functions you need to use at the Drupal API reference site

0

To get started check out http://drupal.org/developing/modules which walks you through developing modules in Drupal or check out http://drupal.org/project/examples that shows you how to use the Drupal API.

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.