You are here

function apigee_edge_entity_view in Apigee Edge 8

Implements hook_entity_view().

File

./apigee_edge.module, line 378
Copyright 2018 Google Inc.

Code

function apigee_edge_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($entity instanceof AppInterface) {

    // Add some required assets to an app's full entity view mode.
    $build['#attached']['library'][] = 'apigee_edge/apigee_edge.components';
    $build['#attached']['library'][] = 'apigee_edge/apigee_edge.app_view';
    if (\Drupal::moduleHandler()
      ->moduleExists('apigee_edge_teams')) {
      if ($team = \Drupal::routeMatch()
        ->getParameter('team')) {
        $team_app_name = $team
          ->getName();
      }
    }
    if ($user = \Drupal::routeMatch()
      ->getParameter('user')) {
      $build['#attached']['drupalSettings']['currentUser'] = $user
        ->id();
    }
    if ($display
      ->getComponent('credentials')) {

      /** @var \Drupal\apigee_edge\Entity\AppInterface $entity */
      $defaults = [
        '#cache' => [
          'contexts' => $entity
            ->getCacheContexts(),
          'tags' => $entity
            ->getCacheTags(),
        ],
      ];
      $build['credentials'] = [
        '#type' => 'container',
      ];
      $index = 0;
      foreach ($entity
        ->getCredentials() as $credential) {
        $build['credentials'][$credential
          ->getStatus()][] = [
          '#type' => 'app_credential',
          '#credential' => $credential,
          '#app_name' => $entity
            ->getName(),
          '#team_app_name' => isset($team_app_name) ? $team_app_name : '',
          '#app' => $entity,
          '#attributes' => [
            'class' => 'items--inline',
            'data-app' => $entity
              ->getName(),
            'data-team' => isset($team_app_name) ? $team_app_name : '',
            'data-app-container-index' => $index,
          ],
        ] + $defaults;
        $index++;
      }

      // Hide revoked credentials in a collapsible section.
      if (!empty($build['credentials'][AppCredentialInterface::STATUS_REVOKED])) {
        $revoked_credentials = $build['credentials'][AppCredentialInterface::STATUS_REVOKED];
        $build['credentials'][AppCredentialInterface::STATUS_REVOKED] = [
          '#type' => 'details',
          '#title' => t('Revoked keys (@count)', [
            '@count' => count($revoked_credentials),
          ]),
          '#weight' => 100,
          'credentials' => $revoked_credentials,
        ];
      }
    }

    // Add link to add keys.
    if ($entity
      ->access('add_api_key') && $entity
      ->hasLinkTemplate('add-api-key-form')) {
      $build['add_keys'] = Link::fromTextAndUrl(t('Add key'), $entity
        ->toUrl('add-api-key-form', [
        'attributes' => [
          'data-dialog-type' => 'modal',
          'data-dialog-options' => json_encode([
            'width' => 500,
            'height' => 250,
            'draggable' => FALSE,
            'autoResize' => FALSE,
          ]),
          'class' => [
            'use-ajax',
            'button',
          ],
        ],
      ]))
        ->toRenderable();
    }
  }
  if ($display
    ->getComponent('warnings')) {

    /** @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker */
    $app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker');
    $warnings = array_filter($app_warnings_checker
      ->getWarnings($entity));
    if (count($warnings)) {
      $build['warnings'] = [
        '#theme' => 'status_messages',
        '#message_list' => [
          'warning' => $warnings,
        ],
      ];
    }
  }
}