public function BlogapiCommunicator::newPost in Blog API 8
Callback for creating a new node.
Parameters
$ct: Content type machine name.
$username: Drupal username.
$pass: Drupal password.
$data: The node contents.
Return value
bool|object|string Either an error or the create node ID.
File
- src/
BlogapiCommunicator.php, line 531
Class
- BlogapiCommunicator
- Class BlogapiCommunicator.
Namespace
Drupal\blogapiCode
public function newPost($ct, $username, $pass, $data) {
// Check user authentication.
$user = $this
->authenticate($username, $pass, TRUE);
if (!$user) {
return $this
->returnXmlError(self::BLOGAPI_XML_ERROR_AUTH);
}
// Check if content type is manageable with blogapi.
if (!$this
->validateBlogId($ct)) {
return $this
->returnXmlError(self::BLOGAPI_XML_ERROR_CT);
}
if (!$user
->hasPermission('create ' . $ct . ' content')) {
return $this
->returnXmlError(self::BLOGAPI_XML_ERROR_NODE_CREATE);
}
$body_field = $this->blogapiConfig
->get('body_' . $ct);
$comment_field = $this->blogapiConfig
->get('comment_' . $ct);
$values = [
'type' => $ct,
'title' => $data['title'],
$body_field => [
'value' => html_entity_decode($data['body']),
'format' => $this
->getValidTextFormat($data, $user),
],
$comment_field => [
'status' => isset($data['comments']) ? $data['comments'] : $this
->getDefaultCommentSetting($ct),
],
'uid' => $user
->id(),
];
$node_manager = $this->entityTypeManager
->getStorage('node');
$node = $node_manager
->create($values);
$node
->setPublished($data['publish']);
$node
->save();
$id = $node
->id();
if (is_numeric($id)) {
return (string) $id;
}
return $this
->returnXmlError(self::BLOGAPI_XML_ERROR_NODE_NOT_FOUND);
}