final protected function SwaggerUIFormatterTrait::buildRenderArray in Swagger UI Field Formatter 8.3
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/SwaggerUIFormatterTrait.php \Drupal\swagger_ui_formatter\Plugin\Field\FieldFormatter\SwaggerUIFormatterTrait::buildRenderArray()
Builds a render array from a field.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: Field items.
\Drupal\Core\Field\FormatterInterface $formatter: The current field formatter.
\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition in the current field formatter.
array $context: Additional context for field rendering.
Return value
array Field value as a render array.
2 calls to SwaggerUIFormatterTrait::buildRenderArray()
- SwaggerUIFileFormatter::viewElements in src/
Plugin/ Field/ FieldFormatter/ SwaggerUIFileFormatter.php - Builds a renderable array for a field value.
- SwaggerUILinkFormatter::viewElements in src/
Plugin/ Field/ FieldFormatter/ SwaggerUILinkFormatter.php - Builds a renderable array for a field value.
File
- src/
Plugin/ Field/ FieldFormatter/ SwaggerUIFormatterTrait.php, line 159
Class
- SwaggerUIFormatterTrait
- Provides common methods for Swagger UI field formatters.
Namespace
Drupal\swagger_ui_formatter\Plugin\Field\FieldFormatterCode
protected final function buildRenderArray(FieldItemListInterface $items, FormatterInterface $formatter, FieldDefinitionInterface $field_definition, array $context = []) : array {
$element = [];
$library_name = 'swagger_ui_formatter.swagger_ui_integration';
/** @var \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery */
$library_discovery = \Drupal::service('library.discovery');
/** @var \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscoveryInterface $swagger_ui_library_discovery */
$swagger_ui_library_discovery = \Drupal::service('swagger_ui_formatter.swagger_ui_library_discovery');
// The Swagger UI library integration is only registered if the Swagger UI
// library directory and version is correct.
if ($library_discovery
->getLibraryByName('swagger_ui_formatter', $library_name) === FALSE) {
$element = [
'#theme' => 'status_messages',
'#message_list' => [
'error' => [
$this
->t('The Swagger UI library is missing, incorrectly defined or not supported.'),
],
],
];
}
else {
$library_dir = $swagger_ui_library_discovery
->libraryDirectory();
// Set the oauth2-redirect.html file path for OAuth2 authentication.
$oauth2_redirect_url = \Drupal::request()
->getSchemeAndHttpHost() . '/' . $library_dir . '/dist/oauth2-redirect.html';
foreach ($items as $delta => $item) {
$element[$delta] = [
'#delta' => $delta,
'#field_name' => $field_definition
->getName(),
];
// It's the user's responsibility to set up field settings correctly
// and use this field formatter with valid Swagger files. Although, it
// could happen that a URL could not be generated from a field value.
$swagger_file_url = $this
->getSwaggerFileUrlFromField($item, $context + [
'field_items' => $items,
]);
if ($swagger_file_url === NULL) {
$element[$delta] += [
'#theme' => 'status_messages',
'#message_list' => [
'error' => [
$this
->t('Could not create URL to file.'),
],
],
];
}
else {
$element[$delta] += [
'#theme' => 'swagger_ui_field_item',
'#attached' => [
'library' => [
'swagger_ui_formatter/' . $library_name,
],
'drupalSettings' => [
'swaggerUIFormatter' => [
"{$field_definition->getName()}-{$delta}" => [
'oauth2RedirectUrl' => $oauth2_redirect_url,
'swaggerFile' => $swagger_file_url,
'validator' => $formatter
->getSetting('validator'),
'validatorUrl' => $formatter
->getSetting('validator_url'),
'docExpansion' => $formatter
->getSetting('doc_expansion'),
'showTopBar' => $formatter
->getSetting('show_top_bar'),
'sortTagsByName' => $formatter
->getSetting('sort_tags_by_name'),
'supportedSubmitMethods' => array_keys(array_filter($formatter
->getSetting('supported_submit_methods'))),
],
],
],
],
];
}
}
}
if ($swagger_ui_library_discovery instanceof CacheableDependencyInterface) {
$cacheable_metadata = CacheableMetadata::createFromRenderArray($element)
->merge(CacheableMetadata::createFromObject($swagger_ui_library_discovery));
$cacheable_metadata
->applyTo($element);
}
return $element;
}