We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26849
    • 16 Posts
    I would like to make a simple "OnWebPagePrerender" plugin to modify some anchors which use the javascript highlight function originally bundled with MODx (I am not sure whether it is still included?). I am using the AnchorsAway plugin (http://modxcms.com/forums/index.php/topic,15753.0.html) and know that it essentially does what I want to achieve.
    For my particular requirement I have some standard anchors linking to references:
    <a href="#ref-1">Reference 1</a>
    <a href="#ref-2">Reference 2</a>
    ...
    


    I would like to replace each with the following code, using an OnWebPagePrerender event:
    <a onclick="highlight('ref-1','#ffffff',1000);" href="#ref-1">Reference 1</a>
    <a onclick="highlight('ref-2','#ffffff',1000);" href="#ref-2">Reference 2</a>
    ...
    


    Therefore, all the plugin really needs to do is add the extra bit of code:
    onclick="highlight(’ref-1’,’#ffffff’,1000);"
    which I am guessing is fairly straightforward, apart from the inclusion of the unique identifier ’ref-1’.

    I have very limited experience with php, but assume that the str_replace or the preg_replace function would achieve what I want. However, I am really struggling to figure out whether this is the right track? I don’t suppose someone would be able to shed some light?

    Many thanks,

    JB
      • 3749
      • 24,544 Posts
      Quote from: jb2009 at Feb 22, 2009, 05:44 AM

      I would like to make a simple "OnWebPagePrerender" plugin to modify some anchors which use the javascript highlight function originally bundled with MODx (I am not sure whether it is still included?). I am using the AnchorsAway plugin (http://modxcms.com/forums/index.php/topic,15753.0.html) and know that it essentially does what I want to achieve.
      For my particular requirement I have some standard anchors linking to references:
      <a href="#ref-1">Reference 1</a>
      <a href="#ref-2">Reference 2</a>
      ...
      


      I would like to replace each with the following code, using an OnWebPagePrerender event:
      <a onclick="highlight('ref-1','#ffffff',1000);" href="#ref-1">Reference 1</a>
      <a onclick="highlight('ref-2','#ffffff',1000);" href="#ref-2">Reference 2</a>
      ...
      


      Therefore, all the plugin really needs to do is add the extra bit of code:
      onclick="highlight(’ref-1’,’#ffffff’,1000);"
      which I am guessing is fairly straightforward, apart from the inclusion of the unique identifier ’ref-1’.

      I have very limited experience with php, but assume that the str_replace or the preg_replace function would achieve what I want. However, I am really struggling to figure out whether this is the right track? I don’t suppose someone would be able to shed some light?

      You are definitely on the right track, although preg_replace is difficult for beginners.

      I think you could do it with a single, fairly complex, regex replace.

      I think you’ll want to have something like this:

      /^(<a href=")#(xxx)("[yyy])$/


      Where xxx will match any of your ref-1, ref-2 references and yyy will match the rest of the string.

      Then, just replace the first () with what you want (including the XXX term) and leave everything else as is (the # will be omitted since it’s not in a group).

      I’m sure someone better at regular expressions can refine this.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 26849
        • 16 Posts
        Thanks for your help BobRay, it helped me get things underway.

        Of course, I’ve stumbled fairly soon after solving out the syntax I need to use (I think). I figured I could use the ereg_replace function:
        <?php
        $string = 'A link to <a href="#ref-1">reference 1</a>';
        echo ereg_replace('(<a href="#ref-(.*)">)', '<a onclick="highlight(\'ref-\\2\',\'#ffffff\',1000);" href="#ref-\\2">', $string); 
        ?>
        

        This seems to do the trick, but when I try to call it in MODx using my adjusted version of the AnchorsAway plugin, only part of the desired html is actually replaced. The plugin I have created looks like this (NOTE: I do not use the php tags in the plugin):
        <?php
        $e= & $modx->Event;
        switch ($e->name) {
            case "OnWebPagePrerender" :
                if($modx->documentIdentifier != $modx->config['site_start'])  {
                    $modx->documentOutput= ereg_replace('(<a href="#ref-(.*)">)', '<a onclick="highlight(\'ref-\\2\',\'#ffffff\',1000);" href="' . $modx->makeUrl($modx->documentIdentifier) . '#ref-\\2">', $modx->documentOutput);
                }
                break;
        }
        ?>
        

        When I run this I am returned with the following html:
        <a onclick="highlight('ref-1">

        instead of the desired:
        <a onclick="highlight('ref-1','#ffffff',1000);" href="#ref-1">


        I don’t suppose anyone knows what I have done wrong?

        Additionally, this only "works" (I use that word loosely as it isn’t working) when I have the AnchorsAway plugin disabled... But of course both these plugins should conflict with one another! Thing is, I have no idea why?

        Any help is appreciated smiley

        JB
          • 3749
          • 24,544 Posts
          I’m afraid I can’t see why you’re not getting the desired replacement. Hopefully someone else with sharper eyes will chime in.

          On the conflict, I think it might be because the other plugin removes the "#" which you are looking for. If you had your plugin execute first (by changing the priority of the two plugins) they might work together.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 26849
            • 16 Posts
            OK, so I’ve had a chance to play with this a little more and figured it out - thanks for your help BobRay.

            For the sake of closing this thread, what I eventually came up with was the following plugin:
            <?php
            $source = &$modx->documentOutput;
            $source = ereg_replace('<a href="#ref-([0-9]{1,3})">', '<a onclick="highlight(\'ref-\\1\',\'#ffffff\',1000);" href="#ref-\\1">', $source);
            ?>
            

            Given my ignorance of php embarrassed, the problem was the (.*) call in my previous postings. I replaced this with [0-9]{1,3} as the remainder of my "#ref-..." anchors are numbers between 1 and 3 digits.

            The issue with the plugin priority was simple to solve when I figured out where the appropriate settings were located (under the Edit Plugin Execution Order by Event) and gave the priority to my plugin over AnchorsAway.

            laugh

            JB
              • 3749
              • 24,544 Posts
              Glad you got it sorted. Thanks for reporting back so others can see your solution. smiley
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting