0

I have Drupal 7 site which is working fine. I added a custom PHP page to my site in the root directory of it. I want to extend the Drupal application functionality to that one page so that I can have access to the different Drupal classes and basically extend Drupal through my page. What includes do I need at the top of my page to be able to access the Drupal application as if I were within the Drupal site?

6

The only file you need to include is '/includes/bootstrap.inc', but you also need to define the DRUPAL_ROOT constant and run drupal_bootstrap() to have access to the API/database/etc.:

define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Your code here

It's exactly the same code that you'll find in the standard index.php file, minus a call to menu_execute_active_handler() which isn't necessary if you dont want to build and output a normal Drupal page.

  • Excellent! This is exactly what I was looking for. Thank you very much!! Much appreciated. – hanleyhansen Jun 28 '12 at 20:41
  • Hi Clive & hanleyhansen. Please see he optional answer I just provided as I feel the approach in the question and the answer Clive provided are if not wrong, a bit bad design which is definitely ok in a small project, but in larger codebases a definite anti-pattern. Thanks! – Tommi Forsström Nov 4 '12 at 17:54
6

Even though Clive's answer answers your question correctly, I'd still like to present the point of view that while what you're doing is not wrong per se, it's not a very good pattern as all custom code in Drupal is designed to reside under sites/all.

The correct way to go about this is to have a custom module, that implements the horribly misleadingly named hook_menu -hook.

I'll omit the details of creating and enabling a custom module as there's plenty of references out there for that, but this is what you need to do in your module that I'll call custommodule :

function custommodule_menu(){
  return array(
    'path_you_want' => array(
      'page callback' => 'custommodule_callback',
      'access callback' => TRUE
    )
  );
}

function custommodule_callback(){
  // do whatever you want to do and return what you want rendered on the page content area.
  return "Hello world from my custom callback";
}

This is pretty much the bare minimun example and just hook_menu has enough options to tweak access, url parameter passing, cache handling etc to take care of most any scenario necessary.

Yes, this is a couple of steps more work than what you were doing, but is far cleaner, more future proof and keeps all code where it's supposed to be. It's also quite a bit better aligned with how Drupal expects custom stuff to interact with it, so your page won't break if Drupal changes the way it bootstraps for some reason.

  • A question I have on this is, do we have to manually also create the page to be available at url 'path_you_want' ? or does adding it to the hook_menu makes it to be available on that link? – pal4life Jan 14 '15 at 18:35

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.