You are here

public function ExternalSettingsJsController::extlinkJsFile in External Links 8

Creates response for external settings JS file.

Return value

\Symfony\Component\HttpFoundation\Response The response object.

1 string reference to 'ExternalSettingsJsController::extlinkJsFile'
extlink.routing.yml in ./extlink.routing.yml
extlink.routing.yml

File

src/Controller/ExternalSettingsJsController.php, line 34

Class

ExternalSettingsJsController
Provides an external endpoint from which extlink settings JS can be loaded.

Namespace

Drupal\extlink\Controller

Code

public function extlinkJsFile() {
  $config = $this->configFactory
    ->get('extlink.settings');
  $settings = _extlink_get_settings_from_config($config);

  // Need to double backslashes to escape the JS string literal.
  $settings_json = str_replace('\\', '\\\\', Json::encode($settings));
  $js = <<<EOT
(function (drupalSettings) {

  'use strict';

  drupalSettings.data = drupalSettings.data || {};

  drupalSettings.data.extlink = JSON.parse('{<span class="php-variable">$settings_json</span>}');

})(drupalSettings);
EOT;
  return CacheableResponse::create($js, 200, [
    'Content-Type' => 'application/javascript',
  ])
    ->addCacheableDependency($config);
}