ApiTokenController.php in API Tokens 8        
                          
                  
                        
  
  
  
  
File
  src/Controller/ApiTokenController.php
  
    View source  
  <?php
namespace Drupal\api_tokens\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\api_tokens\ApiTokenManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ApiTokenController extends ControllerBase {
  
  protected $apiTokenManager;
  
  public function __construct(ApiTokenManagerInterface $api_token_manager) {
    $this->apiTokenManager = $api_token_manager;
  }
  
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.api_token'));
  }
  
  public function adminOverview() {
    $build['table'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Token'),
        $this
          ->t('Synopsis'),
        $this
          ->t('Provider'),
      ],
      '#empty' => $this
        ->t('There are no API tokens registered.'),
    ];
    foreach ($this->apiTokenManager
      ->getSortedDefinitions() as $id => $definition) {
      $row =& $build['table'][$id];
      $row['token'] = [
        '#type' => 'item',
        '#title' => $definition['label'],
        '#description' => $definition['description'],
        '#description_display' => 'after',
      ];
      $row['synopsis'] = $this
        ->buildSynopsis($id);
      $row['provider'] = [
        '#type' => 'item',
        '#title' => $definition['category'],
        '#description' => $this
          ->t('Machine name: @provider', [
          '@provider' => $definition['provider'],
        ]),
        '#description_display' => 'after',
      ];
    }
    return $build;
  }
  
  protected function buildSynopsis($id) {
    $plugin = $this->apiTokenManager
      ->createInstance($id);
    $parts = [];
    foreach ($plugin
      ->reflector()
      ->getParameters() as $param) {
      $name = $param
        ->getName();
      if (!$param
        ->isOptional()) {
        $name = "<strong>{$name}</strong>";
      }
      $parts[] = "<em>{$name}</em>";
    }
    $params = $parts ? '[' . implode(', ', $parts) . ']' : '';
    $build = [
      '#type' => 'html_tag',
      '#tag' => 'code',
      '#value' => "[api:{$id}{$params}/]",
    ];
    return $build;
  }
}