Skip to content
Snippets Groups Projects
Select Git revision
  • 3443841-warning-undefined-array
  • 1.0.x default
  • script-tweaks
  • 1.0.1-beta7
  • 1.0.1-beta6
  • 1.0.1-beta5
  • 1.0.1-beta4
  • 1.0.1-beta3
  • 1.0.1-beta2
  • 1.0.1-beta1
  • 1.0.1-alpha4
  • 1.0.1-alpha3
  • 1.0.1-alpha2
  • 1.0.1-alpha1
14 results

codit_batch_operations-3443841

  • Clone with SSH
  • Clone with HTTPS
  • Forked from project / codit_batch_operations
    55 commits behind, 1 commit ahead of the upstream repository.
    abhishek_gupta's avatar
    Abhishek Gupta authored
    d14f0550
    History

    Codit: Batch Operations is for Drupal developers to easily invoke batch operations in hook_update_n, Drush Deploy hooks, or scripts run with drush.

    Features

    • Baked-in reporting of operations performed in the batch into a BatchOpLog entity.
    • If the Batch Operation gets interrupted (error, exception, ctrl-c ...), the operation is still logged. The state is kept, so the next time it runs it picks up where it left off.
    • Custom batch process scripts live in a local/custom module
    • Batch Operation scripts can be used the same way in:
      • hook_update_n()
      • post_update functions
      • drush deploy and post-deploy functions
      • in terminal with calls to drush scr
      • Drush command to execute scripts - drush codit-batch-operations:run {script class name}
    • Optional UI to run Batch Operation scripts (coming soon)
    • Option to skip items that error and keep going, or stop on first error.

    Post-Installation

    • Assign any roles to the permission to manage the configuration.
    • Visit /admin/config/system/batch_operations and set the following:
      • The machine name of the local module that will contain the scripts.
      • The user id of the user that will get named in entity saves.
    • Go to the custom module that you identified, and add the directory src/cbo_scripts/ This is where you will add your Batch Operations scripts.

    Additional Requirements

    None

    Recommended modules/libraries

    None

    Similar projects

    None

    Create your own script

    1. Copy the StarterScript.php.text from this module in to your src/cbo_scripts directory that you created in the Post-Installation steps.
    2. Rename the file to be a MeaningfulName.php
    3. Open the file and change the name of the class, to match the filename.
    4. Change the "namespace" entry to use the module_name of where this script now resides.
    5. Adjust each of the methods in the script to return what is needed.

    Examples

    How to run Batch Operation from hook_update_n()?

    hook_update_n() runs before config import before hook_post_update() and before deploy (when using drush deploy) They live in any module's install file. (MY_MODULE_NAME.install.php) Full documentation about hook_update_n() https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21module.api.php/function/hook_update_N/

    1. In whatever local/custom module you want to run this Batch Operation, add or use the existing MY_MODULE_NAME.install add the following
        /**
         * A description of what this update will do.
         */
        function MY_MODULE_NAME_update_90??(&$sandbox) {
          $script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME';
          return $script->run($sandbox, 'hook_update');
        }
    2. Change the function name to match the module it resides in.
    3. Make the number be one higher than the previous hook_update_N().
    4. Change the MY_MODULE_NAME in the class to match where the script lives.
    5. Change the SCRIPT_NAME to match the class name of the script you want to run.
    6. This hook_update_N() you just created will run once when you fo either:
      • drush updb
      • drush deploy
      • visit /update.php and run updates in the UI.

    How to run Batch Operation from hook_post_update()?

    Post update runs after all the hook_update_N have run. Post update hooks live in their own file (MY_MODULE_NAME.post_update.php). Hook_post_updates are not sequentially numbered, though they could be. They are suffixed with a name. I suggest using the same name as your BatchOp script or anything else that is meaningful.

    1. In whatever local/custom module you want to run this Batch Operation, add or use the existing MY_MODULE_NAME.post_update.php add the following
        /**
         * A description of what this post update will do.
         */
        function MY_MODULE_NAME_post_update_SCRIPT_NAME(&$sandbox) {
          $script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME';
          return $script->run($sandbox, 'post_update');
        }
      hook_post_update functions are only ever run once. The function name is tracked, but is not sequential. Drupal is more stable and fully operational than it is during hook_update_N().

    How to run Batch Operation from hook_deploy()?

    Drush deploy hooks run after all the hook_update_N and Post update hooks run. They live in their own file (MY_MODULE_NAME.deploy.php). Drush deploy hooks are not sequentially numbered, though they could be. They are suffixed with a name. I suggest using the same name as your BatchOp script or anything else that is meaningful.

    1. In whatever local/custom module you want to run this Batch Operation, add or use the existing MY_MODULE_NAME.deploy.php add the following
        /**
         * A description of what this post update will do.
         */
        function MY_MODULE_NAME_deploy_SCRIPT_NAME(array &$sandbox) {
          $script = \Drupal::classResolver('\Drupal\MY_MODULE_NAME\cbo_scripts\SCRIPT_NAME';
          return $script->run($sandbox, 'post_update');
        }

    FAQs

    • How do I run my script and have it keep going if there are errors? The third argument in the run() method is optional. Set it to TRUE if you want your script to skip items with errors.
    • How do I run my script with Drush? The Drush command is drush codit-batch-operations:run {script class name} it has an optional flag --allow-skip. The flag allows you to skip an errors or failures that occur, leaving the flag off, makes it fail on the first error or exception.
    • When trying to run script, I get an error telling me a script is already running, but one should not be. How do I fix this. First, check with your team to make sure someone else is not running a Batch Operation script. If they confirm that nothing should be running, you can reset the number by using drush state:delete cbo_running_script
    • How do I have the script run as someone other than the default user? There are two methods to switch users:
      1. In your run() function you can call ->switchUser(<UID>).
      2. If executing in a hook, you can call ->switchUser(<UID>) before calling ->run().