1

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;
}

1 Answer 1

1

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
?>
6
  • 1
    So, this is just common-sense website/php stuff, am I right - Drupal isn't presenting some complication to stop this from working?
    – GDP
    Commented Mar 8, 2016 at 20:47
  • if you bootstrap it right, then it shouldn't give you grief. So, yeah, I guess so.
    – oksana-c
    Commented Mar 8, 2016 at 20:51
  • My kingdom for a Drupal programmer who thinks like you....I felt like an idiot asking the question, but it's been like pulling teeth to get this done.
    – GDP
    Commented Mar 8, 2016 at 20:51
  • I would recommend using PDO for calling DB though. drupal.stackexchange.com/questions/139043/…. That's if you are using D7 and above.
    – oksana-c
    Commented Mar 8, 2016 at 20:52
  • Tnx. I hope the code above helps you to solve your task.
    – oksana-c
    Commented Mar 8, 2016 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.