0

In the drush command definition hook for the Node Revision Delete project I have the following code:

use Drupal\node_revision_delete\Utility\Time;

...

function node_revision_delete_drush_command() {
  $values = array_keys(Time::convertWordToTime());

The namespace exists and is in the correct place but once I use drush cr I got this error:

Error: Uncaught Error: Class 'Drupal\node_revision_delete\Utility\Time' not found in /Users/adriancid/Sites/drupal84-test/modules/drupal.org/node_revision_delete/node_revision_delete.drush.inc:34

It's weird because I call the same function in the line 179 for the drush nrd-st command and is working fine:

Time::convertWordToTime();

Trying to understand what happens I tried to use an Utility function from the Core in the drush definition command hook and works fine without problem, but with my function call it doesn't works.

Any idea about class not found problem?

4
  • This is in hook_drush_command(), my guess is that the drupal bootstrap at that point isn't done and you can't rely on that to exist as drush will just load those files and execute the function. So this is simply not possible then.
    – Berdir
    Commented Aug 27, 2018 at 18:38
  • @Berdir that's what I thought too but I don't know why it works for core utility functions like Random::name() Commented Aug 27, 2018 at 18:48
  • Because drush apparently loads that, probably because it needs functions from there itself. Those classes are not in a module, so drush doesn't need a list of installed modules and their location, core/lib/.. is static, always at the same place and therefore easy to register without having to bootstrap Drupal
    – Berdir
    Commented Aug 27, 2018 at 18:49
  • @Berdir thanks, I think that this is the problem as I tried now to use a service inside the hook_drush_command() and I'm getting: \Drupal::$container is not initialized yet Commented Aug 27, 2018 at 18:51

1 Answer 1

0

As @Berdir says it seems Drupal is not fully bootstrapped when the hook_drush_command() is called as I tried to use a service defined in a custom module and I get the following error:

\Drupal::$container is not initialized yet. 

So the solution for the moment is hardcode the values that should be returned by the function.

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.