Skip to content

Drupal\helper\Config

Provides helper for working with configuration.

Methods

Name Description
__construct Config constructor.
create Creates a configuration.
exists Checks if a configuration exists or not.
exportFile Export a single configuration file.
getData Get the config data.
getSyncDirectory Gets the config sync directory.
importDirectory Import a directory containing configuration files.
importFile Import a single configuration file.
importModule Import a module's directory containing configuration files.
isExported Determines if configuration is currently exported.
load Load a configuration.
reExportDirectory Re-export a directory containing configuration files.
reExportModule Import a module's directory containing configuration files.
skipExisting Skip config imports if existing configuration already exists.
updateExisting Sets a flag to update existing configuration when importing the same file.

Config::__construct

Description

public __construct (\Drupal\Core\Config\ConfigFactoryInterface $config_factory, \Drupal\Core\Config\ConfigManagerInterface $config_manager, \Psr\Log\LoggerInterface $logger, \Drupal\Core\File\FileSystemInterface $file_system, \Drupal\Core\Extension\ModuleExtensionList $module_list)

Config constructor.

Parameters

  • (\Drupal\Core\Config\ConfigFactoryInterface) $config_factory : The configuration factory.
  • (\Drupal\Core\Config\ConfigManagerInterface) $config_manager : The configuration manager.
  • (\Psr\Log\LoggerInterface) $logger : The logger.
  • (\Drupal\Core\File\FileSystemInterface) $file_system : The file system.
  • (\Drupal\Core\Extension\ModuleExtensionList) $module_list : The module extension list.

Return Values

void


Config::create

Description

public create (string $config_name, array $data)

Creates a configuration.

This works for both simple config and configuration entities.

Parameters

  • (string) $config_name : The configuration name.
  • (array) $data : The configuration data.

Return Values

\Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Config\Config

The configuration object.


Config::exists

Description

public exists (string $config_name)

Checks if a configuration exists or not.

This works for both simple config and configuration entities.

Parameters

  • (string) $config_name : The configuration name.

Return Values

bool

TRUE if the configuration exists.


Config::exportFile

Description

public exportFile (string $config_name, string|null $directory, array $options)

Export a single configuration file.

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

  • (string) $config_name : The configuration ID.
  • (string|null) $directory : The directory that the configuration file will be written to. If not
    provided the configuration sync directory will be used.
  • (array) $options : An optional array of options.

Return Values

void

Throws Exceptions

\RuntimeException

If the directory is not writeable or if the configuration does not exist.


Config::getData

Description

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

Get the config data.

Parameters

  • (\Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Config\Config) $config : The configuration object.

Return Values

array

The configuration data.


Config::getSyncDirectory

Description

public getSyncDirectory (void)

Gets the config sync directory.

Parameters

This function has no parameters.

Return Values

string

The configuration sync directory.


Config::importDirectory

Description

public importDirectory (string $directory, array $options)

Import a directory containing configuration files.

Parameters

  • (string) $directory : The path to the directory.
  • (array) $options : An array of options to pass to file_scan_directory().

Return Values

void

Throws Exceptions

\RuntimeException

If unable to decode YAML from file.

\Drupal\Core\Entity\EntityStorageException

In unable to save the config.


Config::importFile

Description

public importFile (string $uri, string $contents)

Import a single configuration file.

Parameters

  • (string) $uri : The path to the file.
  • (string) $contents : Optional manual contents of the file.

Return Values

void

Throws Exceptions

\RuntimeException

If unable to decode YAML from file.

\Drupal\Core\Entity\EntityStorageException

In unable to save the config.


Config::importModule

Description

public importModule (string $module, string $directory, array $options)

Import a module's directory containing configuration files.

Parameters

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

Return Values

void

Throws Exceptions

\RuntimeException

If unable to decode YAML from file.

\Drupal\Core\Entity\EntityStorageException

In unable to save the config.


Config::isExported

Description

public isExported (string $config_name)

Determines if configuration is currently exported.

Parameters

  • (string) $config_name : The specific configuration to look for, defaults to system.site that
    should be required to have a Drupal site with exported config.

Return Values

bool

TRUE if configuration file is exported or FALSE if not.


Config::load

Description

public load (string $config_name)

Load a configuration.

This works for both simple config and configuration entities.

Parameters

  • (string) $config_name : The configuration name.

Return Values

\Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Config\Config|null

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


Config::reExportDirectory

Description

public reExportDirectory (string $directory, array $options)

Re-export a directory containing configuration files.

Parameters

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

Return Values

void

Throws Exceptions

\RuntimeException

If the directory is not writeable or if the configuration does not exist.


Config::reExportModule

Description

public reExportModule (string $module, string $directory, array $options)

Import a module's directory containing configuration files.

Parameters

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

Return Values

void

Throws Exceptions

\RuntimeException

If the directory is not writeable or if the configuration does not exist.


Config::skipExisting

Description

public skipExisting (void)

Skip config imports if existing configuration already exists.

Parameters

This function has no parameters.

Return Values

void


Config::updateExisting

Description

public updateExisting (bool $update)

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

Parameters

  • (bool) $update : TRUE to allow updates to existing configuration on file import, or FALSE
    to ignore imports when existing configuration already exists.

Return Values

void