You are here

class SchemaOverviewController in GraphQL 8.3

Hierarchy

Expanded class hierarchy of SchemaOverviewController

File

src/Controller/SchemaOverviewController.php, line 11

Namespace

Drupal\graphql\Controller
View source
class SchemaOverviewController implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The schema plugin manager service.
   *
   * @var \Drupal\graphql\Plugin\SchemaPluginManager
   */
  protected $schemaManager;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('module_handler'), $container
      ->get('plugin.manager.graphql.schema'));
  }

  /**
   * SchemaOverviewController constructor.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler srevice.
   * @param \Drupal\graphql\Plugin\SchemaPluginManager $schemaManager
   *   The schema plugin manager service.
   */
  public function __construct(ModuleHandlerInterface $moduleHandler, SchemaPluginManager $schemaManager) {
    $this->schemaManager = $schemaManager;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * Renders a table of available schemas.
   *
   * @return array
   *   The renderable array for the overview table.
   */
  public function listSchemas() {
    $table = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Schema'),
        $this
          ->t('Provider'),
        $this
          ->t('Operations'),
      ],
      '#attributes' => [
        'id' => 'graphql-schemas',
      ],
    ];
    foreach ($this->schemaManager
      ->getDefinitions() as $key => $definition) {
      $table["schema:{$key}"]['name'] = [
        '#plain_text' => $definition['name'],
      ];
      $table["schema:{$key}"]['provider'] = [
        '#plain_text' => $this->moduleHandler
          ->getName($definition['provider']),
      ];
      $table["schema:{$key}"]['operations'] = $this
        ->buildOperations($key, $definition);
    }
    return $table;
  }

  /**
   * Builds a renderable list of operation links for a schema.
   *
   * @param string $pluginId
   *   The plugin id.
   * @param array $pluginDefinition
   *   The plugin definition array.
   *
   * @return array
   *   A renderable array of operation links.
   */
  protected function buildOperations($pluginId, array $pluginDefinition) {
    $build = [
      '#type' => 'operations',
      '#links' => $this
        ->getOperations($pluginId, $pluginDefinition),
    ];
    return $build;
  }

  /**
   * Provides an array of information to build a list of operation links.
   *
   * @param $pluginId
   *   The plugin id.
   * @param $pluginDefinition
   *   The plugin definition array.
   *
   * @return array
   *   An associative array of operation link data for this list, keyed by
   *   operation name, containing the following key-value pairs:
   *     - title: The localized title of the operation.
   *     - url: An instance of \Drupal\Core\Url for the operation URL.
   *     - weight: The weight of this operation.
   */
  protected function getOperations($pluginId, $pluginDefinition) {
    $operations = $this->moduleHandler
      ->invokeAll('graphql_schema_operations', [
      $pluginId,
      $pluginDefinition,
    ]);
    $this->moduleHandler
      ->alter('graphql_schema_operations', $operations, $pluginId, $pluginDefinition);
    uasort($operations, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
    return $operations;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SchemaOverviewController::$moduleHandler protected property The module handler service.
SchemaOverviewController::$schemaManager protected property The schema plugin manager service.
SchemaOverviewController::buildOperations protected function Builds a renderable list of operation links for a schema.
SchemaOverviewController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
SchemaOverviewController::getOperations protected function Provides an array of information to build a list of operation links.
SchemaOverviewController::listSchemas public function Renders a table of available schemas.
SchemaOverviewController::__construct public function SchemaOverviewController constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.