You are here

class EventSubscriber in Drupal 10

Same name in this branch
  1. 10 core/modules/config/tests/config_transformer_test/src/EventSubscriber.php \Drupal\config_transformer_test\EventSubscriber
  2. 10 core/modules/config/tests/config_import_test/src/EventSubscriber.php \Drupal\config_import_test\EventSubscriber
  3. 10 core/modules/config/tests/config_collection_install_test/src/EventSubscriber.php \Drupal\config_collection_install_test\EventSubscriber
  4. 10 core/modules/config/tests/config_events_test/src/EventSubscriber.php \Drupal\config_events_test\EventSubscriber
  5. 10 core/modules/system/tests/modules/module_install_class_loader_test2/src/EventSubscriber.php \Drupal\module_install_class_loader_test2\EventSubscriber
  6. 10 core/modules/system/tests/modules/module_install_class_loader_test1/src/EventSubscriber.php \Drupal\module_install_class_loader_test1\EventSubscriber
Same name and namespace in other branches
  1. 8 core/modules/config/tests/config_transformer_test/src/EventSubscriber.php \Drupal\config_transformer_test\EventSubscriber
  2. 9 core/modules/config/tests/config_transformer_test/src/EventSubscriber.php \Drupal\config_transformer_test\EventSubscriber

Class EventSubscriber.

The transformations here are for testing purposes only and do not constitute a well-behaved config storage transformation.

Hierarchy

  • class \Drupal\config_transformer_test\EventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EventSubscriber

1 string reference to 'EventSubscriber'
config_transformer_test.services.yml in core/modules/config/tests/config_transformer_test/config_transformer_test.services.yml
core/modules/config/tests/config_transformer_test/config_transformer_test.services.yml
1 service uses EventSubscriber
config_transformer_test.event_subscriber in core/modules/config/tests/config_transformer_test/config_transformer_test.services.yml
Drupal\config_transformer_test\EventSubscriber

File

core/modules/config/tests/config_transformer_test/src/EventSubscriber.php, line 17

Namespace

Drupal\config_transformer_test
View source
class EventSubscriber implements EventSubscriberInterface {

  /**
   * The active config storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $active;

  /**
   * The sync config storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $sync;

  /**
   * The Drupal state.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * EventSubscriber constructor.
   *
   * @param \Drupal\Core\Config\StorageInterface $active
   *   The active config storage.
   * @param \Drupal\Core\Config\StorageInterface $sync
   *   The sync config storage.
   * @param \Drupal\Core\State\StateInterface $state
   *   The Drupal state.
   */
  public function __construct(StorageInterface $active, StorageInterface $sync, StateInterface $state) {
    $this->active = $active;
    $this->sync = $sync;
    $this->state = $state;
  }

  /**
   * The storage is transformed for importing.
   *
   * In this transformation we ignore the site name from the sync storage and
   * set it always to the currently active site name with an additional string
   * so that there will always be something to import.
   * Do not do this outside of tests.
   *
   * @param \Drupal\Core\Config\StorageTransformEvent $event
   *   The config storage transform event.
   */
  public function onImportTransform(StorageTransformEvent $event) {
    $storage = $event
      ->getStorage();
    $site = $storage
      ->read('system.site');

    // Only change something if the sync storage has data.
    if (!empty($site)) {

      // Add "Arrr" to the site name. Because pirates!
      // The site name which is in the sync directory will be ignored.
      $current = $this->active
        ->read('system.site');
      $site['name'] = $current['name'] . ' Arrr';
      $storage
        ->write('system.site', $site);
    }
  }

  /**
   * The storage is transformed for exporting.
   *
   * In this transformation we ignore the site slogan from the site if the sync
   * storage has it. Just export it again with an additional string so that
   * there will always be something new exported.
   * Do not do this outside of tests.
   *
   * @param \Drupal\Core\Config\StorageTransformEvent $event
   *   The config storage transform event.
   */
  public function onExportTransform(StorageTransformEvent $event) {
    $sync = $this->sync
      ->read('system.site');

    // Only change something if the sync storage has data.
    if (!empty($sync)) {
      $storage = $event
        ->getStorage();
      $site = $storage
        ->read('system.site');

      // Add "Arrr" to the site slogan. Because pirates!
      // The active slogan will be ignored.
      $site['slogan'] = $sync['slogan'] . ' Arrr';
      $site['mail'] = $this->state
        ->get('config_transform_test_mail', '');
      $storage
        ->write('system.site', $site);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    $events[ConfigEvents::STORAGE_TRANSFORM_IMPORT][] = [
      'onImportTransform',
    ];
    $events[ConfigEvents::STORAGE_TRANSFORM_EXPORT][] = [
      'onExportTransform',
    ];
    return $events;
  }

}

Members