We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14050
    • 788 Posts
    Quote from: davidm at Jan 31, 2008, 03:07 PM

    Ok I understand the bottom line now !
    This would make a very good description for the Repository smiley

    I was busy coding, and didn’t have time to come up with a clear and concise description tongue I just updated it in the first post and in the repository. Also, David, what are your thoughts regarding this:


    Regarding your feature request, would that be in place of the current behavior or in addition to, if the TV exists? Also, do you think it would be better as a TV or as a parameter in the plugin’s configuration?

      Jesse R.
      Consider trying something new and extraordinary.
      Illinois Wine

      Have you considered donating to MODx lately?
      Donate now. Every contribution helps.
    • I’d say make it a configuration ... one or the other.

      &matchSource ... default to the three document objects currently
      &matchSource=`myTV` ... means use [*myTV*] ... you can always put a default value of [*pagetitle*], [*menutitle*], etc. in there
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 14050
        • 788 Posts
        Quote from: rthrash at Jan 31, 2008, 02:37 PM

        It might be cool to put in a parameter to have a dedicated TV to use for replacement words.

        I started to develop this, and realized we are missing a key component. If I have a dedicated TV with a list of comma delimited "replacement words," what exactly are you wanting them replaced with? A certain link? So would they have to have replacement words and the corresponding URL?

        Something like "Keyword1||URL1,Keyword2||URL2"? Is that what you were talking about? Of course when I use pagetitle, alias, or menutitle of documents in the site, I use the associated Document Identifier to automatically create the URL.
          Jesse R.
          Consider trying something new and extraordinary.
          Illinois Wine

          Have you considered donating to MODx lately?
          Donate now. Every contribution helps.
          • 14050
          • 788 Posts
          So any of the superior PHP coders out there know how I can limit this to the [*content*] field?
            Jesse R.
            Consider trying something new and extraordinary.
            Illinois Wine

            Have you considered donating to MODx lately?
            Donate now. Every contribution helps.
            • 14050
            • 788 Posts
            I thought I might have found a solution to restricting this to just the [*content*] field, but have hit a wall again. Here is what I was attempting to do:

            $content = $modx->getDocument($modx->documentIdentifier,'content');
            $content = $content['content'];
            $temp_content = $content
            


            I could then replace the keywords in $temp_content and then do the following:

            $modx->documentOutput = str_replace($content, $temp_content, $modx->documentOutput);
            


            However, $content retrieved by using getDocument() is unparsed and $modx->documentOutput is parsed, so that last string replace will not work. Any help would be appreciated.
              Jesse R.
              Consider trying something new and extraordinary.
              Illinois Wine

              Have you considered donating to MODx lately?
              Donate now. Every contribution helps.
              • 19083
              • 15 Posts
              Wow,  this plug-in is incredible! 

              If you’re looking for upgrade options, I would suggest only replacing entire words.  For instance, I have a pagetitle called "X-man".  The plug-in is replacing those characters in the word "X-mansion". Another would be a pagetitle of "Emp" and it’s replacing those characters in "Empress".

              Not exactly a perfect match.  Only a minor detail in an otherwise fantastic plug-in.
                • 14050
                • 788 Posts
                I am glad you found it useful. Use this plugin code for a simple work-around that looks for whole words would be to use the following code:

                
                /**
                 * ContextLinks 
                 * Written By Jesse Rochman
                 * January 30, 2008
                 *
                 * Version 1.0
                 *
                 * Events: OnWebPagePrerender
                 *
                 * Paste the following into the Configuration:
                 * 		&parents=Parents;string;0 &replace_with=Match Source;string;pagetitle
                 * 		
                 * Usage:
                 *		Parents should be a comma delimited list of IDs for which children you want context links.
                 *		Document field to use for the Match Source would likely be pagetitle, alias, longtitle, description, or menutitle.
                 *
                 *
                 */
                
                $e= & $modx->Event;
                switch ($e->name) {
                    case "OnWebPagePrerender" : //mo register only for backend
                        $parents = explode(',',$parents);
                		foreach ($parents as $parent) {
                			$children = $modx->getActiveChildren($parent,$replace_with,'DESC', $fields= 'id, ' . $replace_with);
                			foreach ($children as $child) {
                				
                				//Dumb whole word search.  If Keyword is "Man".  The plugin will not match "manhole."  (This is good).
                				//Will not match "man," "man." "'man'" (This is bad because it is thrown off by the punctuation).
                				/*
                				$replaced_string = ' ' . $child[$replace_with] . ' ';
                				$context_link = ' <a href="' . $modx->makeUrl($child['id']) . '">' . $child[$replace_with] . '</a>';
                				$modx->documentOutput = str_replace($replaced_string, $context_link, $modx->documentOutput);
                				*/
                				
                				//Somewhat smarter whole word search, but a little agressive.  If Keyword is "Man".  The plugin will not match "manhole."  (This is good).
                				//Will match "man," "man." "'man;" "'man'" (This is even better).  However, it will also match these keywords if inside attribute fields for your HTML, creating messy links.
                				///*
                				$replaced_string = $child[$replace_with];
                				$context_link = '<a href="' . $modx->makeUrl($child['id']) . '">' . $child[$replace_with] . '</a>';
                				$modx->documentOutput = preg_replace('`\\' . $replaced_string . '\b`', $context_link, $modx->documentOutput);
                				//*/
                			}
                		}
                		
                        break;
                
                    default :
                       return; // stop here - this is very important. 
                }
                


                Any hints on the making these searches better would be nice. Also any hints on how to narrow the scope of the replacement to certain document fields such as pagetitle, content, and longtitle.
                  Jesse R.
                  Consider trying something new and extraordinary.
                  Illinois Wine

                  Have you considered donating to MODx lately?
                  Donate now. Every contribution helps.
                  • 14050
                  • 788 Posts
                  Alright, I found a way to limit the scope of this plugin to one or more of the document objects (e.g., pagetitle, description, content, etc.). However, this is prior to them being parsed, so it would not work if the pagetitle, description, or content contained chunks or snippets that output content needing to be replaced. Would this limitation in scope be useful to anyone? I found that limiting the scope to just content and using the dumb whole word search appears to provide the exact behavior that lead me to start developing this. I will post the new code tomorrow. I will also strive to have this meet other peoples needs with more complex regular expressions (I am still trying to figure them out smiley ).
                    Jesse R.
                    Consider trying something new and extraordinary.
                    Illinois Wine

                    Have you considered donating to MODx lately?
                    Donate now. Every contribution helps.
                    • 6726
                    • 7,075 Posts
                    Really cool !

                    The "whole words" tweak above sure should be posted as a revision on the repository (maybe it’s already done, didn’t check), IMHO, otherwise it might get lost in the forums...

                    About that last mod : what about TVs content, would it be parsed ?
                    If not, that might be a problem for quite a few of us...

                    Anyway, nice work Jesse !
                      .: COO - Commerce Guys - Community Driven Innovation :.


                      MODx est l&#39;outil id
                      • 14050
                      • 788 Posts
                      David, I plan to update the code with more configuration options. 3 total search types. Searching preparsed code or after parsed code, and searching inside any document object, including TVs. Once the coding updates are done, I will post it to the repository.
                        Jesse R.
                        Consider trying something new and extraordinary.
                        Illinois Wine

                        Have you considered donating to MODx lately?
                        Donate now. Every contribution helps.