View source
<?php
namespace Drupal\wsdata\Form;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\wsdata\WSDataService;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WSCallTestForm extends EntityForm {
protected $wsdata;
public function __construct(WSDataService $wsdata) {
$this->wsdata = $wsdata;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('wsdata'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form_state
->disableCache();
$element = $form_state
->getTriggeringElement();
$form['wscallwrapper'] = [
'#type' => 'fieldset',
'#title' => $this
->t('WSCall'),
'title' => [
'#prefix' => '<b>' . $this
->t('Title (Machine name):') . '</b>',
'#markup' => $this->entity
->label() . ' (' . $this->entity
->id() . ')',
],
];
$elements = $this->wsdata
->wscallForm([], $this->entity
->id());
$form['replacements'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Tokens'),
];
$form['replacements']['wscall_form'] = $elements;
unset($form['replacements']['wscall_form']['wscall']);
if ($element and $element['#id'] == 'call') {
$form['responsewrapper'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Response'),
'response' => [
'#prefix' => '<pre>',
'#markup' => Xss::filter($form_state
->getValue('wscall_response')),
'#suffix' => '</pre>',
],
];
}
return $form;
}
public function call(array $form, FormStateInterface $form_state) {
$form_state
->setRebuild(TRUE);
$form_state
->disableCache();
$replacements = [];
foreach ($this->entity
->getReplacements() as $replacement) {
$replacements[$replacement] = $form_state
->getValue($replacement);
}
$response = $this->wsdata
->call($this->entity
->id(), NULL, $replacements, $form_state
->getValue('data'), [], $form_state
->getValue('returnToken'));
$form_state
->setValue('wscall_response', is_array($response) ? print_r($response, TRUE) : $response);
}
public function buildForm(array $form, FormStateInterface $form_state) {
if (!$form_state
->has('entity_form_initialized')) {
$this
->init($form_state);
}
$form = $this
->form($form, $form_state);
$actions = $this
->actionsElement($form, $form_state);
if (!empty($actions)) {
$form['actions'] = $actions;
}
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Call'),
'#id' => 'call',
'#submit' => [
'::call',
],
];
return $actions;
}
}