Chad Peppers

Drupal 10 Utility Class - Settings

01/23/2023

Introduction

Drupal 10 provides a utility class, aptly named Settings, which provides a simple and secure way to manage read only settings for your Drupal site. This class allows you to store your site's configuration settings in a single location, making it easy to manage and update them as needed.

Setup

To take advantage of the Settings class securely, simply create a new file called settings.local.php in your site's /sites/default directory. Add a code to include this file in your settings.php file and add your site specific settings (see examples below) . This file should contain the settings for your site, such as API keys or other environment specific settings.

Including your settings.local.php file.

Initiating a setting value in your settings.local.php file.

Usage

Once you have created your settings.local.php file, you can then use the Settings.php class to access and manage your site's settings. For example, you can use the get() method to retrieve a specific setting, or the set() method to update a setting. You can also use the getAll() method to retrieve all of your site's settings at once. I will show you how to use these methods in the following examples.

Getting a settings value

Getting all setting values

Drupal 10 Settings Utility Class - DPM Dump

Why use settings over configuration?

If you are familiar with Drupal configuration management, you may question the benefits of using settings over configuration. One key advantage is that settings can prevent configuration from being overwritten per environment. Additionally, using settings to store sensitive information, such as API keys, provides an added layer of security by preventing them from being committed to a repository.

Best Practices

It is important to note that while the settings.local.php file is not required, it is not recommended to store settings directly in the settings.php for security reasons, particularly if the code is being committed to a repository. A more secure alternative is to use environmental variables. Here is an example on how to grab environmental variables on Acquia Cloud.

Getting an environmental variable

Search phrases

How to use custom settings in Drupal 8 / 9 / 10
How to use variable_set in Drupal 8 / 9 / 10
How to use variable_get in Drupal 8 / 9 / 10