3

So I have a working setup where some code pulls in the latest Wikipedia extract of a page. I then use Entities to handle a node creation, adding the contents of the extract to the longtext value of the generic body field:

$body = array();
$url = 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles='.urlencode($item['name']);
$result = @file_get_contents($url);
if ($result){
  // Oh yes!
  $decoded = json_decode($result, true);
  foreach ($decoded['query']['pages'] as $page){
    if (array_key_exists('extract', $page)){
      $body[] = t($page['extract']);
    }
  }
} else {
  // Boo, nothing found.
}

// Doesn't exist, we have to make it first. (Using Entities)
$node = entity_create('node', array('type' => 'article', 'uid' => 1));
$entity = entity_metadata_wrapper('node', $node); // $node->nid is probably better here
$entity->title->set($item['name']);
if ($body){
  $entity->body->set(array('value' => $body[0]));
}
$entity->save();
// Woo got our nid!
$nid = $entity->getIdentifier();

The problem is, I can't seem to control the output of the body at all. It renders surrounded with paragraph tags, using html line breaks for each paragraph. I want to surround each paragraph with paragraph tags. I've tried using preg_match to catch newlines (\r\n) but anything I replace it with just gets rendered exactly - making paragraph tags show up in the extract text when viewing the node.

What's the best method of doing some formatting to the extract?

1 Answer 1

4

You just need to specify a format for your text value:

$entity->body->set(array('value' => $body[0], 'format' => 'full_html'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.