Jump to content

Pages2JSON


Stikki

Recommended Posts

Is it possible to filter / extend page fields with function param like

// only selected fields
$myPages->toJSON(array('title', 'image');

// use "default" fields and add / remove fields
$myPages->toJSON(array('title' => false, 'created' => true, 'addCustomField');

I know it could be done by a hook, set / modify fields by function param would be nice.

Link to comment
Share on other sites

Howdy,

Custom fields were meant to be injected by hooks, there is currently no other way.

Currently there are no filtering mechanism and it's purely renderer as it was meant to be, filtering is done based on template field selection, could be inline also. If you have ready pull request, lemme take a look.

Link to comment
Share on other sites

  • 2 weeks later...

Hi @Stikki,

I'll take a closer look into you module code to see how filter feature could be added.

Would it possible to add the JSON tab to user template too? I can't define the fields should be added to users if converted to JSON by your module?

Link to comment
Share on other sites

  • 3 weeks later...

I do some tests with custom code because I need a simple way to set (recursive) fields to view for different page types and fields.

    public function ___WireToArray($obj, $filter = array()) {
        $values = array();
        foreach ($obj AS $item) {
            $array = array();
            foreach ($filter AS $key => $val) {
                if (is_array($val)) {
                    $array[$key] = WireToArray($item->$key, $val);
                } else {
                    $string = (string)$item->$val;
                    $array[$val] = strpos($string, '|') ? explode('|', $string) : $string;
                }
            }
            $values[] = $array;
        }
        return $values;
    }

I built a module which used that way:

$userFilter = array('id', 'name', 'email', 'roles' => array('id', 'name'));
$pages->find('template=user')->toJSON($userFilter, JSON_PRETTY_PRINT)

Output

[
    {
        "id": "40",
        "name": "guest",
        "email": "",
        "roles": [
            {
                "id": "37",
                "name": "guest"
            }
        ]
    },
    {
        "id": "41",
        "name": "admin",
        "email": "andre.hoeg@gmail.com",
        "roles": [
            {
                "id": "37",
                "name": "guest"
            },
            {
                "id": "38",
                "name": "superuser"
            }
        ]
    }
]

 

Example with images

$wa = $pages->find('template=basic-page');
$filter = array(
    'id',
    'name', 
    'images' => array('url', 'width', 'height'),
    'files' => array('url', 'name', 'filesize'),
    'pageref',
);

Output

[
    {
        "id": "1016",
        "name": "test1",
        "images": [
            {
                "url": "\/site\/assets\/files\/1016\/dscn2585.jpg",
                "width": "4608",
                "height": "3456"
            },
            {
                "url": "\/site\/assets\/files\/1016\/dscn2823.jpg",
                "width": "4608",
                "height": "3456"
            }
        ],
        "files": [
            {
                "url": "\/site\/assets\/files\/1016\/aufstellung_und_taktische_aufgaben.pdf",
                "name": "aufstellung_und_taktische_aufgaben.pdf",
                "filesize": "68628"
            },
            {
                "url": "\/site\/assets\/files\/1016\/1und1_leistungsbeschreibung_mobilfunk.pdf",
                "name": "1und1_leistungsbeschreibung_mobilfunk.pdf",
                "filesize": "207816"
            }
        ],
        "pageref": [
            "1017",
            "27"
        ]
    },
    {
        "id": "1017",
        "name": "test2",
        "images": [
            {
                "url": "\/site\/assets\/files\/1017\/dscn2813.jpg",
                "width": "4608",
                "height": "3456"
            }
        ],
        "files": [],
        "pageref": ""
    }
]

Because Pageimages and Pagefiles are based on WireArray it's possible to get just images as JSON

$img = $page->images;
$imgFilter = array('url', 'name', 'filesize', 'width');
$img->toJSON($imgFilter);

Output

[
    {
        "url": "\/site\/assets\/files\/1016\/dscn2585.jpg",
        "name": "dscn2585.jpg",
        "filesize": "4085826",
        "width": "4608"
    },
    {
        "url": "\/site\/assets\/files\/1016\/dscn2823.jpg",
        "name": "dscn2823.jpg",
        "filesize": "3225062",
        "width": "4608"
    }
]

 

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

So you want to add inline filter mode, similar to what we have in GUI currently? As far as i understood.

This: 

$pages->find('template=user')->toJSON($userFilter, JSON_PRETTY_PRINT)

conflicts with header:

	protected function renderJSON($event)
	{
		$options = $event->arguments(0) ? $event->arguments(0) : 0;
		
		$event->return = $this->encode($event->object, $options);
	}	

Could do type check, so i won't be breaking exiting code. Or it could be second parameter? I'll see what i can do with system templates.

Link to comment
Share on other sites

Hi @stikki,

thanks for take a look into it. Additional / instead of GUI field selection I use a array to filter the fields to convert / output. At the moment I have some other topics and can't work & play with PW. My code seems to work for my needs and I like the multidimensional filter array to filter sub-pages and wirearray / pagearray / image fields too.

So if you could implement it a nice and easy way it would be fine. I like to have one module for one job and not some similar modules ?

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
×
×
  • Create New...