githubEdit

Creating Your Own Recipe

Drupal recipes provide a standardized way to package and apply sets of modules, configurations, and permissions as reusable, composable units. This guide explains how to create a custom recipe that builds on Varbase to add project-specific functionality.

Recipe Structure

A Drupal recipe is a directory containing a recipe.yml file and optionally a config/ directory with configuration files. The basic structure is:

my_custom_recipe/
  recipe.yml
  config/
    module_name.config_name.yml
    another_module.another_config.yml

Creating the recipe.yml File

The recipe.yml file is the core of every recipe. It defines the recipe's metadata, dependencies, modules, configuration actions, and permissions.

Basic Structure

name: 'My Custom Recipe'
description: 'A custom recipe that adds project-specific functionality to a Varbase site.'
type: 'Site'

Defining Dependencies

Dependencies declare other recipes that must be applied before this recipe:

When your recipe is applied, all dependency recipes are applied first in the order listed.

Installing Modules

Specify modules that should be installed by the recipe:

Configuration Actions

Config actions allow you to modify existing configuration or create new configuration:

Setting Permissions

Grant permissions to specific roles:

Including Configuration Files

To include full configuration files, place them in the config/ directory within your recipe. These files follow the same naming convention as Drupal configuration exports:

Each configuration file contains the full YAML configuration for that config item. You can export existing configuration using Drush:

Then copy the relevant files to your recipe's config/ directory.

Complete Example

Here is a complete example of a custom recipe that adds a "Project" content type:

With the corresponding configuration files in the config/ directory defining the content type, fields, view displays, and form displays.

Applying Your Recipe

To apply your custom recipe using Drush:

For Recipes in the Project Directory

If your recipe is in a local directory:

For Recipes Installed via Composer

If your recipe is published as a Composer package:

Publishing Your Recipe

To make your recipe available to others:

  1. Create a project on Drupal.org.

  2. Publish the recipe as a Composer package.

  3. Others can then install it via Composer and apply it with Drush.

Best Practices

  1. Keep recipes focused: Each recipe should address a single area of functionality. Compose larger configurations from multiple smaller recipes.

  2. Declare dependencies explicitly: List all recipe dependencies so that applying your recipe automatically satisfies all requirements.

  3. Use config actions over raw config: Prefer createIfNotExists and grantPermissions config actions over raw configuration files when possible, as they are more resilient to existing site state.

  4. Test recipe application: Test your recipe on a clean Varbase installation to ensure it applies without errors.

  5. Document your recipe: Include a clear description and any special instructions in the recipe.yml file.

Last updated