ConfigSubscriber.php in Autosave Form 8
File
src/EventSubscriber/ConfigSubscriber.php
View source
<?php
namespace Drupal\autosave_form\EventSubscriber;
use Drupal\autosave_form\Storage\AutosaveEntityFormStorageInterface;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigSubscriber implements EventSubscriberInterface {
protected $autosaveEntityFormStorage;
public function __construct(AutosaveEntityFormStorageInterface $autosave_entity_form_storage) {
$this->autosaveEntityFormStorage = $autosave_entity_form_storage;
}
public function onConfigSave(ConfigCrudEvent $event) {
$saved_config = $event
->getConfig();
$name = $saved_config
->getName();
$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;
}
}
}
}
public function onConfigDelete(ConfigCrudEvent $event) {
$deleted_config = $event
->getConfig();
$name = $deleted_config
->getName();
$purge_autosave_states = strpos($name, 'field.field.') === 0 || strpos($name, 'field.storage.') === 0 || strpos($name, 'core.entity_form_display.') === 0 || strpos($name, 'user.role.') === 0;
if ($purge_autosave_states) {
$this->autosaveEntityFormStorage
->purgeAutosavedEntitiesStates();
}
}
public static function getSubscribedEvents() {
$events[ConfigEvents::SAVE][] = [
'onConfigSave',
];
$events[ConfigEvents::DELETE][] = [
'onConfigDelete',
];
return $events;
}
}