View source
<?php
namespace Drupal\http_client_manager\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\http_client_manager\HttpClientManagerFactoryInterface;
use Drupal\http_client_manager\HttpServiceApiHandlerInterface;
use GuzzleHttp\Command\Guzzle\Parameter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class HttpConfigRequestForm extends EntityForm {
protected $request;
protected $httpServicesApi;
protected $httpClientFactory;
public function __construct(RequestStack $requestStack, HttpServiceApiHandlerInterface $http_services_api, HttpClientManagerFactoryInterface $http_client_manager_factory) {
$this->request = $requestStack
->getCurrentRequest();
$this->httpServicesApi = $http_services_api;
$this->httpClientFactory = $http_client_manager_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('http_client_manager.http_services_api'), $container
->get('http_client_manager.factory'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$serviceApi = $this->request
->get('serviceApi');
$commandName = $this->request
->get('commandName');
$http_config_request = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $http_config_request
->label(),
'#description' => $this
->t("Label for the Http Config Request."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $http_config_request
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\http_client_manager\\Entity\\HttpConfigRequest::load',
],
'#disabled' => !$http_config_request
->isNew(),
];
$form['service_api'] = [
'#type' => 'value',
'#value' => $serviceApi,
];
$form['command_name'] = [
'#type' => 'value',
'#value' => $commandName,
];
$form['parameters'] = [
'#type' => 'fieldset',
'#title' => $this
->t('parameters'),
'#tree' => TRUE,
];
$client = $this->httpClientFactory
->get($serviceApi);
$parameters = $http_config_request
->get('parameters');
foreach ($client
->getCommand($commandName)
->getParams() as $param) {
$name = $param
->getName();
$form['parameters'][$name] = [
'#command_param' => $param,
'#title' => $this
->t($name),
'#type' => 'textarea',
'#rows' => 1,
'#required' => $param
->isRequired(),
'#default_value' => isset($parameters[$name]) ? $parameters[$name] : $param
->getDefault(),
'#description' => $param
->getDescription() ? $this
->t($param
->getDescription()) : '',
];
if ($param
->has('enum')) {
$enum = $param
->getEnum();
$options = [
'' => $this
->t('- Select -'),
] + array_combine($enum, $enum);
$form['parameters'][$name]['#type'] = 'select';
$form['parameters'][$name]['#options'] = $options;
continue;
}
$type = $param
->getType();
switch ($type) {
case 'bool':
case 'boolean':
$form['parameters'][$name]['#type'] = 'checkbox';
$form['parameters'][$name]['#value_callback'] = [
$this,
'booleanValue',
];
break;
case 'integer':
case 'number':
$form['parameters'][$name]['#type'] = 'number';
$form['parameters'][$name]['#value_callback'] = [
$this,
'integerValue',
];
break;
case 'float':
case 'decimal':
$form['parameters'][$name]['#type'] = 'number';
$form['parameters'][$name]['#step'] = 'any';
$form['parameters'][$name]['#value_callback'] = [
$this,
'floatValue',
];
break;
case 'array':
case 'object':
$form['parameters'][$name]['#type'] = 'textarea';
$form['parameters'][$name]['#rows'] = 12;
$form['parameters'][$name]['#description'] .= '<div class="json-help">' . $this
->t('Example') . ': <small><pre>' . $this
->getJsonHelp($param) . '</pre></small></div>';
$form['parameters'][$name]['#attributes']['placeholder'] = $this
->t('Enter data in JSON format.');
$form['parameters'][$name]['#value_callback'] = [
$this,
'jsonString',
];
$form['parameters'][$name]['#element_validate'][] = [
$this,
'validateJson',
];
break;
}
}
$form['token_help'] = [
'#theme' => 'token_tree_link',
];
return $form;
}
public function integerValue(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && $input !== NULL) {
return (int) $input;
}
return NULL;
}
public function floatValue(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && $input !== NULL) {
return (double) $input;
}
return NULL;
}
public function booleanValue(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE || $input === NULL) {
return (bool) $input;
}
return $element['#default_value'];
}
public function validateJson(array &$element, FormStateInterface $form_state) {
$value = $element['#value'];
if (empty($value)) {
return;
}
if (is_string($value)) {
$form_state
->setError($element);
return;
}
$param = $element['#command_param'];
$type = $param
->getType();
switch ($type) {
case 'array':
$is_valid = is_array($value);
break;
case 'object':
$is_valid = is_object($value) || array_values($value) !== $value;
break;
default:
$is_valid = FALSE;
}
if (!$is_valid) {
$message = $this
->t('Field @title has to be an @type. @var_type provided.', [
'@title' => $element['#title'],
'@type' => $type,
'@var_type' => ucfirst(gettype($value)),
]);
$form_state
->setError($element, $message);
if (!is_string($value)) {
$element['#value'] = json_encode($value, JSON_PRETTY_PRINT);
}
}
}
public function jsonString(array &$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && $input !== NULL) {
$input = trim($input);
if (empty($input)) {
return [];
}
$param = $element['#command_param'];
$assoc = $param
->getType() == 'array' ? JSON_OBJECT_AS_ARRAY : JSON_FORCE_OBJECT;
$item = json_decode($input, $assoc);
if (json_last_error() !== JSON_ERROR_NONE) {
$message = $this
->t('There was an error parsing your JSON: @error', [
'@error' => json_last_error_msg(),
]);
$this
->messenger()
->addError($message);
return $input;
}
return $item;
}
if (empty($element['#default_value'])) {
return NULL;
}
$item = json_encode($element['#default_value'], JSON_PRETTY_PRINT);
if (json_last_error() !== JSON_ERROR_NONE) {
$this
->messenger()
->addError(json_last_error_msg());
return $input;
}
return $item;
}
protected function getJsonHelp(Parameter $param) {
if ($param
->getType() == 'array') {
$properties = $param
->getItems()
->getProperties();
}
else {
$properties = $param
->getProperties();
}
$array = [];
foreach ($properties as $name => $parameter) {
switch ($parameter
->getType()) {
case 'string':
$sample = 'Lorem ipsum...';
break;
case 'integer':
case 'number':
$sample = 123;
break;
case 'float':
case 'decimal':
$sample = 123.01;
break;
case 'bool':
case 'boolean':
$sample = TRUE;
break;
case 'array':
$sample = [];
break;
case 'object':
$sample = new \stdClass();
break;
default:
$sample = '...';
}
$array[$name] = $sample;
}
if ($param
->getType() == 'array') {
$array = [
$array,
];
}
return json_encode($array, JSON_PRETTY_PRINT);
}
public function save(array $form, FormStateInterface $form_state) {
$http_config_request = $this->entity;
$status = $http_config_request
->save();
$messenger = $this
->messenger();
switch ($status) {
case SAVED_NEW:
$messenger
->addStatus($this
->t('Created the %label Http Config Request.', [
'%label' => $http_config_request
->label(),
]));
break;
default:
$messenger
->addStatus($this
->t('Saved the %label Http Config Request.', [
'%label' => $http_config_request
->label(),
]));
}
$form_state
->setRedirectUrl($http_config_request
->toUrl('collection'));
}
}