About Pages to JSON

Adds method to several wire classes to output as JSON

Category Development Tools
Modules that have more to do with assisting development of a site rather than managing its content.
Release StateBeta
Close to stable, but users are advised to be cautious and test thoroughly.*
Authorstikki
Module Version0.0.4
Class NamePages2JSON
Compatibility2.5, 2.6, 2.7, 3.0
Date AddedFebruary 14, 2015
Last UpdatedMarch 1, 2017
Recommended ByNew recommendations may take up to 1 day to appear.

Details

What does this do?

Simply adds method toJSON() to Page and PageArray elements, is capable to converting image and file arrays to URL containers, travels recursively all objects before outputs actual JSON.

Instructions

How to use?

Simply install this module, configure data you want it to export in JSON result (similar to Ryan's ServicePages, thanks btw used some of ur code here). in templates or anywhere you need JSON output, call: $page->toJSON(); or $pages->toJSON();

Live example with template: search.php

Code:
/*****************************************************************Do search *****************************************************************/ $term = $sanitizer->text($input->term); $results = array(); if($term) { $input->whitelist('term', $term); $term = $sanitizer->selectorValue($term); $limit = (int)$input->limit; $limit = $limit ? $limit : 50; $limit = $sanitizer->selectorValue($limit); $selector = "title|categories|content*=$term, limit=$limit, template=default|product"; // Prevent admin pages. if($user->isLoggedin()) $selector .= ", has_parent!=2"; // Find pages that match the selector $results = $pages->find($selector); } /***************************************************************** Output *****************************************************************/ header("Content-type: application/json"); echo $results->toJSON(); exit();

Customizing values:
Code:
if(wire('config')->ajax) {wire()->addHookAfter('Pages2JSON::getValue', function($event) { $value = $event->arguments(0); if(!is_object($value)) return; $page = wire('page'); if($page->template == 'thumbs' && $value->className == 'Pageimage') $event->return = $value->size(28,28)->url; }); }

Here i swap URL of all Pageimage objects in JSON response to match thumbs.

README

Pages2JSON

Module for ProcessWire that adds capability to output pages as JSON

Changelog

Lisa Simpson (0.0.4)

  • Removed template tab and added section under Basic tab for template spesific JSON attribute output selection.
  • Crawler is now targeting Pages and WireArrays based on instance type, not className like it was previously. This should give more flexibility to developers around different types of objects that are derived from either of.
  • Added only public method it most likely ever will have: encode($value, $options = 0). Usage example: $modules('Pages2JSON')->encode($toJSON);
  • Added new hookable method: getFields($page). Gives capability to inject and manipulate runtime fields to be crawled. Useful especially if you need to inject field that dosen't exists, but you still need crawler to notice it.

Montgomery Burns (0.0.3)

  • Kind of forgot to release this, so this remains ghots release that never happen. And to be honest, no one really likes Mr. Burns anyways.

Homer Simpson (0.0.2)

  • Allows user define global data output in module options + individual data based on template.
  • Added forum user Manol's feature request regarding PageImage and PageFile fields.

Bart Simpson (0.0.1)

  • Initial release

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.