0

I am working on a custom installation profile, and am using $_SERVER['DOCUMENT_ROOT'] as part of creating custom folders inside <drupal-root>/sites/sitename.tld/.
It works as expected when using the UI installation process, but when I try to run it through drush site-install my_custom_install it fails. I expect this is because $_SERVER is never set in the CLI (at least that is what I could understand from search around for a solution).

In terms of the error I get when it fails, it is just a permission denied for creating the folder, since it tries to create it in /sites/sitename.tld in the root of the server, which it naturally does not have access to. It's this path that is correctly getting the document root in the UI, but not in CLI.

Code example that is creating the folder

copyTheme($from, $to, &$context) {
  $site_root = $_SERVER['DOCUMENT_ROOT'];

  $context['message'] = 'Copy theme';

  $source = $site_root . $from;
  $destination = $site_root . $to;

  try {
    // If the destination directory does not exist create it
    if (!is_dir($destination)) {
      if (!mkdir($destination, 0777, TRUE)) {
        // If the destination directory could not be created stop processing
        throw new Error('Could not create custom theme folder \'' . $destination . '\'');
      }
    }

    //if (chmod($destination, 0777)) {
    //  rcopy($source, $destination, $context);
    //}
  } catch (\Exception $e) {
    \Drupal::logger('custom_installer')->error($e->getMessage());
  }
}

Snippet of the actual error output during drush site-install
In the error, installtester.dev is the $project_domain and installtester is the $project_name.

Error: Could not create custom theme folder '/sites/installtester.dev/themes/custom/installtester' in /var/www/drupalvm/drupal/webroot/profiles/contrib/custom_installer/batch.inc on line 20 #0 /var/www/drupalvm/drupal/webroot/core/includes/batch.inc(252): copyTheme('/themes/contrib...','/sites/installt...', Array)
#1 /var/www/drupalvm/drupal/webroot/core/includes/form.inc(874): batch_process()
#2 /var/www/drupalvm/drupal/webroot/core/includes/install.core.inc(619): batch_process(Object(Drupal\Core\Url), Object(Drupal\Core\Url))
...

In case it is relevant, I am doing the development on drupal-vm via vagrant and virtualbox on a windows 10 machine.

Question
Is there a better way than using $_SERVER['DOCUMENT_ROOT'] to get the document root? one that will work both using UI install and drush site-install?.

Alternatively, is there a parameter I can check for if I use the --root option for drush site-install?

0

I found a solution that works.

Drupal sets it's own constants, such as DRUPAL_ROOT which can be used instead of $_SERVER['DOCUMENT_ROOT'].

So, all I had to do was to change $site_root = $_SERVER['DOCUMENT_ROOT']; to $site_root = DRUPAL_ROOT;, for it to work correctly when installed via Drush.

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.