Drupal helpers
Drupal helpers is a utility library that provides static facade helpers for common Drupal development tasks, primarily intended for use within deploy hooks and update scripts.
This section covers how Vortex uses the module. The module documentation is the reference for its full API.
Helper facade
The Helper class provides convenient access to helper services without
needing dependency injection:
use Drupal\drupal_helpers\Helper;
// Create taxonomy terms.
Helper::term()->createTree('tags', ['News', 'Events', 'Blog']);
// Create menu links.
Helper::menu()->createTree('main', [
'About' => '/about',
'Contact' => '/contact',
]);
// Delete all entities of a type.
Helper::entity()->deleteAll('node', 'page');
Each facade throws a RuntimeException when the helper needs a module that
isn't installed, so a deploy hook fails loudly rather than half-applying.
Available helpers
Every facade is listed below. The Examples column shows a few of the methods each one exposes. The module documentation lists the complete set.
| Helper | Access | Examples |
|---|---|---|
| Term | Helper::term() | createTree(), deleteAll(), find() |
| Menu | Helper::menu() | createTree(), deleteTree(), findItem(), updateItem() |
| Entity | Helper::entity() | deleteAll() |
| Config | Helper::config() | get(), set(), import(), setFrontPage() |
| User | Helper::user() | create(), createMultiple(), assignRoles(), removeRoles() |
| Redirect | Helper::redirect() | create(), deleteBySource(), importFromCsv() |
| Field | Helper::field() | delete(), deleteInstance() |
Batched operations
Every facade except Helper::config() accepts a $sandbox array, which
turns on batch() and batchEntity() so large datasets are processed
across multiple deploy hook passes:
function ys_base_deploy_update_pages(array &$sandbox): ?string {
return Helper::entity($sandbox)->batchEntity('node', 'page', function ($node) {
$node->set('field_migrated', TRUE);
$node->save();
});
}
batchEntity() queries the entity IDs itself; batch() takes an arbitrary
array of items instead. Both return the progress message Drush prints. The
batch size defaults to 50. To change it, pass a second argument to the
facade, for example Helper::entity($sandbox, 100).
Example in Vortex
The
ys_demo.deploy.php
file uses Helper::menu() to add a Pages link to the main navigation
during deployment, guarding the operation with findItem() so re-running
the hook doesn't create a duplicate.