prethiee

Creating a Custom Profile in Drupal with Configuration Import and Export: A Step-by-Step Guide

Drupal profiles are a powerful way to define a "starting point" for your site build. With a custom profile, you can set up essential modules, configuration settings, themes, and even content types that suit your specific project’s requirements. This step-by-step guide will walk you through creating a custom Drupal profile and managing the configuration import/export process efficiently.
 
Step 1: Setting Up Your Custom Profile
 
1.1. Create a Directory for Your Profile
First, navigate to the `profiles/` folder in your Drupal site directory. Create a folder for your custom profile here:
 
cd profiles/
mkdir my_custom_profile
1.2. Define Your Profile’s `.info.yml` File
Inside the new directory, create a `.info.yml` file, which describes the profile and its dependencies:
 
Here’s a basic structure for the file:
name: 'My Custom Profile'
type: profile
description: 'A custom profile for my Drupal site.'
core_version_requirement: ^10
package: Custom
dependencies:
- drupal:field
- drupal:block
This file defines the profile's name, description, the Drupal version it supports, and some basic dependencies.
 
1.3. Define the Profile in the `.profile` File
The `.profile` file contains the installation logic for your profile. Create a `my_custom_profile.profile` file:
touch my_custom_profile.profile
Add the following PHP code to define the profile:
<?php
/**
* Implements hook_install().
*/
function my_custom_profile_install() {
// Enable specific modules on installation.
\Drupal::service('module_installer')->install([
'views',
'node',
'contact',
]);
}
/**
* Implements hook_schema_version().
*/
function my_custom_profile_schema_version() {
return SCHEMA_INSTALLED;
}
This file can also be extended to perform additional installation tasks, such as creating content types or setting default configurations.
 
Step 2: Enabling Config Management for Your Profile
 
Drupal’s configuration management system is highly flexible, allowing you to easily export and import site settings between different environments. You can leverage this for your custom profile setup.
 
2.1. Enable Configuration Management Modules
To manage configuration, you need to enable the `config` and `config_installer` modules, which handle exporting and importing settings. Add these to your profile dependencies in the `.info.yml` file:
dependencies:
- drupal:config
- drupal:config_installer
2.2. Create a `config/install` Directory
To manage configuration within your profile, create a directory called `config/install` within your profile directory:
mkdir config/install
2.3. Export Configurations for Your Profile
You can export configurations from an existing Drupal site, and use them in your custom profile setup.
1. Export the Configuration:
Use Drupal’s configuration export command to export site configurations into the `config/install` folder:
drush config-export --destination=profiles/my_custom_profile/config/install
Alternatively, use the Drupal UI at `/admin/config/development/configuration/full/export` and place the exported `.yml` files in the same directory.
2. Select the Config Files for Your Profile:
The export will generate a lot of `.yml` files. You can include only the necessary files, like content types, views, and block placements. Place these files in the `config/install` folder.
 
Step 3: Customizing Your Profile Installation
 
3.1. Implement Hook to Import Configuration
In your `.profile` file, you can trigger the configuration import using a hook:
/
* Implements hook_install_tasks_alter().
*/
function my_custom_profile_install_tasks_alter(&$tasks) {
// Automatically import configurations when the profile is installed.
$tasks['import_configuration'] = [
'function' => 'my_custom_profile_import_configuration',
'label' => t('Import configurations'),
];
}
/
* Imports site configuration from the profile.
*/
function my_custom_profile_import_configuration() {
// Import all configurations.
\Drupal::service('config.installer')->installOptionalConfig();
}
This code automatically imports the configurations you’ve stored in the `config/install` folder during the profile installation process.
 
Step 4: Testing Your Profile
4.1. Install Drupal with Your Custom Profile
When you’re ready to test, navigate to your site and begin a new Drupal installation. You’ll see your custom profile listed as an option during the installation process. Select it and continue with the setup.
 
4.2. Verify Configuration Import
Once the installation is complete, verify that the configuration (like content types, views, and blocks) has been imported correctly.
 
Step 5: Extending the Profile
 
5.1. Add Default Content
You can also define default content (nodes, taxonomy terms, etc.) to be imported along with your profile by using the `hook_install()` or by including content in the configuration import.
 
5.2. Include Custom Themes and Styles
If your project requires specific themes, you can include them in your profile’s dependencies and set a default theme within the installation process.
 
Conclusion
Creating a custom Drupal profile with configuration import/export provides a streamlined way to deploy Drupal sites consistently. This method is ideal for teams working on multiple projects or organisations needing standardised setups across environments. With proper use of configuration management, your profile can not only define the base functionality but also bring over all necessary settings and structure.