You are here

public function ReDoc::build in ReDoc for OpenAPI UI 8

Render the tree for an openapi doc library.

The callback is trigged to allow the plugin to define the plugin or library specific rendering which needs to happen in order to construct the element.

Parameters

array $render_element: The render array structure for the element which is triggering the build of this plugin's rendering.

Return value

array The processed render array.

Overrides OpenApiUi::build

File

src/Plugin/openapi_ui/OpenApiUi/ReDoc.php, line 23

Class

ReDoc
Implements openapi_ui plugin for the swagger-ui library.

Namespace

Drupal\openapi_ui_redoc\Plugin\openapi_ui\OpenApiUi

Code

public function build(array $render_element) {
  $schema = $render_element['#openapi_schema'];
  $build = [
    '#type' => 'html_tag',
    '#tag' => 'redoc',
    '#attributes' => [
      'id' => 'redoc-ui',
      'no-auto-auth' => TRUE,
    ],
    '#attached' => [
      'library' => [
        'openapi_ui_redoc/redoc',
      ],
    ],
  ];
  if ($schema instanceof Url) {
    $build['#attributes']['spec-url'] = $schema
      ->toString();
  }
  else {
    $build['#attributes']['spec'] = Json::encode($schema);

    // We need to shim the redoc library to load from the spec attribute.
    $build['#attached']['library'][] = 'openapi_ui_redoc/redoc_attr';
  }
  return $build;
}