public function BlogapiCommunicator::getRecentPosts in Blog API 8
Method that returns recent posts.
Parameters
$ct: Content type machine name.
$user: Drupal username.
$pass: Drupal password.
$nr: Number of recent posts to return.
bool $bodies: TRUE if the node contents are to be returned also.
Return value
array|object The operation response.
File
- src/
BlogapiCommunicator.php, line 793
Class
- BlogapiCommunicator
- Class BlogapiCommunicator.
Namespace
Drupal\blogapiCode
public function getRecentPosts($ct, $user, $pass, $nr, $bodies = TRUE) {
$user = $this
->authenticate($user, $pass, TRUE);
// Check user authentication.
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);
}
// Run the query for recent nodes.
$query = \Drupal::entityQuery('node')
->condition('type', $ct)
->sort('created', 'DESC')
->range(0, $nr);
// Find only the users content if the users role can not edit any content.
if (!$user
->hasPermission('manage any content blogapi')) {
$query
->condition('uid', $user
->id());
}
$result = $query
->execute();
$response = [];
// Format the response for each node.
foreach ($result as $nid) {
$node = $this->entityTypeManager
->getStorage('node')
->load($nid);
$response[] = $this
->formatXml($node, $bodies);
}
return $response;
}