Getting Your Install Profile Ready For Drupal 8
0 min read
Leah Wagner
Working in an agency environment, we're always spinning up new instances of a Drupal site for our clients. This is a task that can be initiated by any developer at any time. Having an install profile can streamline this process; keeping settings and configuration consistent for the entire team. At The Jibe we maintain an install profile for just this reason. It allows developers to get up and running quickly, with the standard settings they need to kick off their latest project.
What’s Changed for Drupal 8?
If you're used to managing a Drupal 7 install profile, what can you expect to be different for Drupal 8?
PROFILE.info > PROFILE.info.yml
Like with the modules and themes, the .info file is now using the .yml format. Other than the new syntax, not much has changed here.
,
D7: PROFILE.info (partial code snippet)
name = Profile Name description = Profile description. version = 1.0 core = 7.x dependencies[] = block dependencies[] = color dependencies[] = comment ...
,
D8: PROFILE.info.yml (partial code snippet)
name: Profile Name type: profile description: 'Profile description.' version: VERSION core: 8.x dependencies: - node - history - block
,
PROFILE.profile
The profile file can be used to customize the form used during the install process. When using the standard Drupal install, this is where the country information is set and the first user is created. Here we like to populate the form with some default information.
,
D7: PROFILE.profile (partial code snippet)
function PROFILE_form_install_configure_form_alter(&$form, $form_state) { // Account information defaults $form['admin_account']['account']['name']['#default_value'] = 'admin'; $form['admin_account']['account']['mail']['#default_value'] = '[email protected]'; // Date/time settings $form['server_settings']['site_default_country']['#default_value'] = 'CA'; $form['server_settings']['date_default_timezone']['#default_value'] = 'America/Vancouver'; }
,
Achieving the same thing in D8 looks very similar. Just an update to the hook arguments and the array name for the date and time settings.
,
D8: PROFILE.profile (partial code snippet)
function PROFILE_form_install_configure_form_alter(&$form, FormStateInterface $form_state) { // Account information defaults $form['admin_account']['account']['name']['#default_value'] = 'admin'; $form['admin_account']['account']['mail']['#default_value'] = '[email protected]'; // Date/time settings $form['regional_settings']['site_default_country']['#default_value'] = 'CA'; $form['regional_settings']['date_default_timezone']['#default_value'] = 'America/Vancouver'; }
,
PROFILE.install
Expect to see syntax changes here as well. In migrating our D7 profile to D8, it was simplest to start with the the code in the install file from the standard profile (core/profiles/standard/standard.install) as a reference.
,
D7: PROFILE.install (partial code snippet)
// Enable the admin theme. db_update('system') ->fields(array('status' => 1)) ->condition('type', 'theme') ->condition('name', 'seven') ->execute();
,
D8: PROFILE.install (partial code snippet)
// Enable the admin theme. \Drupal::configFactory()->getEditable('node.settings')->set('use_admin_theme', TRUE)->save(TRUE);
,
What we customize:
The admin role: While we give our savvy clients the administrator role, occasionally there are certain tasks we want to prevent them from running. To achieve this, we create a Developer role. During the install process, we want to ensure user 1 is assigned this Developer role.
,
D8: PROFILE.install (partial code snippet)
// Assign user 1 the "developer" role. $user = User::load(1); $user->roles[] = 'developer'; $user->save();
,
We also create user accounts for each team member.
,
D8: PROFILE.install (partial code snippet)
// Create accounts for The Jibe team $user_add = \Drupal\user\Entity\User::create(); $user_add->setUsername('Sheila Mullins'); $user_add->setEmail('[email protected]'); $user_add->setPassword(user_password()); $user_add->enforceIsNew(); $user_add->activate(); $user_add->roles[] = 'developer'; $res = $user_add->save(); $user_add = \Drupal\user\Entity\User::create(); $user_add->setUsername('Alberto Mota'); $user_add->setEmail('[email protected]'); $user_add->setPassword(user_password()); $user_add->enforceIsNew(); $user_add->activate(); $user_add->roles[] = 'developer'; $res = $user_add->save();
,
PROFILE/install/*.yml
Drupal 8’s new configuration management system gives us a whole new set of tools to work with. When looking at core’s standard install profile (core/profiles/standard), you will see a new install folder. You may also have noticed this when working with Drupal 8 modules and themes. This folder contains configuration, captured in .yml files, that gets applied on installation.
As mentioned above, we like to create a Developer role during the installation process. To achieve this in a D7 install profile, we would add the following code to PROFILE.install
,
D7: PROFILE.install (partial code snippet)
// Create a default role for The Jibe developers with all available permissions assigned. $dev_role = new stdClass(); $dev_role->name = 'developer'; $dev_role->weight = 2; user_role_save($dev_role); user_role_grant_permissions($dev_role->rid, array_keys(module_invoke_all('permission'))); // Set this as the administrator role. variable_set('user_admin_role', $dev_role->rid);
,
With Drupal 8, we can simply add a new .yml file to the install folder
,
D8: PROFILE/install/user.role.developer.yml
langcode: en status: true dependencies: { } id: developer label: Developer weight: 2 is_admin: true permissions: { }
,
You can apply this logic to any default configuration you want to put in place. Simply capture your desired configuration in the appropriate .yml file and add it to the install folder. If you are exporting this configuration from an existing site, make sure you first remove the config hash from the code. This hash will be created on install so it should not be included here.
, _core: default_config_hash: 2QzULf0zovJQx3J06Y9rufzzfi-CY2CTTlEfJJh2Qyw
,
The next step…
With our Drupal 7 profile, we also used Drush Make files to pull in our standard suite of contributed modules. With Drupal 8, we also have a new approach using Composer. We will be looking into both options to see what works best for us.
Do you have a preference? Let us know!
Image courtesty of Drupal