You are here

public function RestExampleClientEdit::submitForm in Examples for Developers 3.x

Throws

\GuzzleHttp\Exception\GuzzleException

Overrides FormInterface::submitForm

File

modules/rest_example/src/Form/RestExampleClientEdit.php, line 112

Class

RestExampleClientEdit
Edit or create a new node on a remote Drupal site.

Namespace

Drupal\rest_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $node_id = $form_state
    ->get('node_id');
  $node_type = $form_state
    ->get('node_type');
  $form_values = $form_state
    ->getValues();
  $node = [
    'nid' => $node_id,
    'title' => $form_values['title'],
    'type' => $node_type,
  ];
  if (is_null($node_id)) {
    $this->client
      ->create($node);
  }
  else {
    $this->client
      ->update($node);
  }
  $this
    ->messenger()
    ->addStatus($this
    ->t('Node was successfully created'));
  $form_state
    ->setRedirect('rest_example.client_actions_index');
}