You are here

class ConfigHelper in Lightning Core 8.3

Same name and namespace in other branches
  1. 8.5 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper
  2. 8 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper
  3. 8.2 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper
  4. 8.4 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper

A facade to assist with manipulating default config.

Hierarchy

Expanded class hierarchy of ConfigHelper

4 files declare their use of ConfigHelper
ConfigHelperTest.php in tests/src/Kernel/ConfigHelperTest.php
lightning_core.install in ./lightning_core.install
Contains install and update routines for Lightning.
lightning_search.install in modules/lightning_search/lightning_search.install
Contains installation and update routines for Lightning Search.
Update360.php in src/Update/Update360.php

File

src/ConfigHelper.php, line 15

Namespace

Drupal\lightning_core
View source
class ConfigHelper extends InstallStorage {

  /**
   * The extension whose default config is being manipulated by this object.
   *
   * @var \Drupal\Core\Extension\Extension
   */
  protected $extension;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * ConfigHelper constructor.
   *
   * @param \Drupal\Core\Extension\Extension $extension
   *   The extension whose default config is being manipulated by this object.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(Extension $extension, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct();
    $this->extension = $extension;
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Switches to the default config directory.
   *
   * @return $this
   *   The called object, for chaining.
   */
  public function install() {
    $this->directory = self::CONFIG_INSTALL_DIRECTORY;
    return $this;
  }

  /**
   * Switches to the optional config directory.
   *
   * @return $this
   *   The called object, for chaining.
   */
  public function optional() {
    $this->directory = self::CONFIG_OPTIONAL_DIRECTORY;
    return $this;
  }

  /**
   * Transparently loads a config entity from the extension's config.
   *
   * @param string $entity_type
   *   The entity type ID.
   * @param string $id
   *   The entity ID.
   * @param bool $force
   *   (optional) If TRUE, the entity is read from config even if it already
   *   exists. Defaults to FALSE.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   The config entity, or NULL if it doesn't exist.
   */
  public function getEntity($entity_type, $id, $force = FALSE) {
    $storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $entity = $storage
      ->load($id);
    if ($entity && empty($force)) {
      return $entity;
    }
    $prefixes = $this
      ->getConfigPrefixes();
    return $storage
      ->create($this
      ->read($prefixes[$entity_type] . '.' . $id));
  }

  /**
   * Loads a simple config object from the extension's config.
   *
   * @param string $id
   *   The config object ID.
   *
   * @return \Drupal\Core\Config\Config
   *   The config object.
   */
  public function get($id) {
    $data = $this
      ->read($id);
    return $this->configFactory
      ->getEditable($id)
      ->setData($data);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($id) {
    foreach ($this
      ->getConfigPrefixes() as $entity_type => $prefix) {
      $prefix .= '.';
      if (mb_strpos($id, $prefix) === 0) {
        $entity = $this
          ->getEntity($entity_type, mb_substr($id, mb_strlen($prefix)));
        return $entity
          ->delete();
      }
    }
    return $this
      ->get($id)
      ->delete();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll($prefix = '') {
    foreach ($this
      ->listAll($prefix) as $id) {
      $this
        ->delete($id);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getAllFolders() {
    return $this
      ->getComponentNames([
      $this->extension
        ->getName() => $this->extension,
    ]);
  }

  /**
   * Returns a map of config entity type IDs to config prefixes.
   *
   * @return string[]
   *   The config prefixes, keyed by the corresponding entity type ID.
   */
  protected function getConfigPrefixes() {
    $prefix_map = [];
    foreach ($this->entityTypeManager
      ->getDefinitions() as $id => $definition) {
      if ($definition instanceof ConfigEntityTypeInterface) {
        $prefix_map[$id] = $definition
          ->getConfigPrefix();
      }
    }
    return $prefix_map;
  }

  /**
   * Checks if a config entity is bundled with Lightning.
   *
   * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
   *   The config entity.
   *
   * @return bool
   *   Whether the config entity is marked as being bundled with Lightning.
   */
  public static function isBundled(ConfigEntityInterface $entity) {
    return (bool) $entity
      ->getThirdPartySetting('lightning', 'bundled', FALSE);
  }

  /**
   * Creates a new ConfigHelper for a module.
   *
   * @param string $module
   *   The module name.
   *
   * @return static
   *   A new ConfigHelper object.
   */
  public static function forModule($module) {
    return new static(\Drupal::moduleHandler()
      ->getModule($module), \Drupal::configFactory(), \Drupal::entityTypeManager());
  }

  /**
   * Creates a new ConfigHelper for a theme.
   *
   * @param string $theme
   *   The theme name.
   *
   * @return static
   *   A new ConfigHelper object.
   */
  public static function forTheme($theme) {
    return new static(\Drupal::service('theme_handler')
      ->getTheme($theme), \Drupal::configFactory(), \Drupal::entityTypeManager());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigHelper::$configFactory protected property The config factory.
ConfigHelper::$entityTypeManager protected property The entity type manager.
ConfigHelper::$extension protected property The extension whose default config is being manipulated by this object.
ConfigHelper::delete public function Overrides Drupal\Core\Config\FileStorage::delete(). Overrides InstallStorage::delete
ConfigHelper::deleteAll public function Overrides Drupal\Core\Config\FileStorage::deleteAll(). Overrides InstallStorage::deleteAll
ConfigHelper::forModule public static function Creates a new ConfigHelper for a module.
ConfigHelper::forTheme public static function Creates a new ConfigHelper for a theme.
ConfigHelper::get public function Loads a simple config object from the extension's config.
ConfigHelper::getAllFolders protected function Returns a map of all config object names and their folders. Overrides InstallStorage::getAllFolders
ConfigHelper::getConfigPrefixes protected function Returns a map of config entity type IDs to config prefixes.
ConfigHelper::getEntity public function Transparently loads a config entity from the extension's config.
ConfigHelper::install public function Switches to the default config directory.
ConfigHelper::isBundled public static function Checks if a config entity is bundled with Lightning.
ConfigHelper::optional public function Switches to the optional config directory.
ConfigHelper::__construct public function ConfigHelper constructor. Overrides InstallStorage::__construct
FileStorage::$collection protected property The storage collection.
FileStorage::$fileCache protected property The file cache object.
FileStorage::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection 1
FileStorage::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
FileStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
FileStorage::ensureStorage protected function Check if the directory exists and create it if not.
FileStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
FileStorage::getAllCollectionNamesHelper protected function Helper function for getAllCollectionNames().
FileStorage::getCollectionDirectory protected function Gets the directory for the collection.
FileStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
FileStorage::getFileExtension public static function Returns the file extension used by the file storage for all configuration files.
FileStorage::getFileSystem private function Returns file system service.
FileStorage::read public function Implements Drupal\Core\Config\StorageInterface::read(). Overrides StorageInterface::read
FileStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
InstallStorage::$directory protected property The directory to scan in each extension to scan for files. Overrides FileStorage::$directory
InstallStorage::$folders protected property Folder map indexed by configuration name.
InstallStorage::CONFIG_INSTALL_DIRECTORY constant Extension sub-directory containing default configuration for installation.
InstallStorage::CONFIG_OPTIONAL_DIRECTORY constant Extension sub-directory containing optional configuration for installation.
InstallStorage::CONFIG_SCHEMA_DIRECTORY constant Extension sub-directory containing configuration schema.
InstallStorage::exists public function Returns whether a configuration object exists. Overrides FileStorage::exists
InstallStorage::getComponentFolder protected function Get folder inside each component that contains the files.
InstallStorage::getComponentNames public function Get all configuration names and folders for a list of modules or themes.
InstallStorage::getCoreFolder protected function Get folder inside Drupal core that contains the files.
InstallStorage::getCoreNames public function Get all configuration names and folders for Drupal core.
InstallStorage::getFilePath public function Overrides Drupal\Core\Config\FileStorage::getFilePath(). Overrides FileStorage::getFilePath
InstallStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides FileStorage::listAll
InstallStorage::rename public function Overrides Drupal\Core\Config\FileStorage::rename(). Overrides FileStorage::rename
InstallStorage::reset public function Resets the static cache.
InstallStorage::write public function Overrides Drupal\Core\Config\FileStorage::write(). Overrides FileStorage::write
StorageInterface::DEFAULT_COLLECTION constant The default collection name.