class ConfigHelper in Lightning Core 8
Same name and namespace in other branches
- 8.5 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper
- 8.2 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper
- 8.3 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper
- 8.4 src/ConfigHelper.php \Drupal\lightning_core\ConfigHelper
A facade to assist with manipulating default config.
Hierarchy
- class \Drupal\Core\Config\FileStorage implements StorageInterface- class \Drupal\Core\Config\InstallStorage- class \Drupal\lightning_core\ConfigHelper
 
 
- class \Drupal\Core\Config\InstallStorage
Expanded class hierarchy of ConfigHelper
1 file declares its use of ConfigHelper
- lightning_search.install in modules/lightning_search/ lightning_search.install 
- Contains installation and update routines for Lightning Search.
File
- src/ConfigHelper.php, line 16 
Namespace
Drupal\lightning_coreView 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 (Unicode::strpos($id, $prefix) === 0) {
        $entity = $this
          ->getEntity($entity_type, Unicode::substr($id, Unicode::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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| ConfigHelper:: | protected | property | The config factory. | |
| ConfigHelper:: | protected | property | The entity type manager. | |
| ConfigHelper:: | protected | property | The extension whose default config is being manipulated by this object. | |
| ConfigHelper:: | public | function | Overrides Drupal\Core\Config\FileStorage::delete(). Overrides InstallStorage:: | |
| ConfigHelper:: | public | function | Overrides Drupal\Core\Config\FileStorage::deleteAll(). Overrides InstallStorage:: | |
| ConfigHelper:: | public static | function | Creates a new ConfigHelper for a module. | |
| ConfigHelper:: | public static | function | Creates a new ConfigHelper for a theme. | |
| ConfigHelper:: | public | function | Loads a simple config object from the extension's config. | |
| ConfigHelper:: | protected | function | Returns a map of all config object names and their folders. Overrides InstallStorage:: | |
| ConfigHelper:: | protected | function | Returns a map of config entity type IDs to config prefixes. | |
| ConfigHelper:: | public | function | Transparently loads a config entity from the extension's config. | |
| ConfigHelper:: | public | function | Switches to the default config directory. | |
| ConfigHelper:: | public static | function | Checks if a config entity is bundled with Lightning. | |
| ConfigHelper:: | public | function | Switches to the optional config directory. | |
| ConfigHelper:: | public | function | ConfigHelper constructor. Overrides InstallStorage:: | |
| FileStorage:: | protected | property | The storage collection. | |
| FileStorage:: | protected | property | The file cache object. | |
| FileStorage:: | public | function | Creates a collection on the storage. Overrides StorageInterface:: | 1 | 
| FileStorage:: | public | function | Decodes configuration data from the storage-specific format. Overrides StorageInterface:: | |
| FileStorage:: | public | function | Encodes configuration data into the storage-specific format. Overrides StorageInterface:: | |
| FileStorage:: | protected | function | Check if the directory exists and create it if not. | |
| FileStorage:: | public | function | Gets the existing collections. Overrides StorageInterface:: | |
| FileStorage:: | protected | function | Helper function for getAllCollectionNames(). | |
| FileStorage:: | protected | function | Gets the directory for the collection. | |
| FileStorage:: | public | function | Gets the name of the current collection the storage is using. Overrides StorageInterface:: | |
| FileStorage:: | public static | function | Returns the file extension used by the file storage for all configuration files. | |
| FileStorage:: | private | function | Returns file system service. | |
| FileStorage:: | public | function | Implements Drupal\Core\Config\StorageInterface::read(). Overrides StorageInterface:: | |
| FileStorage:: | public | function | Reads configuration data from the storage. Overrides StorageInterface:: | |
| InstallStorage:: | protected | property | The directory to scan in each extension to scan for files. Overrides FileStorage:: | |
| InstallStorage:: | protected | property | Folder map indexed by configuration name. | |
| InstallStorage:: | constant | Extension sub-directory containing default configuration for installation. | ||
| InstallStorage:: | constant | Extension sub-directory containing optional configuration for installation. | ||
| InstallStorage:: | constant | Extension sub-directory containing configuration schema. | ||
| InstallStorage:: | public | function | Returns whether a configuration object exists. Overrides FileStorage:: | |
| InstallStorage:: | protected | function | Get folder inside each component that contains the files. | |
| InstallStorage:: | public | function | Get all configuration names and folders for a list of modules or themes. | |
| InstallStorage:: | protected | function | Get folder inside Drupal core that contains the files. | |
| InstallStorage:: | public | function | Get all configuration names and folders for Drupal core. | |
| InstallStorage:: | public | function | Overrides Drupal\Core\Config\FileStorage::getFilePath(). Overrides FileStorage:: | |
| InstallStorage:: | public | function | Gets configuration object names starting with a given prefix. Overrides FileStorage:: | |
| InstallStorage:: | public | function | Overrides Drupal\Core\Config\FileStorage::rename(). Overrides FileStorage:: | |
| InstallStorage:: | public | function | Resets the static cache. | |
| InstallStorage:: | public | function | Overrides Drupal\Core\Config\FileStorage::write(). Overrides FileStorage:: | |
| StorageInterface:: | constant | The default collection name. | 
