Printing PHP to node I got hook in my *.modules file php_invoke_preprocess_node that should print data from JSON according to node type. I need to print it to node content everytime i run it but now it runs only once and print it at the top of the site and then it won't run until I clear cache.   

function php_invoke_preprocess_node(&amp;$variables) {
$node = $variables['elements']['#node'];
//check content type
switch($node-&gt;getType()) {
  case 'nodeType':
    $session = getSession('','');
    $data = ['additonal JSON headers'];
    echo (CallAPI($data, $session)); //return JSON array that i want to print to node but i dont know how
  break;
That was a hell of a problem to solve. I had to rebuild my code. Instead of hooks I took a bit different approach and I tried a solution proposed by @IvanJaros but I wasn't able to get lazy builders to work. I found that in drupal 7 there was a hook_init that was called everytime but it got deprecated in 8.

The answers are EventSubscribers, using them you can assign any function you want to specific event i.e. loading a page. 
 Here's a great tutorial that intruduce you to that feature.

Here's my event code

    class MyEventSubscriber implements EventSubscriberInterface{

        public static function getSubscribedEvents(){
        // Its called when respond transfer ended
        $events[KernelEvents::TERMINATE][] = ['onRespond',28];
        return $events;
    }

  public function onRespond(FilterResponseEvent $event){ 
    $node = \Drupal::routeMatch()-&gt;getParameter('node');
    $user = \Drupal::currentUser();
    $name = $user-&gt;getUsername();

    $data = [];
    if(isset($node)&amp;&amp;isset($name)){
        $nodeType = $node-&gt;getType();
        switch($nodeType){
            case 'nodeType':
                $soap_response = new JSonGetter();
                $data = $soap_response -&gt; getUserData($name);
                echo('&lt;script&gt;var JSONarray = ' . print_r($faktury,1) . ';&lt;/script&gt;');
                break;
        }

    }
    }

}