You are here

public function BlogapiCommunicator::formatXml in Blog API 8

Helper function to format xml output.

Parameters

$node: A node object.

bool $bodies: TRUE if the node contents are to be returned also.

Return value

array The XML output.

1 call to BlogapiCommunicator::formatXml()
BlogapiCommunicator::getRecentPosts in src/BlogapiCommunicator.php
Method that returns recent posts.

File

src/BlogapiCommunicator.php, line 841

Class

BlogapiCommunicator
Class BlogapiCommunicator.

Namespace

Drupal\blogapi

Code

public function formatXml($node, $bodies = TRUE) {
  $url = $node
    ->url();
  $options = [
    'absolute' => TRUE,
  ];
  $externalUrl = Url::fromUri('internal:' . $url, $options)
    ->toString();
  if ($node
    ->isPublished()) {
    $post_status = 'published';
  }
  else {
    $post_status = 'draft';
  }
  $xmlrpcval = array(
    'userid' => $node
      ->getOwnerId(),
    'dateCreated' => xmlrpc_date($node
      ->getCreatedTime()),
    'title' => $node
      ->getTitle(),
    'postid' => $node
      ->id(),
    'link' => $externalUrl,
    'permaLink' => $externalUrl,
    'post_status' => $post_status,
  );

  // Fetch also the node contents.
  if ($bodies) {
    $content_type = $node
      ->getType();

    // Get the body field.
    $body_field = $this->blogapiConfig
      ->get('body_' . $content_type);
    $body = $node
      ->get($body_field)
      ->getValue();
    if (empty($body)) {
      $body_value = '';
      $format = $this
        ->getDefaultFormat(NULL);
    }
    else {
      $body_value = $body[0]['value'];
      $format = $body[0]['format'];
    }

    // Get the comment field.
    $comment_field = $this->blogapiConfig
      ->get('comment_' . $content_type);
    $node_comment = $node->{$comment_field}
      ->getValue();
    if ($node_comment[0]['status'] == CommentItemInterface::CLOSED) {
      $comment = 2;
    }
    elseif ($node_comment[0]['status'] == CommentItemInterface::OPEN) {
      $comment = 1;
    }
    else {
      $comment = CommentItemInterface::HIDDEN;
    }
    $xmlrpcval['content'] = '<title>' . $node
      ->getTitle() . '</title>' . $body_value;
    $xmlrpcval['description'] = $body_value;
    $xmlrpcval['mt_allow_comments'] = $comment;
    $xmlrpcval['mt_convert_breaks'] = $format;
  }
  return $xmlrpcval;
}