We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14050
    • 788 Posts
    I was thinking that it may be useful to create a plugin that replaced words in the content of the document with links to matching internal pages. This is related to the Blue Sky Vineyards website on which I work. Essentially, since we have a store, I would like any new pages created by editors that mention items in the store, to automatically link to the individual product pages. An example might help. Here was a news posting by one of the editors:


    We recently participated in the San Francisco Chronicle Wine Competition. This is a National Competition where over 4,200 wines were entered from 1,500 different wineries. Our Villard Blanc and Traminette both won silver medals in this competition and our Chambourcin Reserve received a bronze medal. This is the first wine competition that we have entered into this year and we are pleased to have placed so well among so many other wines throughout the United States.

    Visit Blue Sky Vineyard and enjoy the relaxing atmosphere and our award-winning wines.

    See You Soon!

    Since "Villard Blanc," "Traminette," and "Chambourcin Reserve" are all published pages, I would like a plugin to replace those words with links to the respective pages. A couple of questions would be what type of overhead would this create? Does something like this already exist? Does anyone have any tips before I proceed?

    I imagine that I would get the pagetitle, alias, or menutitle (decided by parameter) of all the active children for the container(s) set in a parameter. I would then do string replaces in the output to change them into links.

    What do you think?

    UPDATE
    This has been added to the repository here:
    http://modxcms.com/Context-Links-1847.html
    Support Thread:
    http://modxcms.com/forums/index.php?topic=22271
      Jesse R.
      Consider trying something new and extraordinary.
      Illinois Wine

      Have you considered donating to MODx lately?
      Donate now. Every contribution helps.
      • 7231
      • 4,205 Posts
      You can do this with the ReplaceChunk plugin but as is you would need to enter each phrase manually. This could work as a starting point for something more automated..

      http://modxcms.com/ChunkReplace-837.html
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 14050
        • 788 Posts
        So I created it and you can see a demo here:

        http://cmssandbox.com/MODx095/index.php?id=116

        The code:

        /**
         * ContextLinks 
         * Written By Jesse Rochman
         * January 30, 2008
         *
         * Version 1.0
         *
         * Events: OnWebPagePrerender
         *
         * Paste the following into the Configuration:
         * 		&parents=Comma delimited list of parents for which children need context links;string;110,111 &replace_with=Document field to use for the link;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 link 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,'menuindex','ASC', $fields= 'id, ' . $replace_with);
        			foreach ($children as $child) {
        				$context_link = '<a href="' . $modx->makeUrl($child['id']) . '">' . $child[$replace_with] . '</a>';
        				$modx->documentOutput = str_replace($child[$replace_with], $context_link, $modx->documentOutput);
        			}
        		}
        		
                break;
        
            default :
               return; // stop here - this is very important. 
        }
        


        Is there anyway for me to limit this replacement technique to just what is output by the [*content*] tag? For instance, if you click on one of the links generated, you can see that the <h1> for the content section is also converted, although this is created by using the [*pagetitle*] tag in the template. Also, I am already aware of a bug where two documents contain the same word (e.g., "Chambourcin," and "Chambourcin Reserve"). Any advice on this would be helpful also.
          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
          If anyone knows of a way to just limit the replacement to certain tags such as [*content*], that would be helpful to ensure this plugin doesn’t wreck havoc on your site smiley
            Jesse R.
            Consider trying something new and extraordinary.
            Illinois Wine

            Have you considered donating to MODx lately?
            Donate now. Every contribution helps.
            • 7231
            • 4,205 Posts
            I know what you mean, I tried this a while back and was happy that all the defined words were in bold..but then I noticed <strong> tags in my titles and in other strange places. To be able to limit it to the content (or other content specific field) would be great. It seems that you should be able to isolate the content output..but how is a good question.
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 14050
              • 788 Posts
              dev_cw,

              If you use my preparse method as posted in the Supports\Comments thread tied to the download in the repository, you can limit it to any field you want. Also, I think I might have figured a way to get at just the content by using getTemplateVarOutput(), since [*content*] for all intensive purposes, is really just a predefined TV.
                Jesse R.
                Consider trying something new and extraordinary.
                Illinois Wine

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