Jump to content

page reference field to json


ngrmm
 Share

Recommended Posts

I know there are some modules (pageQueryBoss, GraphQL, …) this. But non of them outputs exactly how i would like my json to be.
So i tried to do it manually:

	$list = $pages->get(1234)->children;
	$list_array = array();
	
	foreach($list as $listItem) {
		$list_array[] = array(
			'title' => $listItem->title, 
			'id' => $listItem->id,
			'tags' => array(
				'ids' => $listItem->page-reference-field 
			)
		);
	}
	
	$list_json = json_encode($list_array, true);
	echo $list_json;
	

How do I loop through a page-reference-field inside an array?

 

UPDATE:
i think, thats the way to do it

	$list = $pages->get(1234)->children;
	$list_array = array();
	
	foreach($list as $listItem) {
		$listItemTags = array();
		foreach($listItem->tags as $tag) {
			$listItemTags[] = array(
				'id' => $tag->id,
				'title' => $tag->title,
			);
		}
		$list_array[] = array(
			'title' => $listItem->title, 
			'id' => $listItem->id,
			'tags' => $listItemTags
		);
	}
	
	$list_json = json_encode($list_array, true);
	echo $list_json;

 

Link to comment
Share on other sites

Hi @ngrmm

$list = $pages->get(1234)->children;
$toJSON = $list->explode(function ($item) {
	return [
		'title' => $item->title,
		'id'    => $item->id,
		'tags'  => [
			'ids' => $item->page_field->explode('id')
		]
	];
});

wireEncodeJSON($toJSON);

 

  • Like 1
Link to comment
Share on other sites

7 minutes ago, Zeka said:

Hi @ngrmm

$list = $pages->get(1234)->children;
$toJSON = $list->explode(function ($item) {
	return [
		'title' => $item->title,
		'id'    => $item->id,
		'tags'  => [
			'ids' => $item->page_field->explode('id')
		]
	];
});

wireEncodeJSON($toJSON);

 

thx @Zeka

I got it working with a temporary loop inside my foreach-loop (see update on my post). Actually I needed the tags to be arrays themself. But your method looks better. Is there a way to output the tags as arrays themself with your method?

Link to comment
Share on other sites

$list = $pages->get(1234)->children;
$toJSON = $list->explode(function ($item) {
	return [
		'title' => $item->title,
		'id'    => $item->id,
		'tags'  => $item->tags->explode(['id', 'name', 'title'])
	];
});

 

  • Like 1
Link to comment
Share on other sites

14 minutes ago, Zeka said:
$list = $pages->get(1234)->children;
$toJSON = $list->explode(function ($item) {
	return [
		'title' => $item->title,
		'id'    => $item->id,
		'tags'  => $item->tags->explode(['id', 'name', 'title'])
	];
});

 

wow much easier than i thought. And what is the correct syntax to go more levels deeper? Let's say one my tags has also page-reference-field itself.
 

Link to comment
Share on other sites

3 minutes ago, ngrmm said:

And what is the correct syntax to go more levels deeper?

$toJSON = $list->explode(function ($item) {
	return [
		'title' => $item->title,
		'id'    => $item->id,
		'tags'  => $item->tags->explode(function($subitem) use($item) {
			return [
				'id' => $subitem->id,
				'title' => $subitem->title,
				'parent_title' => $item->title,
				'sub_page' => $subitem->page_field->explode(['id', 'title'])
			]
		})
	];
});

 

You can get more info about explode, each and implode function here 

https://processwire.com/talk/topic/5098-new-wirearray-api-additions-on-dev/

  • Like 1
Link to comment
Share on other sites

10 minutes ago, Zeka said:

@ngrmm

By the way, you can try to use $pages->findRaw https://processwire.com/api/ref/pages/find-raw/, like 

wireEncodeJSON($pages->findRaw('template=some-template, parent=1234,', ['id', 'title', 'tags' => ['id', 'title']);

 

Actually this is perfect, as I don't need all the page data. But unfortunately it outputs the page ID as key, what don't want to have.
Maybe there will be some options in the future to change this default behaviour.

Thanks @Zeka anyway, the other solution works perfectly!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...