Score:0

How can i render a file using a formatter programmatically in a controller?

lb flag

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.

Kevin avatar
in flag
Why can't you return a render array with a custom theme function to do what you want?
apaderno avatar
us flag
A field formatter is used to render an entity field. Without an entity that is rendered, a field formatter cannot be used.
Mauricio avatar
lb flag
@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?
apaderno avatar
us flag
That's the only way to use a field formatter.
Score:0
lb flag

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.

Score:0
de flag

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);
}
Mauricio avatar
lb flag
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.
Jaypan avatar
de flag
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.
Mauricio avatar
lb flag
Thanks @jaypan i'm going to try this creating a node programmatically, set the file and after render it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.