View source
<?php
namespace Drupal\services\Controller;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\services\ServiceEndpointInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ServiceEndpointResources extends ControllerBase {
protected $pluginManager;
public function __construct(PluginManagerInterface $plugin_manager) {
$this->pluginManager = $plugin_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.services.service_definition'));
}
public function displayList(ServiceEndpointInterface $service_endpoint = NULL) {
$build = [];
foreach ($this
->getCategoryDefinitions($service_endpoint) as $category => $definitions) {
if (!isset($category)) {
continue;
}
$build[$category] = [
'#type' => 'details',
'#title' => $this
->t('@category', [
'@category' => $category,
]),
'#tree' => TRUE,
];
$rows = [];
foreach ($definitions as $plugin_id => $definition) {
$row = $this
->buildRow($service_endpoint, $definition);
$row['operations']['data'] = [
'#type' => 'dropbutton',
'#links' => $this
->buildOperationLinks($service_endpoint, $plugin_id),
];
$rows[] = $row;
}
$build[$category]['table'] = array(
'#type' => 'table',
'#rows' => $rows,
'#header' => $this
->buildHeader(),
'#empty' => $this
->t('No service definitions exist'),
);
}
return $build;
}
protected function buildHeader() {
return [
'title' => $this
->t('Definition'),
'endpoint' => $this
->t('Endpoint'),
'operations' => $this
->t('Operations'),
];
}
protected function buildRow(ServiceEndpointInterface $service_endpoint, array $definition) {
$row = [];
$row['title']['data'] = [
'#markup' => $definition['title'],
];
$row['endpoint']['data'] = [
'#markup' => $definition['path'],
];
return $row;
}
protected function buildOperationLinks(ServiceEndpointInterface $service_endpoint, $plugin_id) {
$links = [];
$links['configure'] = [
'title' => $this
->t('Enable'),
'url' => Url::fromRoute('entity.service_endpoint_resource.config_form', [
'plugin_id' => $plugin_id,
'service_endpoint' => $service_endpoint
->id(),
]),
'attributes' => $this
->getModalAttributes(),
];
if ($service_resource = $service_endpoint
->loadResourceProvider($plugin_id)) {
if ($service_resource
->hasLinkTemplate('delete-form')) {
$links['disable'] = [
'title' => $this
->t('Disable'),
'url' => $service_resource
->toUrl('delete-form'),
'attributes' => $this
->getModalAttributes(),
];
}
$links['configure']['title'] = $this
->t('Configure');
}
return $links;
}
protected function getCategoryDefinitions() {
$definitions = [];
foreach ($this->pluginManager
->getDefinitions() as $plugin_id => $definition) {
if (!isset($definition['category'])) {
continue;
}
$category = $definition['category']
->render();
$definitions[$category][$plugin_id] = $definition;
}
ksort($definitions);
return $definitions;
}
protected function getModalAttributes() {
return [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 800,
]),
];
}
}