Skip to content

How to programmatically apply a Drupal recipe

Skip to content

Search Gists
Search...
All gists
Back to GitHub
Sign in
Sign up
Instantly share code, notes, and snippets.

@phenaproxima
phenaproxima/apply-a-recipe.php
Last active last month
Code
Revisions
5
Stars
2
Forks
1
Clone this repository at <script src="https://gist.github.com/phenaproxima/182b7d5f23d3c3e17879057d20877bb1.js"></script>
<script src="https://gist.github.com/phenaproxima/182b7d5f23d3c3e17879057d20877bb1.js"></script>
How to programmatically apply a Drupal recipe (works in 10.3 and later)
apply-a-recipe.php
<?php

use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;

// There are two ways to apply a recipe programmatically: immediately, or as a batch job.
// The batch job is generally the safer option, since more complex recipes could risk
// timing out if they try to do too much at once.

// So, first thing you need to do is get a Recipe object. Point it to the directoy where
// recipe.yml is:
$recipe = Recipe::createFromDirectory('/path/of/some/recipe');

// Now, if you want to make that a batch job:

$batch = new BatchBuilder();
foreach (RecipeRunner::toBatchOperations($recipe) as $operation) {
  $batch->addOperation(...$operation);
}

// Maybe you do other stuff with $batch here, like adding more operations, setting up the messages, etc.

// Finally, to kick off the job:
batch_set($batch->toArray());

// And that's that.

// If you want to just immediately apply the recipe, without giving the user any feedback, that's even
// easier:

RecipeRunner::processRecipe($recipe);

// Done-zo.

// By the way, BOTH of these techniques will automatically apply all the recipes that $recipe depends upon. You don't
// have to worry about setting it up or recursing or any of that nonsense. Just let the recipe system handle it for you.
 to join this conversation on GitHub. Already have an account? Sign in to comment
Footer
© 2025 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Docs
Contact
Manage cookies
Do not share my personal information