You are here

public function SettingsSaveEventSubscriber::onSave in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/SettingsSaveEventSubscriber.php \Drupal\content_lock\EventSubscriber\SettingsSaveEventSubscriber::onSave()

On config save.

Parameters

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

File

src/EventSubscriber/SettingsSaveEventSubscriber.php, line 33

Class

SettingsSaveEventSubscriber
Class acts on config save event.

Namespace

Drupal\content_lock\EventSubscriber

Code

public function onSave(ConfigCrudEvent $event) {
  if ($event
    ->getConfig()
    ->getName() == 'content_lock.settings') {
    foreach (array_filter($event
      ->getConfig()
      ->get('types')) as $type => $value) {

      // Skip if the entity type does not exist.
      if (!$this->entityTypeManager
        ->getDefinition($type, FALSE)) {
        continue;
      }

      // Create an action config for all activated entity types.
      $action = $this->entityTypeManager
        ->getStorage('action')
        ->loadByProperties([
        'plugin' => 'entity:break_lock:' . $type,
      ]);
      if (empty($action)) {
        $action = $this->entityTypeManager
          ->getStorage('action')
          ->create([
          'id' => $type . '_break_lock_action',
          'label' => 'Break lock ' . $type,
          'plugin' => 'entity:break_lock:' . $type,
          'type' => $type,
        ]);
        $action
          ->save();
      }
    }
  }
}