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.

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', 'article');

Available helpers

HelperAccessCommon operations
TermHelper::term()createTree(), deleteAll(), find()
MenuHelper::menu()createTree(), deleteTree(), findItem(), updateItem()
EntityHelper::entity()deleteAll(), batch()
ConfigHelper::config()Import and manage config YAML
UserHelper::user()Create accounts, assign roles
RedirectHelper::redirect()Create redirects, import from CSV
FieldHelper::field()Delete fields with data purging

Batched operations

For large datasets, pass a $sandbox array to enable automatic batching:

function ys_base_deploy_update_articles(array &$sandbox): ?string {
return Helper::entity($sandbox)->batch('node', 'article', function ($node) {
$node->set('field_migrated', TRUE);
$node->save();
});
}

Example in Vortex

The ys_demo.deploy.php file demonstrates using drupal_helpers to create a menu link for the articles page during deployment.