0

I'm trying to create a controller to render a swagger documentation using the module swagger_ui_formatter that provide a formatter to the file field that render swagger files (yml, yaml, json) using javascript libraries to show the documentation like swagger website do. Then i copy my yml file in my module directory and in my controller i can load this file.

  public function buildApiDocumentation() {

    $file_system = \Drupal::service('file_system');

    $searchFiles = '/^.*\.(yml)$/';
    $directory = dirname(__DIR__,2).'/docs';

    $file = $file_system->scanDirectory($directory, $searchFiles, ['recurse' => FALSE]);
}

But i don't know how can i render this file using the swagger formatter that the module provide, i can't use the traditional way to use this formatter, creating a content type with a file field and after that create a view and render it, because is a very manual process and i need to fix the api documentation file in a directory that only the dev team can update.

4
  • Why can't you return a render array with a custom theme function to do what you want?
    – Kevin
    Commented Sep 28, 2021 at 23:32
  • A field formatter is used to render an entity field. Without an entity that is rendered, a field formatter cannot be used.
    – avpaderno
    Commented Sep 29, 2021 at 19:43
  • @apaderno interesting... i don't know that, so i need to create a node with this file and using this node i can render because now is an entity, right?
    – Mauricio
    Commented Sep 29, 2021 at 19:59
  • That's the only way to use a field formatter.
    – avpaderno
    Commented Sep 30, 2021 at 13:43

2 Answers 2

0

You can return a BinaryFileRespose from your controller:

use Symfony\Component\HttpFoundation\BinaryFileResponse;

public function buildApiDocumentation() {

  $file_system = \Drupal::service('file_system');

  $searchFiles = '/^.*\.(yml)$/';
  $directory = dirname(__DIR__,2).'/docs';

  $file = $file_system->scanDirectory($directory, $searchFiles, ['recurse' => FALSE]);

  return new BinaryFileResponse($file);
}
3
  • Thanks for your answer, but what i'm looking for is how can i use a formatter to render this file, using the formatter that swagger_ui_formatter module provide. i need to render a yml like swagger website do.
    – Mauricio
    Commented Sep 29, 2021 at 19:41
  • I don't know this module, but if it has a field formatter, it means the data can be saved into a field. You need to create a field of that type, save the value to that field, and set that field to render output using that field formatter.
    – Jaypan
    Commented Sep 29, 2021 at 19:48
  • Thanks @jaypan i'm going to try this creating a node programmatically, set the file and after render it.
    – Mauricio
    Commented Sep 29, 2021 at 20:01
0

as @apaderno mentioned we can't use a formatter that way, then i decided to build my own module to render my swagger documentation using the official swagger ui, and load the file using a javascript directive.

Thanks for helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.