Saturday, January 7, 2017

Drush and hook_init: how to don't break drush commands

If you have redirect functionality implemented with drupal_goto() function on hook_init() probably you aren't able to run drush commands because they crashe with a message:
Drush command terminated abnormally due to an unrecoverable error.  [error]
It happens because drush bootstraps Drupal application but can't perform redirect. To avoid this you need to check whether script runs from cli or not:
<?php

/**
 * Implements hook_init().
 */
function module_init() {
  if (!drupal_is_cli()) {
    drupal_goto('some/path');
  }
}

Key notes:

No comments:

Post a Comment