function RestExampleClientCalls::update

Same name in other branches
  1. 4.0.x modules/rest_example/src/RestExampleClientCalls.php \Drupal\rest_example\RestExampleClientCalls::update()

Update (PATCH) a node on the remote server.

You are encouraged to read the code and the comments in RestExampleClientCalls::create() first.

Parameters

array $node: Contains the data of the node we want to create.

Return value

\Symfony\Component\HttpFoundation\Response A HTTP response.

Throws

\InvalidArgumentException

\GuzzleHttp\Exception\GuzzleException

File

modules/rest_example/src/RestExampleClientCalls.php, line 175

Class

RestExampleClientCalls
Here we interact with the remote service.

Namespace

Drupal\rest_example

Code

public function update(array $node) {
    if (empty($this->remoteUrl)) {
        return new Response('The remote URL has not been setup.', 500);
    }
    $request_body = json_encode([
        '_links' => [
            'type' => [
                'href' => $this->remoteUrl . '/rest/type/node/' . $node['type'],
            ],
        ],
        'title' => [
            0 => [
                'value' => $node['title'],
            ],
        ],
    ]);
    $response = $this->client
        ->request('POST', $this->remoteUrl . '/node/' . $node['nid'], [
        'headers' => $this->clientHeaders,
        'auth' => $this->clientAuth,
        'body' => $request_body,
    ]);
    if ($response->getStatusCode() != 204) {
        return new Response('An error occurred while patching the node.', 500);
    }
}