Jump to content

Output formatting before page render


bits-please
 Share

Recommended Posts

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

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

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

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...