<?php

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\node\NodeInterface;
use Drupal\Core\Access\AccessResult;

class SwapiLinkTask extends ControllerBase {
  public function exampleTabContent($node) {
    $nid = $node;
    $api = \Drupal::service('mymodule.custom_services');
    $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
    $bundle = $node->bundle();
    $swapi_id = $node->get('field_swapi_id')->getValue();
    $uri = 'https://swapi.dev/api/' . $bundle . '/' . $swapi_id[0]['value'];
    $result = $api->getResponse($uri);

    foreach ($result as $key => $value) {
      if (is_array($value)) {
        $build = $build . "\"$key\"" . ": [<br>";
        foreach ($value as $value2) {
          $build = $build . " \"$value2\"" . ",<br>";
        }
        $build = $build . "],<br>";
      } else {
        $build = $build . "\"$key\": \"$value\",<br>";
      }
    }
    $build = substr($build,0,-5);
    return ['#markup' => $build];
  }
}
