You are here

public static function OpenApiUi::preRenderOpenApiUi in OpenAPI UI 8

Provides markup for associating a tray trigger with a tray element.

A tray is a responsive container that wraps renderable content. Trays present content well on small and large screens alike.

Parameters

array $element: A renderable array.

Return value

array A renderable array.

File

src/Element/OpenApiUi.php, line 85

Class

OpenApiUi
Defines a render element for OpenApi Doc display librarys.

Namespace

Drupal\openapi_ui\Element

Code

public static function preRenderOpenApiUi(array $element) {
  $messenger = \Drupal::service('messenger');
  $plugin = $element['#openapi_ui_plugin'];

  // If the plugin id was passed, get the plugin instance.
  if (is_string($plugin) && !empty($plugin)) {
    $ui_plugin_manager = \Drupal::service('plugin.manager.openapi_ui.ui');
    $plugin = $ui_plugin_manager
      ->createInstance($plugin);
    $element['#openapi_ui_plugin'] = $plugin;
  }
  if (!$plugin instanceof OpenApiUiInterface) {
    $messenger
      ->addError(t('Unknown OpenAPI UI plugin being used.'));
    return $element;
  }
  $schema = $element['#openapi_schema'];

  // If a callback was passed, execute it to get a string, array, or url.
  if (is_callable($schema)) {
    $schema = call_user_func($schema, $element);
    $element['#openapi_schema'] = $schema;
  }

  // If the schema is a string, convert it to a URL object.
  if (is_string($schema)) {
    $schema = Url::fromUri($schema);
    $element['#openapi_schema'] = $schema;
  }

  // If schema is not a complient array or a URL, quit rendering.
  if (!(is_array($schema) || $schema instanceof Url)) {
    $messenger
      ->addError(t('Invalid schema source provided.'));
    return $element;
  }
  $element['#tree'] = TRUE;
  $element['ui'] = $plugin
    ->build($element);
  return $element;
}