ConfigManager.php in UI Patterns Settings 8.2
File
src/ConfigManager.php
View source
<?php
namespace Drupal\ui_patterns_settings;
use Drupal\Core\Config\ConfigFactoryInterface;
class ConfigManager {
private $config;
private $typeMap = [];
private $variantMap = [];
public function __construct(ConfigFactoryInterface $config_factory) {
$this->config = $config_factory
->getEditable('ui_patterns_settings.settings');
}
public function addMapping($field_id, $pattern_setting_id) {
$field_id = str_replace('.', '--', $field_id);
$map = $this->config
->get('mapping');
if ($pattern_setting_id !== NULL) {
$map[$field_id] = $pattern_setting_id;
}
else {
unset($map[$field_id]);
}
$this->config
->set('mapping', $map);
$this->config
->save();
}
public function findVariantMappings($type) {
if (isset($this->variantMap[$type])) {
return $this->variantMap[$type];
}
$map = is_array($this->config
->get('mapping')) ? $this->config
->get('mapping') : [];
foreach ($map as $field_id => $pattern_string) {
[
$pattern_id,
$setting_id,
] = explode('::', $pattern_string);
[
$entity_type_id,
$field_name,
] = explode('--', $field_id);
if ($setting_id === 'variant') {
$this->variantMap[$type][] = $field_name;
}
}
if (!isset($this->variantMap[$type])) {
$this->variantMap[$type] = [];
}
return $this->variantMap[$type];
}
public function getMappingByType($type) {
if (isset($this->typeMap[$type])) {
return $this->typeMap[$type];
}
$map = is_array($this->config
->get('mapping')) ? $this->config
->get('mapping') : [];
foreach ($map as $field_id => $pattern_id) {
[
$field_type,
$field_name,
] = explode('--', $field_id);
$this->typeMap[$field_type][$field_name] = $pattern_id;
}
if (!isset($this->typeMap[$type])) {
$this->typeMap[$type] = [];
}
return $this->typeMap[$type];
}
public function getMapping($field_id) {
$field_id = str_replace('.', '--', $field_id);
$map = $this->config
->get('mapping');
return $map[$field_id] ?? NULL;
}
}