Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This project is not covered by Drupal’s security advisory policy.
This module provides interfaces & services to help modernize the Batch API. Here is an example of a custom Batch Worker Service utilizing the API provided by this module:
namespace Drupal\example_module\Form\Batch;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\batch_services\BatchWorkerService;
class FooBatchWorkerService extends BatchWorkerService {
private ExampleHelperServiceInterface $exampleHelper;
public function __construct(MessengerInterface $messenger, ExampleHelperServiceInterface $exampleHelper)
{
parent::__construct($messenger);
$this->exampleHelper = $exampleHelper;
}
/**
* Gets the batch title.
*/
public function getTitle(): TranslatableInterface
{
return $this->t('Processing Foo items...');
}
/**
* Gets remote items.
*
* @param int $page
* The human-readable page number (e.g. starting with "1").
* @param int $limit
* How many items to get with each page.
*
* @return array
* A list of assets & metadata about the query.
*/
public function getRemoteItems(int $page = 1, int $limit = 100): array
{
return [
'media' => ['foo', 'bar', 'baz'],
'total' => 3,
];
}
/**
* Processes a batch item.
*/
public function processBatchItem(): void
{
$result = $this->getRemoteItems($this->getPageNumber(), static::ITEMS_PER_PAGE);
$this->setTotal($result['total']['count']);
foreach ($result['media'] as $asset) {
if ('baz' === $asset) {
$this->incrementSkipped();
}
else {
$this->exampleHelper->createMediaEntityFromAsset($asset)->save();
$this->incrementCreated();
}
$this->incrementProgress();
$this->incrementCurrentId();
}
}
}
Supporting organizations:
Project information
- Seeking co-maintainer(s)
Maintainers are looking for help reviewing issues. - Module categories: Developer Tools
- Ecosystem: Drupal core, Services
- Created by yesnoio on , updated
- This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.
Releases
1.0.0-beta1
released 12 September 2022
Works with Drupal: ^9 || ^10
✓ Recommended by the project’s maintainer.
Updating module to be better compatible with D9+.
Install:
Development version: 1.0.x-dev updated 12 Sep 2022 at 03:05 UTC