Skip to content

Config

Provides helper for working with configuration.


  • Full name: \Drupal\helper\Config
  • This class implements: LoggerAwareInterface

Properties

updateExistingConfig

Flag if existing active config should be updated when importing.

protected bool $updateExistingConfig

configFactory

protected \Drupal\Core\Config\ConfigFactoryInterface $configFactory

configManager

protected \Drupal\Core\Config\ConfigManagerInterface $configManager

fileSystem

protected \Drupal\Core\File\FileSystemInterface $fileSystem

moduleList

protected \Drupal\Core\Extension\ModuleExtensionList $moduleList

Methods

__construct

Config constructor.

public __construct(\Drupal\Core\Config\ConfigFactoryInterface $configFactory, \Drupal\Core\Config\ConfigManagerInterface $configManager, \Drupal\Core\File\FileSystemInterface $fileSystem, \Drupal\Core\Extension\ModuleExtensionList $moduleList): mixed

Parameters:

Parameter Type Description
$configFactory \Drupal\Core\Config\ConfigFactoryInterface
The configuration factory.
$configManager \Drupal\Core\Config\ConfigManagerInterface
The configuration manager.
$fileSystem \Drupal\Core\File\FileSystemInterface
The file system.
$moduleList \Drupal\Core\Extension\ModuleExtensionList
The module extension list.

updateExisting

Sets a flag to update existing configuration when importing the same file.

public updateExisting(bool $update = true): self

Parameters:

Parameter Type Description
$update bool
TRUE to allow updates to existing configuration on file import, or FALSE
to ignore imports when existing configuration already exists.

skipExisting

Skip config imports if existing configuration already exists.

public skipExisting(): self

isExported

Determines if configuration is currently exported.

public isExported(string $config_name = 'system.site'): bool

Parameters:

Parameter Type Description
$config_name string
The specific configuration to look for, defaults to system.site that
should be required to have a Drupal site with exported config.

Return Value:

TRUE if configuration file is exported or FALSE if not.


getSyncDirectory

Gets the config sync directory.

public getSyncDirectory(): string

Return Value:

The configuration sync directory.


exists

Checks if a configuration exists or not.

public exists(string $config_name): bool

This works for both simple config and configuration entities.

Parameters:

Parameter Type Description
$config_name string
The configuration name.

Return Value:

TRUE if the configuration exists.


load

Load a configuration.

public load(string $config_name): \Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Config\Config|null

This works for both simple config and configuration entities.

Parameters:

Parameter Type Description
$config_name string
The configuration name.

Return Value:

The configuration if it could be loaded or NULL if it could not be loaded (or created if $created is TRUE).


create

Creates a configuration.

public create(string $config_name, array $data): \Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Config\Config

This works for both simple config and configuration entities.

Parameters:

Parameter Type Description
$config_name string
The configuration name.
$data array
The configuration data.

Return Value:

The configuration object.


getData

Get the config data.

public getData(\Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Config\Config $config): array

Parameters:

Parameter Type Description
$config \Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Config\Config
The configuration object.

Return Value:

The configuration data.


importFile

Import a single configuration file.

public importFile(string $uri, string $contents = null): void

Parameters:

Parameter Type Description
$uri string
The path to the file.
$contents string
Optional manual contents of the file.

Throws:

If unable to decode YAML from file. - RuntimeException In unable to save the config. - EntityStorageException


importDirectory

Import a directory containing configuration files.

public importDirectory(string $directory, array $options = []): void

Parameters:

Parameter Type Description
$directory string
The path to the directory.
$options array
An array of options to pass to file_scan_directory().

Throws:

If unable to decode YAML from file. - RuntimeException In unable to save the config. - EntityStorageException


importModule

Import a module's directory containing configuration files.

public importModule(string $module, string $directory = 'install', array $options = []): void

Parameters:

Parameter Type Description
$module string
The module name.
$directory string
The directory name inside the module's config directory. Defaults to
'install'.
$options array
An array of options to pass to file_scan_directory().

Throws:

If unable to decode YAML from file. - RuntimeException In unable to save the config. - EntityStorageException


exportFile

Export a single configuration file.

public exportFile(string $config_name, string|null $directory = null, array $options = []): void

When a directory is provided, this will by default exclude certain keys from the exported file, like uuid and _core. When not providing a specific directory, the file will be saved to the configuration sync directory and no keys will be filtered out from the export.

Parameters:

Parameter Type Description
$config_name string
The configuration ID.
$directory string|null
The directory that the configuration file will be written to. If not
provided the configuration sync directory will be used.
$options array
An optional array of options.

Throws:

If the directory is not writable or if the configuration does not exist. - RuntimeException


reExportDirectory

Re-export a directory containing configuration files.

public reExportDirectory(string $directory, array $options = []): void

Parameters:

Parameter Type Description
$directory string
The path to the directory.
$options array
An array of options to pass to ::exportFile().

Throws:

If the directory is not writable or if the configuration does not exist. - RuntimeException


reExportModule

Import a module's directory containing configuration files.

public reExportModule(string $module, string $directory = 'install', array $options = []): void

Parameters:

Parameter Type Description
$module string
The module name.
$directory string
The directory name inside the module's config directory. Defaults to
'install'.
$options array
An array of options to pass to ::exportFile().

Throws:

If the directory is not writable or if the configuration does not exist. - RuntimeException


getEntityStorage

Gets the configuration storage.

protected getEntityStorage(string $config_name): \Drupal\Core\Config\Entity\ConfigEntityStorageInterface|null

Parameters:

Parameter Type Description
$config_name string
The configuration name.

Return Value:

The configuration entity type storage if available.


getEntityId

Get the entity ID for a configuration object.

protected getEntityId(\Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage, string $config_name): string

Parameters:

Parameter Type Description
$storage \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
The configuration entity storage.
$config_name string
The configuration name.

Return Value:

The entity ID.