Select Field populated with json suddenly not working

I have a select field that gets its values from a json file created by a route. It’s suddenly stopped working on a site i built a while back. When i load the page the progress bar takes ages to get across. PHP 7.1.

It’s 2.5.12. If i hit the json file directly in the browser, thats working fine, but for some reason the select fields are refusing to read it. The select is empty and it wont let me save the page.

Any idea?

The route looks like this…

  array(
    'pattern' => 'pagelist.json',
    'action'  => function() {
      header('Content-type: application/json; charset=utf-8');
      $pages = site()->index()->visible()->filterBy('template', 'not in', ['error', 'blogarticle']);
      $json = array();

      foreach ($pages as $page) {
          $json[$page->autoid()->value()] = $page->title()->value();

      echo json_encode($json);
    }
  ),

And the blue print…

  cblocklinkurl:
      label:             Button One Link
      type:              select
      options:           url
      width:             1/2
      help:              The button link value
      url:               pagelist.json

I think it’s something to do with the modules plugin. Can any confirm it works alright these days? It is 3 years old after all.

@lukasbestle Is there any reason it wouldnt work on newer PHP versions?

There’s a problem with your code, it should be:

foreach ($pages as $page) {
     $json[$page->autoid()->value()] = $page->title()->value();   
}

echo json_encode($json);

i.e. with the echo statement outside the loop.

Ah that was my sloppy atempting at pasting in to the forum i removed a couple of confidential lines. The code in project is ok.

I’ve just noticed it works fine on the staging site (real server, running CentOS) - it just doesnt work locally on Valet for some reason. I need it too tho.