You are here

public function SmartDocsFormatter::viewElements in Apigee API Catalog 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/SmartDocsFormatter.php \Drupal\apigee_api_catalog\Plugin\Field\FieldFormatter\SmartDocsFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/SmartDocsFormatter.php, line 115

Class

SmartDocsFormatter
Plugin implementation of the SmartDocs OpenAPI spec formatter.

Namespace

Drupal\apigee_api_catalog\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $entity = $items
    ->getEntity();
  $entity_type = $entity
    ->getEntityTypeId();

  // The list of OpenAPI specs to pass to SmartDocs Angular app.
  $openapi_files = [];

  /** @var \Drupal\file\Entity\File $file */
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {
    $openapi_files[] = [
      // Get the URL of the file on server.
      'fileUrl' => $file
        ->createFileUrl(FALSE),
      'fileExtension' => pathinfo($file
        ->getFilename(), PATHINFO_EXTENSION),
    ];
  }
  $elements['#attached'] = [
    'library' => [
      'apigee_api_catalog/apigee_api_catalog.js_yaml',
      'apigee_api_catalog/apigee_api_catalog.smartdocs_integration',
      'apigee_api_catalog/apigee_api_catalog.smartdocs',
    ],
  ];
  $elements['#attached']['drupalSettings']['smartdocsFieldFormatter'][$this->fieldDefinition
    ->getName()] = [
    'openApiFiles' => $openapi_files,
    'entityId' => $entity
      ->id(),
    'entityType' => $entity_type,
  ];
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      // Create tag on page for SmartDocs Angular app.
      '#type' => 'html_tag',
      '#tag' => 'app-root',
      '#value' => $this
        ->t('Loading...'),
    ];
  }
  return $elements;
}