You are here

public function ConfigSubscriber::onConfigSave in Autosave Form 8

Purges all autosave states in case a form-related config is saved.

Deletes all autosaved states if an important property of a form related config is changed. We cannot know if some kind of an inline reference has been used and its display has been changed, which is why we purge the whole autosave storage. Otherwise a much more complex detection mechanism will be needed. We though try to prevent the purging in case a change on an property is made, which doesn't influence the form functionality - e.g. if the label of a field is changed then we don't have to purge the autosave states.

Parameters

\Drupal\Core\Config\ConfigCrudEvent $event: The configuration event.

File

src/EventSubscriber/ConfigSubscriber.php, line 47

Class

ConfigSubscriber
Purges autosave states on configuration changes.

Namespace

Drupal\autosave_form\EventSubscriber

Code

public function onConfigSave(ConfigCrudEvent $event) {
  $saved_config = $event
    ->getConfig();
  $name = $saved_config
    ->getName();

  // We try to prevent purging autosave states unnecessarily by defining
  // properties, which are allowed to change and don't have an influence on
  // the form functionality.
  $allowed_changes = [];
  $purge_autosave_states = TRUE;
  if (strpos($name, 'field.field.') === 0) {
    $allowed_changes = [
      'dependencies',
      'label',
      'description',
      'required',
      'default_value',
      'default_value_callback',
    ];
  }
  elseif (strpos($name, 'field.storage.') === 0) {
    $allowed_changes = [
      'dependencies',
      'module',
      'indexes',
      'persist_with_no_fields',
      'custom_storage',
    ];
  }
  elseif (strpos($name, 'core.entity_form_display.') === 0) {
    $allowed_changes = [
      'hidden',
    ];
  }
  elseif (strpos($name, 'user.role.') === 0) {
    $allowed_changes = [
      'label',
      'weight',
    ];
  }
  else {
    $purge_autosave_states = FALSE;
  }
  if ($purge_autosave_states) {
    $property_names = array_diff(array_keys($saved_config
      ->getRawData()), $allowed_changes);
    foreach ($property_names as $property_name) {
      if ($event
        ->isChanged($property_name)) {
        $this->autosaveEntityFormStorage
          ->purgeAutosavedEntitiesStates();
        break;
      }
    }
  }
}