HttpConfigRequestListBuilder.php in HTTP Client Manager 8
File
src/HttpConfigRequestListBuilder.php
View source
<?php
namespace Drupal\http_client_manager;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class HttpConfigRequestListBuilder extends ConfigEntityListBuilder {
protected $request;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, RequestStack $request_stack) {
parent::__construct($entity_type, $storage);
$this->request = $request_stack
->getCurrentRequest();
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager')
->getStorage($entity_type
->id()), $container
->get('request_stack'));
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery()
->condition('service_api', $this->request
->get('serviceApi'))
->condition('command_name', $this->request
->get('commandName'))
->sort($this->entityType
->getKey('id'));
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
public function buildHeader() {
$header['label'] = $this
->t('Http Config Request');
$header['id'] = $this
->t('Machine name');
$header['params'] = $this
->t('Parameters');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity
->label();
$row['id'] = $entity
->id();
$params = [];
foreach ($entity
->getParameters() as $key => $value) {
$params[$key] = $this
->t('@key: @value', [
'@key' => $key,
'@value' => is_array($value) ? json_encode($value, JSON_PRETTY_PRINT) : $value,
]);
}
$row['params']['data'] = [
'#theme' => 'item_list',
'#items' => $params,
];
return $row + parent::buildRow($entity);
}
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity
->access('execute') && $entity
->hasLinkTemplate('execute')) {
$operations['execute'] = array(
'title' => $this
->t('Execute'),
'weight' => 101,
'url' => Url::fromRoute('entity.http_config_request.execute', [
'http_config_request' => $entity
->id(),
'serviceApi' => $entity
->get('service_api'),
'commandName' => $entity
->get('command_name'),
]),
);
}
return $operations;
}
}