Skip to main content

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 page 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 is not 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, not the complete set - see the module documentation for that.

HelperAccessExamples
TermHelper::term()createTree(), deleteAll(), find()
MenuHelper::menu()createTree(), deleteTree(), findItem(), updateItem()
EntityHelper::entity()deleteAll()
ConfigHelper::config()get(), set(), import(), setFrontPage()
UserHelper::user()create(), createMultiple(), assignRoles(), removeRoles()
RedirectHelper::redirect()create(), deleteBySource(), importFromCsv()
FieldHelper::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, and the batch size defaults to 50 - pass a second argument to the facade to change it, 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 does not create a duplicate.