Sometimes you need to be able to insert a teaser break before the admin settings trimmed char cutoff. These lines should help with that. Not able to provide a patch from where I am now.
/**
* Implements hook_field_formatter_view().
*/
function trimmed_plaintext_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'text_plain_trimmed':
foreach ($items as $delta => $item) {
+ $delimiter = strpos($item['value'], '
');
+ if ($delimiter) {
+ $output = preg_replace('/\s\s+/', ' ', strip_tags(substr($item['value'], 0, $delimiter)));
+ } else {
$output = preg_replace('/\s\s+/', ' ', strip_tags(_text_sanitize($instance, $langcode, $item, 'value')));
+ }
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | trimmed_plaintext-break-1432796-9.patch | 1.04 KB | perarnet |
| #8 | trimmed_plaintext-break-1432796-8.patch | 1.69 KB | j0rd |
| #2 | trimmed_plaintext-break.patch | 501 bytes | perarnet |
Comments
Comment #1
j0rd commented@perarnet Seems like this may be a useful feature. I'm going to need a patch to be able to incorporate this for you though, as it seems that when posting your code in here, drupal.org has stripped away the <!--break-->
So please post the code properly between
PHP tagsor something so I can see it properly and I'm not wasting my time trying to guess what you code is.I'm just as lazy as you are :D
Comment #2
perarnet commentedHere's a patch. The git repo was not consistent with the version for download, so just used a diff output.
Comment #3
j0rd commentedWe should be using text_summary() to create the teaser for us. Problem is I'm tripping the 'BREAK' out before running it though text_summary().
So while this bug exists, I don't believe your patch solves it properly.
Also if you're going to do a diff, use these arguments `diff -urN`. Otherwise, I'm unsure how to apply it with `patch`
Comment #4
j0rd commentedneeds work.
Comment #5
j0rd commentedThis problem was also created by this patch
#1350902: Should you be stripping tags before trimming
Comment #6
j0rd commentedAlso this core bug causes a problem, as _text_sanitize & filtered_html remove the BREAK tag, thus text_summary never finds it.
#881006: Regression: 'break' tag doesn't work with Filtered HTML
Comment #7
j0rd commentedReal problem is Drupal's stupidity. What should happen when a body is saved and has a BREAK, $item['summary'] should get populated, and then my filter module should only have to test for that.
Why should ever single field_formatter have to test for $item['summary'] and if it doesn't exist summarize the text myself.
This not only adds a bunch of additional logic required for every field_formatter, but it forces me to re-computate the value every time, instead of doing it once and saving the result.
Comment #8
j0rd commentedHere's a patch which would work once #881006: Regression: 'break' tag doesn't work with Filtered HTML is fixed.
While your patch above would work as expected, the HTML would never be passed through input filters, which could do replacements in the HTML and thus change the text before the BREAK.
My solution is more optimal, but won't work (if you're using filtered_html) until the above issue is resolved in core.
Comment #9
perarnet commentedAdding < !-- > to allowed tags in input format lets the patch work, but still need to add the fix to case 'text_plain_trimmed'.
Here's a patch that works for me.
Comment #10
greg boggsTeaser breaks can be controlled using Display Suite on a per display basis which may be helpful for others finding this thread.
Also, getting editor "teaser" break to work with filtered HTML using the regular summary/teaser requires the same edit to accept
, so I think this patch should be committed along with an update to readme.txt explaining how to do it!
Comment #11
greg boggs