Page context missing in Kirbytext filter, resulting in wrong file urls

Hello,

I have a blogpost template that gets “advertisments” from another page like:

<?= page("werbung/".$page->advertisment())->text()->kirbytext() ?>

This works most of the times. Also images are rendered as expected but…

If the text contains tags i handle with a plugin the image URL is wrong then:

The text looks like:

bla
(image: webinar-projektcontrolling.png alt:bild)   <- works
(center...)(image: webinar-projektcontrolling.png alt:bild)(...center)  <- nope... wrong URL
blub

and here is the plugin code:

<?php
/* Center Plugin
 *
 * @author Jimmy Rittenborg <jimmy@sitemarina.com>
 * @version 1.0.0
 */

kirbytext::$pre[] = function($kirbytext, $text) {

  $text = preg_replace_callback('!\(center(…|\.{3})\)(.*?)\((…|\.{3})center\)!is', function($matches) use($kirbytext) {

    $html = kirbytext($matches[2]);

    return '<div class="' . c::get('center.class', 'text-center') . '">' . $html . '</div>';

  }, $text);

  return $text;

};

Any suggestions how i can fix this?

thx,
Svnt

I think the page context is missing, better do it the way the columns plugin handles this, I guess.

<?php
/* Center Plugin
 *
 * @author Jimmy Rittenborg <jimmy@sitemarina.com>
 * @version 1.0.0
 */

kirbytext::$pre[] = function($kirbytext, $text) {

  $text = preg_replace_callback('!\(center(…|\.{3})\)(.*?)\((…|\.{3})center\)!is', function($matches) use($kirbytext) {

    $match = $matches[2];
    $field = new Field($kirbytext->field->page, null, $match);
    $html = '<div class="' . c::get('center.class', 'text-center') . '">' . kirbytext($field) . '</div>';
    return $html;

  }, $text);

  return $text;

};
1 Like
kirbytext::$pre[] = function($kirbytext, $text) {

  $text = preg_replace_callback('!\(center(…|\.{3})\)(.*?)\((…|\.{3})center\)!is', function($matches) use($kirbytext) {

    $html = new Field($kirbytext->field->page, null, trim($matches[2]));
    return '<div class="' . c::get('center.class', 'text-center') . '">' . kirbytext($html) . '</div>';

  }, $text);

  return $text;

};

Yes! Fixed! Thank you so much! :heart_eyes:

@JimmyRittenborg: FYI, maybe you can include this fix into your plugin. BTW., is it on github?