ConfigReverter.php in Configuration Update Manager 8
File
src/ConfigReverter.php
View source
<?php
namespace Drupal\config_update;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ConfigReverter implements ConfigRevertInterface, ConfigDeleteInterface {
protected $entityManager;
protected $activeConfigStorage;
protected $extensionConfigStorage;
protected $extensionOptionalConfigStorage;
protected $configFactory;
protected $dispatcher;
public function __construct(EntityTypeManagerInterface $entity_manager, StorageInterface $active_config_storage, StorageInterface $extension_config_storage, StorageInterface $extension_optional_config_storage, ConfigFactoryInterface $config_factory, EventDispatcherInterface $dispatcher) {
$this->entityManager = $entity_manager;
$this->activeConfigStorage = $active_config_storage;
$this->extensionConfigStorage = $extension_config_storage;
$this->extensionOptionalConfigStorage = $extension_optional_config_storage;
$this->configFactory = $config_factory;
$this->dispatcher = $dispatcher;
}
public function import($type, $name) {
$full_name = $this
->getFullName($type, $name);
$value = FALSE;
if ($full_name) {
$value = $this->extensionConfigStorage
->read($full_name);
if (!$value) {
$value = $this->extensionOptionalConfigStorage
->read($full_name);
}
}
if (!$value) {
return FALSE;
}
if ($type == 'system.simple') {
$this->configFactory
->getEditable($full_name)
->setData($value)
->save();
}
else {
$entity_storage = $this->entityManager
->getStorage($type);
$entity = $entity_storage
->createFromStorageRecord($value);
$entity
->save();
}
$event = new ConfigRevertEvent($type, $name);
$this->dispatcher
->dispatch(ConfigRevertInterface::IMPORT, $event);
return TRUE;
}
public function revert($type, $name) {
$value = FALSE;
$full_name = $this
->getFullName($type, $name);
if ($full_name) {
$value = $this->extensionConfigStorage
->read($full_name);
if (!$value) {
$value = $this->extensionOptionalConfigStorage
->read($full_name);
}
}
if (!$value) {
return FALSE;
}
if (!$this->activeConfigStorage
->read($full_name)) {
return FALSE;
}
if ($type == 'system.simple') {
$config = $this->configFactory
->getEditable($full_name);
$core = $config
->get('_core');
$config
->setData($value)
->set('_core', $core)
->save();
}
else {
$definition = $this->entityManager
->getDefinition($type);
$id_key = $definition
->getKey('id');
$id = $value[$id_key];
$entity_storage = $this->entityManager
->getStorage($type);
$entity = $entity_storage
->load($id);
$core = $entity
->get('_core');
$entity = $entity_storage
->updateFromStorageRecord($entity, $value);
$entity
->set('_core', $core);
$entity
->save();
}
$event = new ConfigRevertEvent($type, $name);
$this->dispatcher
->dispatch(ConfigRevertInterface::REVERT, $event);
return TRUE;
}
public function delete($type, $name) {
$config = FALSE;
$full_name = $this
->getFullName($type, $name);
if ($full_name) {
if (!$this->activeConfigStorage
->read($full_name)) {
return FALSE;
}
$config = $this->configFactory
->getEditable($full_name);
}
if (!$config) {
return FALSE;
}
$config
->delete();
$event = new ConfigRevertEvent($type, $name);
$this->dispatcher
->dispatch(ConfigDeleteInterface::DELETE, $event);
return TRUE;
}
public function getFromActive($type, $name) {
$full_name = $this
->getFullName($type, $name);
if ($full_name) {
return $this->activeConfigStorage
->read($full_name);
}
return FALSE;
}
public function getFromExtension($type, $name) {
$value = FALSE;
$full_name = $this
->getFullName($type, $name);
if ($full_name) {
$value = $this->extensionConfigStorage
->read($full_name);
if (!$value) {
$value = $this->extensionOptionalConfigStorage
->read($full_name);
}
}
return $value;
}
protected function getFullName($type, $name) {
if ($type == 'system.simple' || !$type) {
return $name;
}
$definition = $this->entityManager
->getDefinition($type);
if ($definition) {
$prefix = $definition
->getConfigPrefix() . '.';
return $prefix . $name;
}
else {
return FALSE;
}
}
}
Classes
Name |
Description |
ConfigReverter |
Provides methods related to config reverting, deleting, and importing. |