ConfigProviderOptional.php in Configuration Provider 8
File
src/Plugin/ConfigProvider/ConfigProviderOptional.php
View source
<?php
namespace Drupal\config_provider\Plugin\ConfigProvider;
use Drupal\config_provider\InMemoryStorage;
use Drupal\config_provider\Plugin\ConfigProviderBase;
use Drupal\Core\Config\Entity\ConfigEntityDependency;
use Drupal\Core\Config\Entity\ConfigDependencyManager;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
class ConfigProviderOptional extends ConfigProviderBase {
const ID = InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
public function addConfigToCreate(array &$config_to_create, StorageInterface $storage, $collection, $prefix = '', array $profile_storages = []) {
}
public function addInstallableConfig(InMemoryStorage $installable_config, array $extensions = []) {
$storage = $this
->getExtensionInstallStorage(static::ID);
$enabled_extensions = $this
->getEnabledExtensions();
$existing_config = $this
->getActiveStorages()
->listAll();
$list = $this
->listConfig($storage, $extensions);
$list = array_filter($list, function ($config_name) use ($existing_config) {
return !in_array($config_name, $existing_config) && $this->configManager
->getEntityTypeIdByName($config_name);
});
$all_config = array_merge($existing_config, $list);
$all_config = array_merge($all_config, $installable_config
->listAll());
$all_config = array_combine($all_config, $all_config);
$config_to_create = $storage
->readMultiple($list);
$dependency_manager = new ConfigDependencyManager();
$dependency_manager
->setData($config_to_create);
$config_to_create = array_merge(array_flip($dependency_manager
->sortAll()), $config_to_create);
foreach ($config_to_create as $config_name => $data) {
$remove = !$this
->validateDependencies($config_name, $data, $enabled_extensions, $all_config);
if (!$remove && !empty($dependency)) {
$config_entity = new ConfigEntityDependency($config_name, $data);
$remove = !$config_entity
->hasDependency(key($dependency), reset($dependency));
}
if ($remove) {
unset($config_to_create[$config_name]);
unset($all_config[$config_name]);
}
else {
$installable_config
->write($config_name, $data);
}
}
}
}