bits-please Posted January 24, 2016 Share Posted January 24, 2016 Hi, first of all big thanks to the community and to the developers for this great product. I have a module that adds a hook before page render to act as rest endpoints. I added the hook with: $this->addHookBefore('PageRender::renderPage', $this, 'endpoint'); Everything works fine except the returned json has the ouput unformatted. I'm creating the json array with: $page->setOutputFormatting(true); $json = array(); foreach ($page->fields as $field) { $json[$field->name] = $page->get($field->name); } $json['id'] = $page->id; $json['name'] = $page->name; $json['created_user'] = $page->createdUser->name; $parentEvent->return = $json; A page with a title "Title between quotes" will have its json title property as title: ""Title between quotes"" Am I missing something or is this the expected behaviour? I also tried searching for a Page method that returns the field formatted, but it seems only getUnformatted exists. Thank you in advance. Link to comment Share on other sites More sharing options...
BitPoet Posted January 24, 2016 Share Posted January 24, 2016 The html entity encoding happens through the Textformatter assigned to the title field. That's something output formatting does, so you need to turn output formatting off to get the raw value with quotes from the field. There's an in-depth article in the wiki on that topic that explains the behaviour quite well. Link to comment Share on other sites More sharing options...
DaveP Posted January 24, 2016 Share Posted January 24, 2016 I might be missing something here, but do you not need json_encode($json) in there somewhere? Link to comment Share on other sites More sharing options...
bits-please Posted January 24, 2016 Author Share Posted January 24, 2016 BitPoet, thank you. I guess I misunderstood what output formatting actually did! DaveP, not really. This is the expected behaviour. The hook only acts on the rendering if the request is ajax or if the Accept header value is application/json. The above snippet acts on pages rendering during an ajax/json request that have $options['json'], so that the result is stored in a variable instead of rendered. That's why I don't encode the array. Hope that was clear. Thanks for the help. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now