You are here

public function BlogapiCommunicator::editPost in Blog API 8

Callback for editing a node.

Parameters

$nid: The node ID.

$username: Drupal username.

$pass: Drupal password.

$data: The node contents.

Return value

object|string A node ID or an error.

File

src/BlogapiCommunicator.php, line 322

Class

BlogapiCommunicator
Class BlogapiCommunicator.

Namespace

Drupal\blogapi

Code

public function editPost($nid, $username, $pass, $data) {

  // Check user authentication.
  $user = $this
    ->authenticate($username, $pass, TRUE);
  if (!$user) {
    return $this
      ->returnXmlError(self::BLOGAPI_XML_ERROR_AUTH);
  }

  // Check is node exists.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load($nid);
  if (!$node) {
    return $this
      ->returnXmlError(self::BLOGAPI_XML_ERROR_NODE_NOT_FOUND, $nid);
  }

  // Check node access for the loaded user.
  if (!$node
    ->access('update', $user)) {
    return $this
      ->returnXmlError(self::BLOGAPI_XML_ERROR_NODE_UPDATE, $nid);
  }
  if (!$this
    ->checkUserNodeAccess($user, $node)) {
    return $this
      ->returnXmlError(self::BLOGAPI_XML_ERROR_NODE_UPDATE, $nid);
  }
  $content_type = $node
    ->getType();
  $body_field = $this->blogapiConfig
    ->get('body_' . $content_type);
  $comment_field = $this->blogapiConfig
    ->get('comment_' . $content_type);
  $body = [
    'value' => html_entity_decode($data['body']),
    'format' => $this
      ->getValidTextFormat($data, $user),
  ];
  $comment = [
    'status' => isset($data['comments']) ? $data['comments'] : $this
      ->getDefaultCommentSetting($content_type),
  ];
  $node
    ->set($body_field, $body);
  $node
    ->set($comment_field, $comment);
  $node
    ->setTitle($data['title']);
  $node
    ->setPublished($data['publish']);
  $node
    ->save();
  return (string) $node
    ->id();
}