Skip to content

Add a command to generate batch service #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bhanu951 opened this issue Apr 1, 2024 · 2 comments
Open

Add a command to generate batch service #130

bhanu951 opened this issue Apr 1, 2024 · 2 comments

Comments

@bhanu951
Copy link
Contributor

bhanu951 commented Apr 1, 2024

Please add a command to generate batch service.
Currently there is no command to generate batch class.
Hence we need to code same boilerplate code again and again.
Please Create a generator class to generate batch service.

@harivansh0
Copy link

Hi @bhanu951

Proposed Solution

Introduce a new generator command: generate:batch-service, which will create a batch service class with the required structure to use Drupal's Batch API.


namespace {{ namespace }};

use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\StringTranslation\StringTranslationTrait;

class {{ class }} {
  use StringTranslationTrait;

  public function buildBatch(array $items) {
    $batch_builder = new BatchBuilder();
    $batch_builder
      ->setTitle($this->t('Processing items...'))
      ->setFinishCallback([$this, '{{ finished_callback|default("finished") }}']);

    foreach ($items as $item) {
      $batch_builder->addOperation([$this, '{{ operation_callback|default("processItem") }}'], [$item]);
    }

    return $batch_builder->toArray();
  }

  public function {{ operation_callback|default("processItem") }}($item, array &$context) {
    // TODO: Implement batch processing logic.
  }

  public function {{ finished_callback|default("finished") }}(bool $success, array $results, array $operations) {
    if ($success) {
    // TODO: Implement batch processing logic.

    }
    else {
    // TODO: Implement batch processing logic.

    }
  }
}

Lets me know if you any suggestion, Other wise I would start working on feature

@bhanu951
Copy link
Contributor Author

@harivansh0

Lets me know if you any suggestion, Other wise I would start working on feature

I think it looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants