Jump to content

Recommended Posts

Posted

Hello @ all,

I have created a very simple Textformatter module which should add a class to ordered and unordered lists.

<?php
/**
 * Processwire module for adding a special class for lists added via CKEditor.
 * by Juergen Kern
 *
 * 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 TextformatterListCK extends Textformatter implements Module
{
    
    public static function getModuleInfo()
    {
        return array(
            'title' => "Textformatter for Lists",
            'version' => "100",
            'summary' => "This little Textformatter adds a class to the ol and ul tag (only for lists added via editor)",
            'author' => "Juergen Kern",
            'autoload' => false,
            'singular' => false,
            'permanent' => false,
            'requires' => array(
                "PHP>=5.4.0",
                "ProcessWire>=2.5.28"
            ),
            'icon' => 'list'
        );
    }
    
    public function format(&$str) 
    {
        $str = str_replace('<ul>', '<ul class="textlist fa-ul">', $str);
        $str = str_replace('<ol>', '<ol class="rounded-list">', $str);
    }
    
}
?>

Problem: This Textformatter is added to the body field, but it doesnt change anything and I could not find any mistake. Maybe someone has a better experience in creating Textformatter modules and could give me a hint why it is not working.

Thanks in advance

Posted

The module itself is fine. Is the markup in the body field really what you think (i.e. no whitespaces or additional attributes in the ul/ol tags)? Does the content you look at really come from a body fields? Do you have caching enabled?

Posted

Hello BitPoet,

no whitespaces or additional attributes and the content comes from the body field. Caching is deactivated. Thats the reason why I am a little bit confused why it is not working ???

Posted

you might try using Tracy debugger, this way you can bd($str) inside the function when viewing the frontend and see what's going on there.

  • Like 1
Posted

Hmmmm! Tracy Debugger says that this Textformatter module is not loaded (I cannot find it in the list of loaded modules) The other Textformatter that I have added to the body field will be loaded (it appears on the module list of Tracy Debugger).

 

Posted
20 minutes ago, Juergen said:

Hmmmm! Tracy Debugger says that this Textformatter module is not loaded (I cannot find it in the list of loaded modules) The other Textformatter that I have added to the body field will be loaded (it appears on the module list of Tracy Debugger).

 

In a rush, but when I install your module the flag field in the module's database table is set to 0. If you change that to 1 and do a modules refresh then Tracy shows it as loaded.

Posted

I think the problem is one of these:

            'autoload' => false,
            'singular' => false,
            'permanent' => false,

Just remove all those and refresh and it should work - at least it does here.

Posted

Gave you tried clearing compiled files, or reinstall the module?

Btw, multivalue textformatter (see my sig below) uses different methods than yours. Why is that?

Posted
2 minutes ago, Juergen said:

Flag stays at 1, but nothing happens on the frontend. I will take a closer look tomorrow.

Working fine here so I would try uninstalling and reinstalling.

Screen Shot 2016-08-26 at 12.28.18 PM.png

Is it now being loaded at least and just not formatting, or is it not even showing up as loaded by Tracy?

Stupid question I know, but after all your testing, do you now definitely have it applied to the desired field?

  • Like 2
Posted

After reinstalling it is not being loaded at all, flag stays at 1 in the db and it is definitely the body field where I have applied this Textformatter. I have created another CKEditor field for testing purposes and have applied this Textformatter too - same effect. The problem is definitely that the Textformatter will not be loaded in my case.

I use the latest dev version (3.0.30) of PW.

Posted

Hello @tpr,

I see you use a different approach. Your function in MultivalueTextformatter looks like

public function formatValue(Page $page, Field $field, &$value) {

while my function is

public function format(&$str) 

Could this be a reason?

Posted

A wild and unlikely guess, but any chance it has something to do with the case of your class name and your php version?

What if you try: TextformatterListCk

Note the lowercase "k"

  • Like 1
Posted

@Juergen, you should remove the closing PHP tag at the end of the file. For PHP-only files it's normally considered best practice to omit the closing tag, because you can get errors and odd behaviour if any character or whitespace is accidentally added after the closing tag. Not sure if that could cause the problem you're having (your module works normally for me).

  • Like 2
Posted

Thanks  @ Robin for the hint,

I know this and i have tried it without the closing tag yesterday, but there was no influence. Currently I am not at home so I cannot test all the recommendations.

  • Like 1
Posted
7 hours ago, Juergen said:

Could this be a reason?

Both functions can be used to alter the markup. Only difference is that format() does not receive $page and $field.

  • Like 2
Posted

Another idea: is there a class with the same name (TextformatterListCK) lying around somewhere in an include path?

  • Like 1
Posted

Thank you all for your contribution!!

Finally I ended up with this piece of code, which works as expected:

 

<?php
/**
 * Processwire module for adding a special class for lists added via CKEditor.
 * by Juergen Kern
 *
 * 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 TextformatterForLists extends Textformatter implements Module
{
    
    public static function getModuleInfo()
    {
        return array(
            'title' => "Textformatter for lists",
            'version' => "100",
            'summary' => "This little Textformatter adds a class to the ol and ul tags",
            'author' => "Juergen Kern",
            'requires' => array(
                "PHP>=5.4.0",
                "ProcessWire>=2.5.28"
            ),
            'icon' => 'list'
        );
    }
    
    public function format(&$str) 
    {
        $str = str_replace('<ul>', '<ul class="textlist fa-ul">', $str);
        $str = str_replace('<ol>', '<ol class="rounded-list">', $str);
    }
    
}

It seems that the class name causes the problem, but I swear I have not the same class anywhere on the page. Maybe the CK in the classname was responsible for the behaviour. Anyway now it works.

  • Like 3

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...