We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16702 ☆ A M B ☆
    • 536 Posts
    Hello,
    in new Revo 2.1.1 DynamicDescription is not work, but in old one 2.0.8 is work perfect, can somebody help me to implement to new Revo?
    <?php
    /* Parameters */
    
    /**
     * &maxWordCount
     * integer (25)
     * Sets the maximum word count of the description, default is 25 words
     */
    $maxWordCount = (!empty($maxWordCount) && is_numeric($maxWordCount)) ? $maxWordCount : 25;
    
    /**
     * &id
     * integer ($modx->documentObject['id'])
     * the id of the page you want a description for, defaults to the
     * current page.
     */
    $id = (is_numeric($id) && !empty($id)) ? $id : $modx->documentObject['id'];
    
    /**
     * &descriptionTV
     * string ('')
     * The name of a template variable you've set up for the description.
     * If you have a separate template variable set up for a description
     * you write in, set that with this parameter.  If the parameter ends
     * up being empty, it will default back to the content. If it is not
     * empty then it will display the value of the TV and stop processing.
     * In other words, it will print it just as you typed it.
     */
    $descriptionTvArray = (!empty($descriptionTV)) ? $modx->getTemplateVarOutput($descriptionTV,$id) : '';
    $descriptionTV = $descriptionTvArray[$descriptionTV];
    
    
    /* Snippet Code */
    
    /**
     * Function: getDynaDescription()
     * Returns:  A string of text ready to be placed in a meta description tag
     */
    if (!function_exists(getDynaDescription)) {
      function getDynaDescription($text='',$excerpt_length=25)
      {
        global $modx;
        $text = str_replace(']]>', ']]>', $text);
        /* remove line breaks */
        $text = str_replace("\n",' ',$text);
        $text = str_replace("\r",' ',$text);
        /* remove entity chars */
        //$text = preg_replace('~&.+;~U',' ',$text);
        /* remove special MODx tags - chunks, snippets, etc.
         * If we don't do this they'll end up expanded in the description.
         */
        $text = $modx->stripTags($text);
        $words = preg_split ("/\s+/", $text,$excerpt_length+1);
        if (count($words) > $excerpt_length) {
          array_pop($words);
          array_push($words, '...');
          $text = implode(' ', $words);
        }
        return trim(stripslashes($text));
      }
    }
    
    if (!empty($descriptionTV)) {
      // if the $descriptionTV is not empty then we just print it with
      // no extra processing, it's up to you to manage your description
      echo $descriptionTV;
    } else {
      // for everyone else that is lazy, just grab it from the content, yeah!
      $content = $modx->getDocument($id,'content');
      $content = $content['content'];
      echo getDynaDescription($content,$maxWordCount);
    }
    


    Thanks!
      palma non sine pulvere
      • 3749
      • 24,544 Posts
      I’ll take a quick stab at it. I tweaked it a little and added a &fullTag property so you wouldn’t have to type in the rest of the tag for each one:

      <?php
      /**  getDynaDescription snippet
        *  Creates a full meta description tag or just the content part.
        */
      
      
      
      /* Properties */
      
      /**
       * &fullTag 
       * (int 1/0)
       * Set to one if you want a full meta tag returned,
       * set to 0 if you want just the content and will create the meta
        * tag yourself.
       */
      
      /**
       * &maxWordCount
       * integer (25)
       * Sets the maximum word count of the description, default is 25 words
       */
      $mwc = $scriptProperties['maxWordCount'];
      $maxWordCount = (!empty($mwc) && is_numeric($mwc)) ? $mwc : 25;
      
      /**
       * &id
       * integer
       * the id of the page you want a description for, defaults to the
       * current page.
       */
      $pid = $scriptProperties['id'];
      $pid = (is_numeric($pid) && !empty($pid)) ? $pid : $modx->resource->get('id');
      
      /**
       * &descriptionTv
       * string ('')
       * The name of a template variable you've set up for the description.
       * If you have a separate template variable set up for a description
       * you write in, set that with this parameter.  If the parameter ends
       * up being empty, it will default back to the content. If it is not
       * empty then it will display the value of the TV and stop processing.
       * In other words, it will print it just as you typed it.
       */
      
      if (!empty ($scriptProperties['descriptionTv'])) {
          
          $tv = $modx->getObject('modTemplateVar',array('name'=>$scriptProperties['descriptionTv']));
          
          $descriptionTv = $tv? $tv->getValue($pid) : '';
      } else {
          $descriptionTv = '';
      }
      
      /**
       * Function: getDynaDescription()
       * Returns:  A string of text ready to be placed in a meta description tag
       */
      if (!function_exists(getDynaDescription)) {
        function getDynaDescription($text='',$excerpt_length=25)
        {
          global $modx;
          $text = str_replace(']]>', ']]>', $text);
          /* remove line breaks */
          $text = str_replace("\n",' ',$text);
          $text = str_replace("\r",' ',$text);
          /* remove entity chars */
          //$text = preg_replace('~&.+;~U',' ',$text);
          /* remove special MODx tags - chunks, snippets, etc.
           * If we don't do this they'll end up expanded in the description.
           */
          $text = $modx->stripTags($text);
          $words = preg_split ("/\s+/", $text,$excerpt_length+1);
          if (count($words) > $excerpt_length) {
            array_pop($words);
            array_push($words, '...');
            $text = implode(' ', $words);
          }
          return trim(stripslashes($text));
        }
      }
      
      if (!empty($descriptionTv)) {
        // if the $descriptionTv is not empty, use it
      
        $output =  $descriptionTv;
      } else {
        // for everyone else that is lazy, just grab it from the content, yeah!
      
         if ($pid == $modx->resource->get('id') ) { // don't pull another object unless we have to
             $content = $modx->resource->getContent();
         } else {
             $otherResource = $modx->getObject('modResource', $pid);
             $content = $otherResource->getContent();
         }
      
         $output =  getDynaDescription($content,$maxWordCount);
      
         
      }
      
      if ($fullTag == true) {
             $output = '<meta name="description" content="' . $output . '">' . "\n";
      }
      
      
      return $output;
      



        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
        • 16702 ☆ A M B ☆
        • 536 Posts
        Quote from: BobRay at Jun 09, 2011, 12:19 PM


        I’ll take a quick stab at it (untested). I tweaked it a little and added a &fullTag property so you wouldn’t have to type in the rest of the tag for each one:

        ...

        Hello BobRay,
        first THANKS!
        I don`t know why but i get:

        HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
        P.S. and i have no error in log
          palma non sine pulvere
          • 3749
          • 24,544 Posts
          I corrected a couple of errors and updated the code above. Try that and let me know. It works for me. wink
            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
            • 16702 ☆ A M B ☆
            • 536 Posts
            Quote from: BobRay at Jun 09, 2011, 04:14 PM

            I corrected a couple of errors and updated the code above. Try that and let me know. It works for me. wink

            work perfect! Great! What you drink? laugh laugh grin
              palma non sine pulvere
              • 3749
              • 24,544 Posts
              Quote from: Kristalin at Jun 10, 2011, 12:12 AM

              Quote from: BobRay at Jun 09, 2011, 04:14 PM

              I corrected a couple of errors and updated the code above. Try that and let me know. It works for me. wink

              work perfect! Great! What you drink? laugh laugh grin

              Anything -- as long as it’s beer. wink

              I’m working on a Package for this with a few extra features and a really interesting install process with options to create a TV and insert the tags in selected templates (currently fighting with preg_replace() for uninstalling the tags). 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
                • 32699 ☆ A M B ☆
                • 427 Posts
                Interesting concept. I typically use the [[*description]] as the meta description. I will have to play around with this.
                  Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                  Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
                  • 3749
                  • 24,544 Posts
                  It’s available as a package now, called getDynaDescription.

                  It’s kind of worth looking at just to see what you can do with installation options in Revo.
                    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
                    • 16702 ☆ A M B ☆
                    • 536 Posts
                    Quote from: BobRay at Jun 12, 2011, 10:05 PM

                    It’s available as a package now, called getDynaDescription.

                    It’s kind of worth looking at just to see what you can do with installation options in Revo.

                    great! THANKS, Bob
                    Kr.

                    :))))))))))
                      palma non sine pulvere
                      • 16899
                      • 284 Posts
                      Why don´t you also try getResources plugin? You can push from your custom resources a tv created by you and shown in a specifi template created bty you... well this is just an idea!!!! But i think you have your problem solved!!!!
                      http://rtfm.modx.com/display/ADDON/getResources