You are here

public function ConfigSubscriber::onSave in JSON:API Extras 8.2

Same name and namespace in other branches
  1. 8.3 src/EventSubscriber/ConfigSubscriber.php \Drupal\jsonapi_extras\EventSubscriber\ConfigSubscriber::onSave()

Rebuilds container and routes when 'path_prefix' configuration is changed.

Parameters

\Drupal\Core\Config\ConfigCrudEvent $event: The Event to process.

File

src/EventSubscriber/ConfigSubscriber.php, line 52

Class

ConfigSubscriber
Associates config cache tag and rebuilds container + routes when necessary.

Namespace

Drupal\jsonapi_extras\EventSubscriber

Code

public function onSave(ConfigCrudEvent $event) {
  $container = \Drupal::getContainer();

  // It is problematic to rebuild the container during the installation.
  $should_process = $container
    ->getParameter('kernel.environment') !== 'install' && $event
    ->getConfig()
    ->getName() === 'jsonapi_extras.settings';
  if ($should_process) {

    // @see \Drupal\jsonapi_extras\JsonapiExtrasServiceProvider::alter()
    if ($event
      ->isChanged('path_prefix')) {
      $this->drupalKernel
        ->rebuildContainer();

      // Because \Drupal\jsonapi\Routing\Routes::routes() uses a container
      // parameter, we need to ensure that it uses the freshly rebuilt
      // container. Due to that, it's impossible to use an injected route
      // builder service, at least until core updates it to support
      // \Drupal\Core\DrupalKernelInterface::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED.
      $this->service = $container
        ->get('router.builder');
      $container
        ->get('router.builder')
        ->rebuild();
    }
  }
}