You are here

public function XmlRpcExampleClientForm::buildForm in xmlrpc 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

xmlrpc_example/src/Form/XmlRpcExampleClientForm.php, line 37

Class

XmlRpcExampleClientForm
Form demonstrating XML-RPC client.

Namespace

Drupal\xmlrpc_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('xmlrpc_example.server');
  $form['explanation'] = [
    '#markup' => '<div>' . $this
      ->t('This example demonstrates how to make XML-RPC calls with Drupal. <br />The "Request methods" button makes a request to the server and asks for the available list of methods, as a service discovery request. <br/>The "Add integers" and "Subtract integers" use the xmlrpc() function to act as a client, calling the XML-RPC server defined in this same example for some defined methods.<br />An XML-RPC error will result if the result in the addition or subtraction requested is out of bounds defined by the server. These error numbers are defined by the server. <br />The "Add and Subtract" button performs a multicall operation on the XML-RPC server: several requests in a single XML-RPC call.<br />') . '</div>',
  ];

  // We are going to call add and subtract methods, and
  // they work with integer values.
  $form['num1'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Enter an integer'),
    '#default_value' => 2,
    '#size' => 5,
    '#required' => TRUE,
  ];
  $form['num2'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Enter a second integer'),
    '#default_value' => 2,
    '#size' => 5,
    '#required' => TRUE,
  ];

  // Include several buttons, each of them calling a different method.
  // This button submits a XML-RPC call to the system.listMethods method.
  $form['information'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Request methods'),
    '#submit' => [
      [
        $this,
        'submitInformation',
      ],
    ],
  ];

  // This button submits a XML-RPC call to the xmlrpc_example.add method.
  $form['add'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add the integers'),
    '#submit' => [
      [
        $this,
        'submitAdd',
      ],
    ],
  ];

  // This button submits a XML-RPC call to the xmlrpc_example.subtract method.
  $form['subtract'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Subtract the integers'),
    '#submit' => [
      [
        $this,
        'submitSubtract',
      ],
    ],
  ];

  // This button submits a XML-RPC call to the system.multicall method.
  $form['add_subtract'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add and Subtract'),
    '#submit' => [
      [
        $this,
        'submitAddSubtract',
      ],
    ],
  ];
  if ($config
    ->get('alter_enabled')) {
    $form['overridden'] = [
      '#type' => 'markup',
      '#markup' => '<div><strong>' . $this
        ->t('Just a note of warning: The <a href=":url">alter form</a> has been used to disable the limits, so you may want to turn that off if you do not want it.', [
        ':url' => Url::fromRoute('xmlrpc_example.alter')
          ->toString(),
      ]) . '</strong></div>',
    ];
  }
  return $form;
}