You are here

final protected function SwaggerUIFormatterTrait::buildRenderArray in Swagger UI Field Formatter 8.2

Same name and namespace in other branches
  1. 8.3 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 156

Class

SwaggerUIFormatterTrait
Provides common methods for Swagger UI field formatters.

Namespace

Drupal\swagger_ui_formatter\Plugin\Field\FieldFormatter

Code

protected final function buildRenderArray(FieldItemListInterface $items, FormatterInterface $formatter, FieldDefinitionInterface $field_definition, array $context = []) {
  $element = [];
  $library_path = _swagger_ui_formatter_get_library_path();
  if (!$library_path) {
    $element = [
      '#theme' => 'status_messages',
      '#message_list' => [
        'error' => [
          $this
            ->t('Swagger UI library is missing.'),
        ],
      ],
    ];
  }
  else {

    // Set the right oauth2-redirect.html file path for OAuth2 authentication.
    $oauth2_redirect_url = NULL;
    if (file_exists(DRUPAL_ROOT . $library_path . '/dist/oauth2-redirect.html')) {
      $oauth2_redirect_url = \Drupal::request()
        ->getSchemeAndHttpHost() . $library_path . '/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/swagger_ui_formatter.swagger_ui',
              'swagger_ui_formatter/swagger_ui_formatter.swagger_ui_integration',
            ],
            'drupalSettings' => [
              'swaggerUIFormatter' => [
                "{$field_definition->getName()}-{$delta}" => [
                  'svgDefinition' => _swagger_ui_formatter_get_svg_definition(),
                  'oauth2RedirectUrl' => $oauth2_redirect_url,
                  // For BC, we pass an array here instead of a single value.
                  'swaggerFiles' => [
                    $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'))),
                ],
              ],
            ],
          ],
        ];
      }
    }
  }
  $element = NestedArray::mergeDeepArray([
    $element,
    [
      '#cache' => [
        // If Swagger UI library's location changes render this field again.
        'tags' => [
          SWAGGER_UI_FORMATTER_LIBRARY_PATH_CID,
        ],
      ],
    ],
  ]);
  return $element;
}