3

I am using Drush 9 and Drupal 8. When I execute drush site:install I find that it also modifies the permission of the web/site/default directory to r-xr-x--x, which troubles me delete that directory later. I want that directory to be 740 in my case. So two methods came to my mind. One is to run a custom shell script as post drush si or to override the default dush si command. I tried to go through some blogs but I couldn't understand them. Please explain stepwise.

How can I invoke a custom shell script to drush site:install which will be run just after that command is executed.

How can I override the default site:install command?

Note: I am using Drupal composer project. So I have a drush folder outside the webroot but I don't know if I can edit some files in that directory and write my custom commands.

3
  • 1
    Every time the cache is cleared in Drupal and some other similar events, it will lock those folders down.
    – Kevin
    Commented Nov 20, 2018 at 21:44
  • 1
    So you mean it is not only matter of drush si but any command is executed it will modify the permission. Any way to provide my custom permission ?
    – SkyRar
    Commented Nov 20, 2018 at 21:47
  • 1
    @Kevin I examined with drush cr and cc. But It doesn't modify the permission. Only drush si does
    – SkyRar
    Commented Nov 20, 2018 at 21:54

3 Answers 3

2

Greg's answer is great. But to more precisely answer this particular question follow the below steps.

1- As you are using Drupal composer project template, Go to the directory your_drupal_project/drush and create a file called SiteInstallCommands.php. You can see there are another similar file PolicyCommands.php.

2- SiteInstallCommands.php should have below code snippets. installTest() is your custom commands that is going to be executed post site:install. So change it according to your needs.

<?php

namespace Drush\Commands;


class SiteInstallCommands extends DrushCommands
{

    /**
     * Executing command just after `drush site:install`
     *
     * @hook post-command site-install
     *
     */
    public function installTest() {
        drush_shell_exec('echo test');
    }
}
3

See the example file PolicyCommands.php in the Druhs examples directory. Create a directory named "Commands" in your site's "drush" directory, and put your command file there. Its name must end with "Commands.php" or "Hooks.php".

Create a hook similar to one of the ones shown in the example file. You'll want @hook post-command site:install.

3
  • 1
    Thanks. Do I also need to define drush.yml here github.com/drupal-composer/drupal-project/tree/8.x/drush ? Is there anything I need to define or edit ? Can you please have a look on this link ?
    – SkyRar
    Commented Nov 21, 2018 at 1:34
  • 1
    For testing purpose, as per the documentation I run drush --include=/var/www/drupal/vendor/drush/drush/examples xkcd , drush cc drush, then drush xkcd, but getting error command is not defined. Am I missing something ?
    – SkyRar
    Commented Nov 21, 2018 at 3:19
  • If you put your command in /var/www/drupal/drush/Commands, then you do not need --include Commented Nov 22, 2018 at 22:43
0

To create a site:install hook I just needed to create a file SiteInstallCommands.php inside drush/Commands dir. then drush cc drush to clear the drush cache and my custom post command is up. Thanks to @greg_1_anderson for guiding. Note you have to add @hook post-command site-install not @hook post site:install.

<?php

namespace Drush\Commands;


class InstallCommands extends DrushCommands
{

    /**
     * Modify files/directory permission as per project's need executing just after `drush site:install`
     *
     * @hook post-command site-install
     *
     */
    public function installChmod() {
        $this->logger()->success(dt('Executing post site:install commands'));
        drush_shell_exec('chmod -R 740 sites/default');

    }
}
1
  • 1
    Thanks for correcting my typo; I fixed my answer to "post-command" Commented Nov 22, 2018 at 22:44

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.