Jump to content


Photo

PagesToJSON

Module

  • Please log in to reply
3 replies to this topic

#1 evanmcd

evanmcd

    Full Member

  • Members
  • PipPipPip
  • 92 posts
  • 29

Posted 21 July 2012 - 10:43 PM

Hi all,

Using code provided by Ryan here, I've created a module that coverts a PageArray into JSON format.

It's been super useful to me, and I'm hoping it will be for others as well :)

Attached Files



#2 evanmcd

evanmcd

    Full Member

  • Members
  • PipPipPip
  • 92 posts
  • 29

Posted 21 July 2012 - 10:53 PM

Couple things I'm looking to do to improve this module are to:
  • add the ability to pass in the native fields you'd like in the output
  • add an option in the Field Details section for whether or not you want the field included in JSON output
Re the first one, I could use a bit of help trying to figure out how to pass options into a hooked function. I've tried adding an $options param to addHook, like this:

$this->addHook('PageArray::toJSON', $this, 'pagesToJSON', $options);

and calling it like this:

$myPages->toJSON(array('fields' => 'id,name'));

But, when I do I get this error:


Method PageArray::toJSON does not exist or is not callable in this context (in /Projects/TeamScheme/teamscheme_0.5/wire/core/Wire.php line 231)

If someone could point me in the right directions for adding params to hooked functions (and perhaps point to a module that implements something like I want to do wfor item #2) I would appreciate it.

Thanks.

#3 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 658 posts
  • 246

  • LocationBerlin, Germany

Posted 22 July 2012 - 05:43 AM

Could be really helpful for creating a public read API for a PW page sometime.

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,982 posts
  • 3383

  • LocationAtlanta, GA

Posted 22 July 2012 - 07:04 AM

This sounds great Evan, I can't wait to check this one out. To answer your question about how to pass arguments to a hook function:

$myPages->toJSON($a, $b, $c); 

public function toJSON(HookEvent $event) {
  $a = $event->arguments[0];
  $b = $event->arguments[1];
  $c = $event->arguments[2]; 
}

Since you want to pass in an options array as the first argument, you would call your function like in your example, and you could retrieve the argument like this:

$options = $event->arguments[0];






Also tagged with one or more of these keywords: Module

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users