You are here

public function ClientModuleController::index in farmOS 2.x

Returns the ClientModule JS.

Parameters

\Drupal\farm_client\Entity\ClientModuleInterface $client_module: The ClientModule config entity.

Return value

\Symfony\Component\HttpFoundation\Response A response containing the ClientModule JS or a 422 error response.

1 string reference to 'ClientModuleController::index'
farm_client.routing.yml in modules/core/client/farm_client.routing.yml
modules/core/client/farm_client.routing.yml

File

modules/core/client/src/Controller/ClientModuleController.php, line 52

Class

ClientModuleController
Serves JavaScript files for client modules.

Namespace

Drupal\farm_client\Controller

Code

public function index(ClientModuleInterface $client_module) {

  // Get the client module library.
  $library = $client_module
    ->getLibrary();
  [
    $extension,
    $name,
  ] = explode('/', $library, 2);
  $definition = $this->libraryDiscovery
    ->getLibraryByName($extension, $name);

  // Bail if no JS library is provided.
  if (empty($definition['js'])) {
    throw new UnprocessableEntityHttpException('The client module does not have a valid JS library configured.');
  }

  // Try loading the raw JS data.
  $raw = file_get_contents($definition['js'][0]['data']);
  if (empty($raw)) {
    throw new UnprocessableEntityHttpException('The client module JS library could not be loaded.');
  }

  // Return a response with the JS asset.
  $response = new Response();
  $response->headers
    ->set('Content-Type', 'application/javascript; utf-8');
  $response
    ->setContent($raw);
  return $response;
}