Jump to content

My replace Textformatter does not work. Please help


dotnetic
 Share

Recommended Posts

I try to replace the string "P. Jentschura" with "P. Jentschura" via a Textformatter for the body field.

When I load the page, it loads infinitely, which is caused by the Textformatter. What am I doing wrong?

Here is my code:

<?php

/**
 * Textformatter P. Jentschura (1.0.0)
 * Sorgt dafr, dass der Name P. Jentschura mit einem geschtztem Leerzeichen versehen wird.
 *
 * @author Jens Martsch
 *
 * ProcessWire 2.x
 * Copyright (C) 2011 by Ryan Cramer
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */
class TextformatterJentschura extends Textformatter
{

    public static function getModuleInfo()
    {
        return array(
            'title' => "Textformatter P. Jentschura",
            'version' => "1.0.1",
            'summary' => "Sorgt dafür, dass der Name P. Jentschura mit einem geschütztem Leerzeichen versehen wird.",

            'author' => "Jens Martsch",
            'href' => "",

            'permission' => array(""),
            'autoload' => false,
            'singular' => true,
            'permanent' => false,

            'requires' => array("PHP>=5.4.0", "ProcessWire>=2.5.3", ""),
            'installs' => array(""),

        );
    }

    public function formatValue(Page $page, Field $field, &$value) {
        if (strpos($value, "P. Jentschura") === false) return;
        $value = preg_replace_callback('/P\. Jentschura/', array($this,"replace"), $value);
    }

    function replace($match){
        return str_replace($match, 'P. Jentschura', $match);
    }

}
Link to comment
Share on other sites

If you don't need the $page / $field info I'd rather just use the format(&$str) function. Also I don't really see the need for preg_replace_callback(). preg_replace should be enough as it's already replacing all occurrences, as long as you don't tell it to do otherwise.

  • Like 1
Link to comment
Share on other sites

The mix of preg_replace_callback and str_replace here doesn't work (throws an "Array to string conversion" error), as the $match passed to the callable is an array of matches (with $match[0] being the entire matched string and $match[1..n] the first to nth capturing group). It would need to either be written as

    function replace($match){
        return 'P. Jentschura';
    }

or, as the others wrote, simply get rid of preg_replace_callback completely and directly perform a str_replace.

    public function formatValue(Page $page, Field $field, &$value) {
        $value = str_replace('P. Jentschura', 'P. Jentschura', $value);
    }
  • Like 1
Link to comment
Share on other sites

EDIT: I got it working now. Seems like PW 3 has cached (compiled) my module and used the old version, instead of my changed script.

Don´t know if this is really the case, but it works now. 

Thank you for all suggestions.

Another EDIT: Now I know, where the error and endless loading came from. It was the line

'permission' => array(""),

in the getModuleInfo function. After removing it and deinstalling and installing the module again, everything works fine.

How can I prevent, that PW3 caches my module, so I can see changes immediately when I change the code or is the caching (compiling) intended?

Link to comment
Share on other sites

  • 2 months later...
On 16.4.2016 at 8:52 AM, BitPoet said:

The mix of preg_replace_callback and str_replace here doesn't work (throws an "Array to string conversion" error), as the $match passed to the callable is an array of matches (with $match[0] being the entire matched string and $match[1..n] the first to nth capturing group). It would need to either be written as


    function replace($match){
        return 'P. Jentschura';
    }

or, as the others wrote, simply get rid of preg_replace_callback completely and directly perform a str_replace.


    public function formatValue(Page $page, Field $field, &$value) {
        $value = str_replace('P. Jentschura', 'P. Jentschura', $value);
    }

 

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

×
×
  • Create New...