I have the following code ` class PopularContentParselyBlock extends BlockBase {

  * {@inheritdoc}

 public function build() {
 $client = new \GuzzleHttp\Client();
 $res = $client->request('GET', 'http://www.APIURL.com');
 $decoded = json_decode($res->getBody(),TRUE);
 foreach($decoded['data'] as $article) {
    $url = Url::fromUri( $article['url']);
    $endpoint = Link::fromTextAndUrl($article['title'], $url);

  return $endpoint->toRenderable();


When it prints out though all I get us Object. What am I doing wrong?

  • How are you printing it? Ideally it should be in a template but because Twig understands render arrays I'm assuming not? – Clive Feb 1 '16 at 19:44
  • This is the end of the class. Sorry I forgot to add it to start. } return [ '#markup' => $endpoint, ]; } – StevenPatz Feb 1 '16 at 19:58

#markup is used for content that has already been rendered. As the caller seems to be expecting a render array, you can just return it directly:

return $endpoint->toRenderable();
| improve this answer | |
  • That yields this error: Call to a member function toRenderable() on string – StevenPatz Feb 1 '16 at 20:23
  • Hmm. I have this in a foreach loop and if I don't concatenate items onto $endpoint it works. – StevenPatz Feb 1 '16 at 20:29
  • That error doesn't really make sense in this narrow context, maybe you can edit the question and add the full code for this function? And what it is e.g. Controller? – Clive Feb 1 '16 at 20:32
  • It's a block so in a Plugin. Code is updated. That works. But there are 5 urls that need created – StevenPatz Feb 1 '16 at 20:42
  • This was the closest to what I needed to do to get this to work. Turns out that making $endpoint an array and then looping through that array in the twig template produced what we needed. – StevenPatz Feb 2 '16 at 15:19

You have to put the links in some kind of a render array, for example:

$build['links'] = array(
  '#theme' => 'links',
  '#links' => $links,
return $build;
| improve this answer | |
  • After a meeting this afternoon I realized I probably need to just have this array of links sent off to a twig template where a foreach in there would output what I need. Going to try your suggestion though. – StevenPatz Feb 1 '16 at 23:31
  • Look at the documentation, you need a sub array of href and title, no need for the link function, the template is doing this: api.drupal.org/api/drupal/… – 4k4 Feb 2 '16 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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