You are here

public function ConfigReverter::revert in Configuration Update Manager 8

Reverts configuration to the value from extension storage.

This action triggers a ConfigRevertInterface::REVERT event.

Parameters

string $type: The type of configuration.

string $name: The name of the config item, without the prefix.

Return value

bool TRUE if the operation succeeded; FALSE if the base configuration could not be found to revert to. May also throw exceptions if there is a problem during saving the configuration.

Overrides ConfigRevertInterface::revert

See also

\Drupal\config_update\ConfigRevertInterface::REVERT

File

src/ConfigReverter.php, line 124

Class

ConfigReverter
Provides methods related to config reverting, deleting, and importing.

Namespace

Drupal\config_update

Code

public function revert($type, $name) {

  // Read the config from the file. Note: Do not call getFromExtension() here
  // because we need $full_name below.
  $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;
  }

  // Make sure the configuration exists currently in active storage.
  if (!$this->activeConfigStorage
    ->read($full_name)) {
    return FALSE;
  }

  // Load the current config and replace the value, retaining the config
  // hash (which is part of the _core config key's value).
  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();
  }

  // Trigger an event notifying of this change.
  $event = new ConfigRevertEvent($type, $name);
  $this->dispatcher
    ->dispatch(ConfigRevertInterface::REVERT, $event);
  return TRUE;
}