in views_views_json_style.theme.inc, use html_entity_decode() when the option for plaintext_output = TRUE and "field_output" = normal.
This way, HTML entities that come from field values are made into true plain text.
We also must ensure that the field output is UTF-8, in order to be compliant with JSON.
$content = $plaintext_output ? strip_tags($field->content) : $field->content;
Becomes:
$content = $plaintext_output ? strip_tags(html_entity_decode($field->content, ENT_QUOTES)) : $field->content;
$content = mb_check_encoding($content, 'UTF-8') ? $content : utf8_encode($content);
This functionality is already in place for view_xml plugin. See /views/theme/views_view.xml_style.theme.inc
We use the ENT_QUOTES option to convert single and double quotes (') and ("") as those are commonly encoded entities by Drupal Core.
Example is with Node titles. As of Drupal 7, all node titles are run through htmlentities(), so characters like apostrophes become '
, when in reality we want an actual apostrophe in our plaintext output.
Patch below.
Comment | File | Size | Author |
---|---|---|---|
#11 | views_datasource-1915492-html-entities-encode-11.patch | 2.87 KB | jcisio |
#7 | views_datasource_views_json-plaintext-decode-html-entities-1915492-7.patch | 1.89 KB | Anonymous (not verified) |
Comments
Comment #1
lookatthosemoose CreditAttribution: lookatthosemoose commentedPatch, attached.
Comment #2
lookatthosemoose CreditAttribution: lookatthosemoose commentedNew patch that include the UTF-8 checking. Disregard last patch.
Comment #3
kriskhaira CreditAttribution: kriskhaira commentedI get the following error while trying to apply the patch.
patch: **** malformed patch at line 13: $content = array();
I then manually applied the changes but don't see any changes in the JSON output.
Comment #4
kriskhaira CreditAttribution: kriskhaira commentedCorrection: the changes work (I had accidentally switched off plaintext_output), but I still needed to make the changes manually.
I'm attaching a patch I created - same changes, just recreated with different line number references.
Comment #5
davidneedhamThe patch in #4 applies successfully, but my json feed continues to output
'
. I've verified that my cache is flushed and the code was changed appropriatelyComment #6
davidneedhamActually, I did exactly the same thing as @ kriskhaira. Once you enable plaintext output, it works perfectly.
Comment #6.0
davidneedhamAddition to code example.
Comment #7
Anonymous (not verified) CreditAttribution: Anonymous commentedUpdated patch to apply properly, in the module itself, not from drupal root.
Comment #8
Jibus CreditAttribution: Jibus commentedPatch #7 resolves the issue ! Thanks ! Hope this will get commited soon :)
Comment #9
anthonylindsay CreditAttribution: anthonylindsay commented#7 looks good to me. Committing to 7.x branch.
Comment #10
anthonylindsay CreditAttribution: anthonylindsay commentedClosing.
Comment #11
jcisio CreditAttribution: jcisio commentedSame issue and same fix in D6.
Comment #13
yannickoo