Drupal - How can I execute Drush commands programmatically?

Just start it from the other side. Imagine that every Drush command executes some PHP code. Then check the code of the Drush command you want to run. And then refactor it into the submit function of your button.

Doing it like suggested in your question doesn't seem to be very sustainable, taking into account that not every host has Drush installed. So you better make that functionality work independently from its environment.


Found one possible way: How to run Drupal console command from code? using Symfony's Process and flagged question as possible duplicate. And regarding drush_invoke_process(), as how I understand it so far, it seems to be callable only from within other Drush commands.


Next thing that came to my attention: In the comments to your question you are saying that you are trying to execute /vendor/bin/drush/drush status which would mean you have the vendor folder at the root of your machine. Which very probably is wrong and you just need to fix that path.


From Drush 9 and higher drush commands are symfony services, so you can load them as other services though the services container. example:

$command = \Drupal::service('migrate_tools.commands');
$command->import('my_migration', ['update' => TRUE, 'sync' => TRUE]);

Tags:

Drush

8