Jump to content

beginnerquestion on performance with a textformatter


mr-fan
 Share

Recommended Posts

Hi guys,

i've just a simple question of a little textformatter that i use for replacing different textvalues.

I'm not that experienced PHP professional so i asking before i get in possible troubles...

Setup is a "Glossar" like page holder with glossar_entries of various types like (abbr, internal link, external link)

In my textfields i use pipes to set a term of that glossary like ||PW|| is great! i can preset ||Internal|| and ||External|| links and reuse them in every textblock...if i change the glossar entry on every page i use this it will automagic change...

so far so simple (i've tested autolink and bought Profields...but i wanna give the user the power to edit this entries and have more control where these kind of autolinks work and could used)

Here is my first more advanced textformatter on this and my simple question is - will this produce any overload or trouble if i get around about 0-10 terms on a content page?

Second Question is on caching with textformatter replacements? How this is handled?

<?php
/**
 * ProcessWire TextformatterGlossary
 *
 * module made by mr-fan.
 * 15.09.15 basic class and wrapper configuration added
 *
 */
class TextformatterGlossary extends Textformatter
{
    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    public static function getModuleInfo()
    {
        return array(
            'title' => 'Autolink from Glossar',
            'version' => 101,
            'author' => 'mr-fan',
            'summary' => "Allows to use tags in textareas to autolink to specific glossary links."
            //'href' => 'http://processwire.com/talk/topic/1182-module-image-tags/?p=57160',
        );
    }
    /**
     * Format the given text string.
     *
     * @param Page $page
     * @param Field $field
     * @param string $value
     */
    public function formatValue(Page $page, Field $field, &$value){

        // use fast strpos check to make sure that $value contains pipes ||
        if (stripos($value, '||') === false) return;

        //get all terms in ||pipes|| in an array
        $matches = array();
        preg_match_all('/\Q||\E[^|]+\Q||\E/', $value, $matches);

        //the multidimensional array holds the single strings in the second array['0']
        foreach ($matches['0'] as $key => $match) {

            //get all glossary pages in a pagearray
            $entry = wire('pages')->find("template=glossar_item,title=$match")->first();

            if ($entry) { //entry is found in our glossar pages

                //rip the pipes
                $term = str_replace('|', '', $match);

                //set the replacement depending from the item type
                switch ($entry->glossar_type) {
                    case '1': //abbr
                        $replacement = '<abbr title="' . $entry->headline . '">' . $term . '</abbr>';
                        break;
                    case '2': //external link
                        $replacement = '<a rel="help" target="blank" href="' . $entry->extern_link->url . '" data-original-title="' . $entry->headline . '"><span class="fa-globe" aria-hidden="true"></span> ' . $term . '</a>';
                        break;
                    case '3': //internal link
                        //internal link need to get the url
                        $internLink  = wire('pages')->get("$entry->page_link");
                        $replacement = '<a rel="help" href="' . $internLink->url . '" data-original-title="' . $entry->headline . '">' . $term . '</a>';
                        break;
                    default:
                        $replacement = $term;
                }
                //works the part inside the tags are changed ||test|| on every match
                $value = str_replace($match, $replacement, $value);

            } else { //the entry for  ||term|| is not found and get renderd without pipes just as normal text
                
                //rip the pipes
                $term  = str_replace('|', '', $match);
                //replace the matches of ||term|| with the cleaned value
                $value = str_replace($match, $term, $value);
            }
        }
    }
}

Best regards mr-fan

Link to comment
Share on other sites

thanks, nice idea and good example.

sometimes i have a replacements table or textarea right in the editor; then i use {{tokens}} in the body to grab those values;

(template does the runtime replacement).

Sometimes you might have some boilerplate content that is reused in various places but needs to have certain terms replaced contextually to the page..

+---------+----------+
| search1 | replace1 |
+---------+----------+
| search2 | replace2 |
+---------+----------+
Link to comment
Share on other sites

to be honest i'm on a tight dealine for a project i'll use this textformatter first and like i wrote i'm not the great PHP dev.
... :wacko:

so may someone could write if the code seems to be ok or will this give me a mess or performance problem in the long run and important for me while this is nowhere in the doc's are textformatter's output/replacements cached?

Link to comment
Share on other sites

The whole system itself works great i've in the pagetree

-glossar (rootpage with BatchChildEditor in Lister Edit mode)

--item 1

--item 2

item could be actual an <abbr> or an internal link (with a page field with choosen select) or a external link...but could be everything i will setup on this replacements

itemtype is simple choosen by a optionsfield

only condition is the title of the glossaritem have to match the term.

this could be much more develeopped to get real footnotes or additonal contentypes to replace....

Link to comment
Share on other sites

hi mr-fan,

what kind of caching do you use? if you are using template cache then performance should not be a problem as the textformatter will only be executed once (when the cache is created). in that case you would have to flush your cache every time your glossar is changed...

i think you could also use markupcache for this special field: https://processwire.com/talk/topic/7-new-markupcache-module/

Link to comment
Share on other sites

Thanks for reading...

Your thoughts are exactly what i need..since I bet that I forgot some details like to setup flushing if glossary entries change.

And I will take a look into markupcache. Until now I only worked with template cache.

Best regards mr-fan

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