You are here

class SettingsSaveEventSubscriber 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

Class acts on config save event.

Hierarchy

  • class \Drupal\content_lock\EventSubscriber\SettingsSaveEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of SettingsSaveEventSubscriber

1 string reference to 'SettingsSaveEventSubscriber'
content_lock.services.yml in ./content_lock.services.yml
content_lock.services.yml
1 service uses SettingsSaveEventSubscriber
settings_save_event_subscriber in ./content_lock.services.yml
\Drupal\content_lock\EventSubscriber\SettingsSaveEventSubscriber

File

src/EventSubscriber/SettingsSaveEventSubscriber.php, line 13

Namespace

Drupal\content_lock\EventSubscriber
View source
class SettingsSaveEventSubscriber implements EventSubscriberInterface {
  protected $entityTypeManager;

  /**
   * SettingsSaveEventSubscriber constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * On config save.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The save event.
   */
  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();
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::SAVE][] = [
      'onSave',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SettingsSaveEventSubscriber::$entityTypeManager protected property
SettingsSaveEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
SettingsSaveEventSubscriber::onSave public function On config save.
SettingsSaveEventSubscriber::__construct public function SettingsSaveEventSubscriber constructor.