Question

It seems like such a simple question, and I apologize for my Drupal ignorance, but I am unable to get our Drupal contractors to accomplish what seems to be a simple request. I need the below script to work without paying for a month of contracting work:

The user will be logged in, and of course, there is a valid database. If it matters, the site is hosted by "Pantheon", and the script will also call a custom Drupal function to return a recordset.

Is this code not possible? Am I asking something that cannot be done ind Drupal thus the endless stream of complicated files that they keep giving me?

root\myscript\index.php (to run as www.mysite.com\myscript\index.php)

<?php
/* Check for Drupal Instance and then bootstrap to get User and Database connection */
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $db_url;
$creds      =  parse_url($db_url);
$db_host    = isset($creds['host'])     ? $creds['host']    : null ;
$db_user    = isset($creds['user'])     ? $creds['user']    : null ;
$db_pass    = isset($creds['pass'])     ? $creds['pass']    : null ;
$db_name    = isset($creds['db_name'])  ? $creds['db_name'] : null ;

$db = mysqli_connect ($db_host, $db_user, $db_pass, $db_name);
if(!$db){
    die('Failed! Could not open a db connection'.mysqli_connect_errno() . mysqli_connect_error());
}elseif(!$user){
    echo 'Failed! Connected to database, but did not get $user - we canNOT proceed';
    echo '<pre>'.print_r($db,true).'</pre>';
    die('Connected to database, but did not get $user')    ;
}else{
    echo "Success! User and DB are set and ready to use - we can proceed";
    echo '<pre>'.print_r($user,true).'</pre>';
    echo '<pre>'.print_r($db,true).'</pre>';
}

This (I think) is at least the start of the Drupal function that will return a recordset when the main code above is working.

/**
* Implements hook_block_info().
*
*/
function xyz_listofmyitems_block_info() {
    $blocks['xyz_listofmyitems'] = array(
        // The name that will appear in the block list.
        'info' => t('List of available items'),
        // Default setting.
        'cache' => DRUPAL_CACHE_PER_ROLE,
    );
    return $blocks;
}
Was it helpful?

Solution

To answer your question - Yes, it is possible to have an external script to bootstrap Drupal.

If Drupal is installed in the folder [root], and the index.php file that you want to get working is in [root/myscript], you'd first get the working directory and then call bootstrap.inc and bootstrap Drupal.

<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/../includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// do your thing
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top