Guys,

I need to update the Drupal navigation menu items without manually doing it or create a Drupal module. Is there a way to write custom code that lives outside the Drupal Framework that uses the Drupal Api?

I'm comfortable with Java so if there is a java solution please let me know but I'm trying to learn php right now.

Comments

gpk’s picture

require_once './includes/bootstrap.inc'; // assuming your script is in the same folder as Drupal's index.php
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // you now have access to all Drupal functionality

gpk
----
www.alexoria.co.uk

nevets’s picture

Note that only works if you are using PHP (Drupal does not have a java API)

mokargas’s picture

What if my file is outside the directory? Can't seem to get it to work

I have D5.8 in a 'cms' directory.

ie:

myfile.php
-- cms <--drupal's index.php and folders in here

I tried pointing directly to bootstrap, using:

require_once 'cms/includes/bootstrap.inc'; // assuming your script is in the same folder as Drupal's index.php
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // you now have access to all Drupal functionality

I get file not found errors. What could this be?

gpk’s picture

If you mean that cms/includes/bootstrap.inc is not found then try ./cms/includes/bootstrap.inc

Also if you want to have access to Drupal sessions from within your script then you will need to set $base_url in settings.php. This is because the session cookie name is a hash that depends on $base_path, and the latter will be come out different if Drupal is bootstrapped from a different folder location from usual.

gpk
----
www.alexoria.co.uk

mokargas’s picture

Thanks GPK. After trying that URL with no luck I discovered the problem was I didn't use CHDIR properly, works great now!

Mark Theunissen’s picture

You can use the bootstrap PHP code as above, or you can use Java to edit entries in the database directly.

Both options are quite technical though.

Hope that helps!

__________________________________________________________

Mark Theunissen

Code Baboon
Drupal based services

otoko6’s picture

i have little problem. so hear me. I learned how to use drupal codes outside drupal folder. Now there is
public_html/drupal and puclic_html/chat folders. Im trying to edit chat so i can log in with drupal name. This can be done by editing form of that chat. (where it ask my name for chat) so i have this code inside my chat's codes form's value="(here)"

so that (here) is code:

$drupal_dir = "(plaplapla)/drupal"; // wherever Drupal is
$current_dir = getcwd();
chdir($drupal_dir);

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

global $user;
if ($user->uid) {
print t('@name', array('@name' => $user->name));
}

but i get errors:

Warning: ini_set() [function.ini-set]: A session is active. You cannot change the session module's ini settings at this time in plaplapla/drupal/sites/default/settings.php on line 144

Warning: ini_set() [function.ini-set]: A session is active. You cannot change the session module's ini settings at this time in plaplapla/drupal/sites/default/settings.php on line 147

Fatal error: Cannot redeclare t() (previously declared in plaplapla/chat/chat.php:243) in plaplapla/drupal/includes/common.inc on line 947

settings.php line 144 is like this:
ini_set('session.save_handler', 'user');

settings.php line 147 is like this:
ini_set('session.use_trans_sid', 0);

chat.php:243 (if that means line) is:
function t($message)

and common.inc line is like this:
}

papile’s picture

The message tells you exactly what is wrong. Drupal has a t() function for translation. You are making another one in global namespace so it is not allowed. The session warnings are due to you having 2 bootstraps going. Could you not just put chat.php in module form and do it the drupal way?

otoko6’s picture

there is lots of php files and folders with xml things in chat folder so only thing i need is to make that drupal username to work outside drupal folder in that chat. can't put it inside module.

but it is possible if there is drupal module where you could put chat folder inside. (and there is many folders and files inside that chat folder) and then that chat can work. ?

papile’s picture

Another thing is why are you having to bootstrap Drupal just to get a user name? You could fetch the cookie/session user id if you are under the same domain to get this information and make web callbacks in your modules to expose APIs to get whatever info you need from Drupal.

If your task you are doing is heavily integrated with Drupal it should be integrated in module form. If it is loosely coupled it should be loosely coupled with web api callbacks. I cannot see any reason why you would want to bootstrap Drupal outside of Drupal unless you were doing something very temporary that is easier with the Drupal API such as importing a bunch of users etc.

otoko6’s picture

I need only user name for chat, because you don't need anything but name in that chat. I don't know much about codes and this chat is really cool and what i always have wanted. i came here to ask help for this single problem: putting drupal username inside that form in chat.php. and i think it can be done with that bootstrap thing?

otoko6’s picture

Here is my chat code where you can see drupalcode inside form's value:
http://www.text-upload.com/read.php?id=35989&c=1783350

what i have to change to get that thing work?

i mean why does that code work in chat/test.php but not in chat/chat.php??? those form codes must have something to do with it...

Maybe one solution to this problem would be this: i make file code.php and put that bootstrap code inside. Then when i go to code.php there is my drupal username. then there would be some code that sends that name to chat.php's value... or something.

crossmedia’s picture

I would like to discuss couple of options here

1- You can use the bootstrap PHP code

$drupal_dir = "(plaplapla)/drupal"; // wherever Drupal is 
$current_dir = getcwd(); 
chdir($drupal_dir); 
require_once './includes/bootstrap.inc'; 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 

2- Services Module - details you can check from this URL http://drupal.org/project/services
however in this case you need to install and enable services module and configure XMLRPC Server and provide access to view, update, delete and add contents through client.

3- Please correct me if i am wrong.

micheas’s picture

You need to set the following variable if you are bootstrapping drupal from the command line.

<?php
$_SERVER['REMOTE_ADDR'] = "localhost";
?>
<?php
$drupal_dir = "(plaplapla)/drupal"; // wherever Drupal is 
$current_dir = getcwd(); 
$_SERVER['REMOTE_ADDR'] = "localhost";
chdir($drupal_dir); 
require_once './includes/bootstrap.inc'; 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); ?>
aaustin’s picture

Not sure if this is a Drupal 7 thing or something else but I was getting the error:

PHP Fatal error: require_once(): Failed opening required 'DRUPAL_ROOT/includes/errors.inc' (include_path='.:/usr/share/php:/usr/share/pear') in (plaplapla)/drupal/includes/bootstrap.inc on line 2240

And needed to add "define('DRUPAL_ROOT', $drupal_dir);"

$drupal_dir = "(plaplapla)/drupal"; // wherever Drupal is 
$current_dir = getcwd(); 
chdir($drupal_dir); 
require_once './includes/bootstrap.inc'; 
define('DRUPAL_ROOT', $drupal_dir);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);