Jump to content

Simple Textformatter module doesnt work


Juergen
 Share

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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
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...