You are here

function template_preprocess_app_credential in Apigee Edge 8

Prepares variables for app_credential templates.

Default template: app-credential.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An associative array containing the credential information. Properties used:

    • #credential: A \Apigee\Edge\Api\Management\Entity\AppCredential object. A developer app credential.
    • #app_name: string. App name.
    • #team_app_name: string. Team app name.
  • attributes: HTML attributes for the containing element.

File

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

Code

function template_preprocess_app_credential(array &$variables) {

  /** @var \Apigee\Edge\Api\Management\Entity\AppCredentialInterface $credential */
  $credential = $variables['elements']['#credential'];

  /** @var \Drupal\apigee_edge\Entity\AppInterface $app */
  $app = $variables['elements']['#app'];

  /** @var \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter */
  $dateFormatter = Drupal::service('date.formatter');
  $serializer = new AppCredentialSerializer();

  // Convert app entity to an array.
  $normalized = (array) $serializer
    ->normalize($credential);
  $properties_in_primary = [
    'consumerKey' => [
      'label' => t('Consumer Key'),
      'value_type' => 'plain',
    ],
    'consumerSecret' => [
      'label' => t('Consumer Secret'),
      'value_type' => 'plain',
    ],
    'issuedAt' => [
      'label' => t('Issued'),
      'value_type' => 'date',
    ],
    'expiresAt' => [
      'label' => t('Expires'),
      'value_type' => 'date',
    ],
    'status' => [
      'label' => t('Key Status'),
      'value_type' => 'status',
    ],
  ];
  $secret_properties = [
    'consumerKey',
    'consumerSecret',
  ];
  $variables['primary_wrapper'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'wrapper--primary app-details-wrapper',
    ],
  ];
  $index = 0;
  foreach ($properties_in_primary as $property => $def) {
    $variables['primary_wrapper'][$property] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => 'item-property',
      ],
    ];
    $variables['primary_wrapper'][$property]['label'] = [
      '#type' => 'label',
      '#title' => $def['label'],
      '#title_display' => 'before',
    ];
    $value = array_key_exists($property, $normalized) ? $normalized[$property] : NULL;
    if ($def['value_type'] == 'date') {

      // TODO Should we make format configurable?

      /** @var \DateTimeInterface $value */
      if ($value !== -1) {
        $time_diff = \Drupal::time()
          ->getRequestTime() - intval($value / 1000);
        if ($time_diff > 0) {
          $value = t('@time ago', [
            '@time' => $dateFormatter
              ->formatTimeDiffSince(intval($value / 1000)),
          ]);
        }
        else {
          $value = t('@time hence', [
            '@time' => $dateFormatter
              ->formatTimeDiffUntil(intval($value / 1000)),
          ]);
        }
      }
      else {
        $value = t('Never');
      }
    }

    // Below, $value is expected to be a string in some places, and it might be
    // TranslatableMarkup or a string. If it is not a string, then in some
    // cases warnings will be generated. This way it is always a string,
    // removing ambiguity.
    $value = (string) $value;
    if (in_array($property, $secret_properties)) {

      // Render the consumerKey and the consumerSecret as secret fields.
      $variables['primary_wrapper'][$property]['value'] = [
        '#type' => 'apigee_secret',
      ];
    }
    elseif ($def['value_type'] === 'status') {

      // Check if expired.
      if ($normalized['expiresAt'] !== -1 && \Drupal::time()
        ->getRequestTime() - (int) ($normalized['expiresAt'] / 1000) > 0) {
        $value = t('Expired');
      }
      $variables['primary_wrapper'][$property]['value'] = [
        '#type' => 'status_property',
        '#value' => $value,
        '#indicator_status' => $value === AppCredentialInterface::STATUS_APPROVED ? StatusPropertyElement::INDICATOR_STATUS_OK : StatusPropertyElement::INDICATOR_STATUS_ERROR,
      ];
    }
    else {
      $variables['primary_wrapper'][$property]['value'] = [
        '#markup' => Xss::filter($value),
      ];
    }
    $index++;
  }
  $variables['secondary_wrapper'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'wrapper--secondary',
    ],
    'title' => [
      '#type' => 'label',
      '#title_display' => 'before',
      '#title' => \Drupal::entityTypeManager()
        ->getDefinition('api_product')
        ->getPluralLabel(),
    ],
    'list' => [
      '#type' => 'app_credential_product_list',
      '#credential_products' => $credential
        ->getApiProducts(),
    ],
  ];

  // Helpful $content variable for templates.
  $variables['content'] = $normalized;

  // Add operations.
  $variables['operations'] = [
    '#type' => 'operations',
  ];
  if ($credential
    ->getStatus() === AppCredentialInterface::STATUS_APPROVED && $app
    ->access('revoke_api_key') && $app
    ->hasLinkTemplate('revoke-api-key-form')) {
    $variables['operations']['#links']['revoke'] = [
      'title' => t('Revoke'),
      'url' => $app
        ->toUrl('revoke-api-key-form')
        ->setRouteParameter('consumer_key', $credential
        ->getConsumerKey()),
    ];
  }
  if ($app
    ->access('delete_api_key') && $app
    ->hasLinkTemplate('delete-api-key-form')) {
    $variables['operations']['#links']['delete'] = [
      'title' => t('Delete'),
      'url' => $app
        ->toUrl('delete-api-key-form')
        ->setRouteParameter('consumer_key', $credential
        ->getConsumerKey()),
    ];
  }
}