Jump to content

Unexpected output with $sanitizer->truncate() when PageFrontEdit is enabled


iank
 Share

Recommended Posts

I'm not sure if this should be considered a bug, a feature, or just something to be aware of, but I came across this behaviour today:

Scenario: 
I have a 'body' field which is a CKEditor textarea field.  I'm using $sanitizer->truncate() to output a truncated summary of the body for use on a parent 'list' page; e.g.:

$sanitizer->truncate($page->body,200);

Everything worked fine.  Longer body content is truncated, shorter (<200) is output in its entirety.

I then enabled the PageFrontEdit module for the site, and allowed the body field to be automatically editable (Option A). Suddenly, all the shorter body fields were being duplicated in the output.  For example, when the body field contains just this:

<p>More information will be coming soon...</p>

The output from the above $sanitizer->truncate() call was this:

More information will be coming soon... More information will be coming soon...

After some investigation, I realised this is because the formatted value of the body field in this scenario looks like this:

<div id=pw-edit-2 class='pw-edit pw-edit-InputfieldCKEditor' data-name=body data-page=1115 data-lang='0' style='position:relative'><div class=pw-edit-orig><p>More information will be coming soon...</p></div><div class=pw-edit-copy id=pw-editor-body-1115 style='display:none;-webkit-user-select:text;user-select:text;' contenteditable><p>More information will be coming soon...</p></div></div>

... including all the PageFrontEdit wrapping context.  When stripped of the html tags the text appears twice and still being less than 200 characters, is output twice.

Solution:

It's easy to fix in this particular example.  Just get the unformatted value before sending to $sanitizer->truncate():

$sanitizer->truncate($page->getUnformatted('body'),200);

I don't know if this should be opened as an issue, since $sanitizer->truncate() doesn't know the context of what it's receiving; it's just a string.  Maybe this can help others who might run into this problem.

  • Like 2
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...