0

I would like to create a PHP page that executes Drupal API functions.

The page would be inside my Drupal 7 website, perhaps the theme directory, but I'm not sure where it should reside.

I would like this page to run a query to the database using the Drupal's database abstraction layer. Once the query is retrieved, I would like to run some other D7 API functions and finally print out a page.

The page should not be one of the default Drupal templates, but a new page, that simply has one single within it.

How can I achieve this?

EDIT: The correct way to do this is to implement hook_menu() inside a module. I'm trying to do this, but my approach doesn't work. This is what I'm doing:

  • the profileBox.info file
name = PB
description = "Creates a callback to returnuid.tpl.php"
package = SDNN
core = 7.x
files[] = profileBox.module
  • the profileBox.module file is installed and has the following code:
function profileBox_menu() {
  $items['returnunid'] = array(
    'title'=>'Example'
    'description'=>'examples'
    'page callback' => 'profileBoxGetit',
    'access callback'=>TRUE,
  );

  return $items;
}

function profileBoxGetit() {
  $stringit = "the string output";

  return $stringit;
}
  • I created a returnuid.tpl.php file with some html code in it - just to test (not functional code)
  • I then pointed my browser to [root]/?q=returnuid
  • I expected to get the content of the $stringit variable, but I instead got an "access denied" error

This module actually messes up the Administration menu, which dissapears when the module is installed.

What am I doing wrong?

  • Also a few other hints: 1. You do not need to add the .module file to files[], only files which contain php classes. 2. Use a lower case name for your module, e.g. "profilebox". Otherwise, update_N hooks will not work. – Berdir Jun 3 '11 at 8:53
  • 1
    You should not update your question too add further issue while dealing with the early answer. Ask new questions instead. Otherwise, it makes the original questions and answer useless to others. – Pierre Buyle Jun 3 '11 at 9:48
  • I've deleted my answer because it answered only the original question, and it was a really bad idea :) Glad to see you've rephrased this. – tim.plunkett Jun 3 '11 at 12:59
4

The correct way to "create a PHP page that executes Drupal API functions" is to create a module.

In that module, you implement hook_menu() which allows you to tell Drupal that it should call a given php function for a specific path, for example 'yourmodule/somepath'. Inside that, you can then execute whatever PHP and Drupal API functions which you want.

See http://drupal.org/node/1074360 for more information.

|improve this answer|||||
  • you're right, the correct way should be hook_menu(). can you take a look at my edit above? – Carlos Muñiz Jun 3 '11 at 8:45
  • 1
    Your hook menu implementation is incomplete. You need to either set a permission with 'access arguments' => array('permission name') (which you can define with hook_permission() if you want your own) or set 'access callback' => TRUE to allow access to everyone. Also, you need to define a title or set the type to MENU_CALLBACK if you don't want to have it visible in the menu. – Berdir Jun 3 '11 at 8:50
  • yes. this work, but it makes my administration menu dissapear. any idea why? – Carlos Muñiz Jun 3 '11 at 9:37
  • 1
    When you say "Administration menu", do you mean the black bar provided by drupal.org/project/admin_menu? If not: what do you mean? If yes: check if your page has valid HTML code and if there are any javascript errors on the page. – marcvangend Jun 3 '11 at 9:46
  • I'm back, I had to get some sleep :) No, the admin menu provided by Drupal works fine. What I mean is the contrib module called administration menu (drupal.org/project/admin_menu). This menu dissapears when my module is on. I checked all the code and it seems ok. any ideas? – Carlos Muñiz Jun 3 '11 at 13:55
2

Another approach is to create standalone file, just like index.php, and perform drupal bootstrap there. For example:

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
/**
Write your code here
use PHP core, drupal core and contrib functions
**/
|improve this answer|||||
  • There are reasons to do this, but in most cases, this isn't a good idea. You should extend Drupal with modules, not standalone files. A possible exception for example is providing a faster callback for ajax calls and avoiding the full drupal bootstrap. – Berdir Aug 21 '11 at 21:07
  • I have some php files including the drupal_bootstrap. I wonder how to register these php files to Drupal using hook_menu. I want to add one of those files as a tab on the user page. – Елин Й. Mar 5 '13 at 12:58

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.