By Joren


2012-04-17 00:17:30 8 Comments

This is what I have right now:

<?php
    define('DRUPAL_ROOT', '/var/www/clients/client1/web15/web');
    include_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//  drupal_flush_all_caches();  

    $users = views_get_view_result('userlist', 'uid_list');
    $i = 0;
    foreach ($users as $user) {
        $filename = 'sites/default/files/generated_pdfs/individual_pdf_report_' . $user->uid . '.pdf';
        generate_employee_pdf($user->uid, $filename);
        error_log('Generated ' . $filepath);
        $i++;
    }
    // create object
    $zip = new ZipArchive();
    // open archive
    if ($zip->open("sites/default/files/generated_zips/individual-reports.zip", ZIPARCHIVE::CREATE) !== TRUE) {
        die ("Could not open archive");
    }
    // initialize an iterator
    // pass it the directory to be processed
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("sites/default/files/generated_pdfs/"));
    // iterate over the directory
    // add each file found to the archive
    foreach ($iterator as $key=>$value) {
        $zip->addFile(realpath($key), $value->getFilename()) or die ("ERROR: Could not add file: $key");
    }
    // close and save archive
    $zip->close();
?>

I'm getting a bunch of errors from the above when run standalone. Works fine in my module. Some of the errors are:

Warning: Invalid argument supplied for foreach() in drupal_depth_first_search() (line 56 of /var/www/clients/client1/web15/web/includes/graph.inc).
Warning: Invalid argument supplied for foreach() in _module_build_dependencies() (line 229 of /var/www/clients/client1/web15/web/includes/module.inc).
Warning: include_once(sites/all/modules/views_bulk_operations/actions/archive.action.inc): failed to open stream: No such file or directory in views_bulk_operations_load_action_includes() (line 56 of /var/www/clients/client1/web15/web/sites/all/modules/views_bulk_operations/views_bulk_operations.module).

4 comments

@Letharion 2014-01-09 12:52:01

By far the easiest way to manage the bootstrap is to use drush php-script, alias scr.

file.php:

<?php
echo l('Link', 'path');

Cli:

drush @alias scr file.php

Output:

<a href="/path">Link</a>

This way you need to deal with nothing with regard to the bootstrap, drush will solve it entirely for you, and gives you the ability to use it easily across sites thanks to aliases. (Might even work on remote sites, though I haven't tried that)

@mpdonadio 2014-01-09 14:53:52

If you want to do this on a remote site, the script has to be remote (see drupal.stackexchange.com/questions/98098). If I ever get free time (ha!) will try a patch for this.

@Bala Clark 2014-04-23 09:43:48

This really is the simplest way to do it and should get more upvotes.

@leymannx 2015-11-17 10:21:39

What exactly is @alias supposed to do? I only get a Could not find the alias @alias. Unfortunately the link you provided is broken. Could you please fix it and expand your explanation?

@Letharion 2016-02-27 19:41:02

@leymannx Hey, sorry I didn't notice your question before. I've updated the link to the documentation. I've not expanded the alias part however, as drush aliases are really out of scope for this particular question.

@Matoeil 2015-05-15 15:45:09

this is enough:

define('DRUPAL_ROOT', '/path/to/drupal/root/');
 require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

@Zydrunas Tamasauskas 2012-04-17 05:49:24

your standalone Drupal 7 bootstrap is not quite right.

Try this:

/**
 * Do not let execute this file from http request
 */
if (isset($_SERVER['REMOTE_ADDR'])) {
  print "...";
  exit(1);
}

/**
 * Initialize Drupal
 */
define('DRUPAL_ROOT', '/var/www/clients/client1/web15/web');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
require_once DRUPAL_ROOT . '...'; // include required module files to work with here

drupal_override_server_variables(array('url' => 'http://www.example.com/batch.php')); // trick drupal to do bootstrap
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

set_time_limit(4600); // adjust for your needs
ini_set('memory_limit', '3524M'); // adjust for your needs

Hope this helps.

@Joren 2012-04-17 18:59:42

Hmm. It's failing silently at this line: $users = views_get_view_result('userlist', 'uid_list'); Am I correct that your drupal_override_server_variables call should point at the location of my php file?

@Zydrunas Tamasauskas 2012-04-18 06:53:31

Yes You right. This bootstrap loads only database layer, but not modules. You must invoke module init hook for views to use it's functionality or include module file responsible for data processing here: require_once DRUPAL_ROOT . '...'; // include required module files to work with here

@Joren 2012-04-18 18:01:48

I've included common.inc, views.module and ctools.module as the errors made it obvious they were needed. After these 3 I stopped getting errors and it just died silently. Not sure where to go from here.

@Zydrunas Tamasauskas 2012-04-19 11:47:47

Do printouts after each inclusion and see where it stuck. Then you may know what's wrong. As an option, try to do full bootstrap and see what's happens.

@mikeytown2 2012-04-17 00:49:33

The last error

Warning: include_once(sites/all/modules/views_bulk_operations/actions/archive.action.inc): failed to open stream: No such file or directory in views_bulk_operations_load_action_includes() (line 56 of /var/www/clients/client1/web15/web/sites/all/modules/views_bulk_operations/views_bulk_operations.module).

is a bug in VBO. views_bulk_operations_load_action_includes() does not use DRUPAL_ROOT when using include_once. I would file a bug and fix it in VBO.

The other error (the 2 are related to each other) might be related to _system_rebuild_module_data() not using DRUPAL_ROOT when accessing modules .info file. I would put break points in system_rebuild_module_data() for the $modules variable and see if you can pin point it. Once again file a bug with drupal core as I don't see this issue in the issue queue.

Good luck!

Related Questions

Sponsored Content

2 Answered Questions

Hierarchical select hook form alter

1 Answered Questions

[SOLVED] Save image from URL to a field from a custom module

  • 2013-07-15 11:19:54
  • Bálint Deáki
  • 6935 View
  • 2 Score
  • 1 Answer
  • Tags:   7 media

1 Answered Questions

Cant figure out what's wrong with my image style generation code

  • 2016-09-26 23:16:13
  • Remog
  • 196 View
  • 0 Score
  • 1 Answer
  • Tags:   7 media

5 Answered Questions

1 Answered Questions

Notify Module Truncate Eroor

  • 2015-01-06 18:52:04
  • WebWanderer
  • 10 View
  • 0 Score
  • 1 Answer
  • Tags:   7 notifications

1 Answered Questions

[SOLVED] How to embed a view in hook_page_build()

  • 2014-12-20 19:32:25
  • vintorg
  • 224 View
  • 1 Score
  • 1 Answer
  • Tags:   7 views hooks

Sponsored Content