We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40385
    • 75 Posts
    Hello,

    I'm facing a problem with getResource. My modx website has a section with resources representing user profiles. These resources have a pagetitle syntax like this:

    "Surname Lastname"

    When I use "&sortby=`{"pagetitle":"ASC"}`" in my getResource call it only filters by the first word/surname. Is there a way to sortby the second word/lastname? I was thinking about using a custom output filter but I have no clue if I can pass the result of this output filter to the sortby call?!

    Do you have any recommendations other than using single TVs for sur- and lastname?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It looks like you can; this example is used in the documentation on output filters
      [[!getResources:default=`Sorry - nothing matched your search.`?
         &tplFirst=`blogTpl`
         &parents=`2,3,4,8`
         &tvFilters=`blog_tags==%[[!tag:htmlent]]%`
         &includeTVs=`1`]]

      Notice the tvFilters property is a snippet using an output filter.

        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 40385
        • 75 Posts
        Thanks scottwell,

        unfortunately this seems to be only working for the "tvFilters" property. When I apply this to the "sortby" property (where "stripString" is the custom output filter) it outputs a php error:

        [[!getResources:default=`Sorry - nothing matched your search.`?
          &showHidden=`1`
          &tpl=`getListTpl`
          &limit=`100`
          &includeTVs=`1`
          &processTVs=`1`
          &sortby=`{"pagetitle:stripString":"ASC"}`
        ]]
          • 4172
          • 5,888 Posts
          if you need filtering, sorting by lastname, I really would put them in two different tvs.
          It would be possible to split the pagetitle automatically by onDocFormSave into two TVs, or reverse, fill the pagetitle with content from sur- lastname- TVs
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 40385
            • 75 Posts
            Thanks Bruno17, that sounds like I plan. I don't want the client to enter surname and lastname two times. So I create two additional TVs and then I have to write a plugin that will generate the page title onDocFormSave, correct?
              • 4172
              • 5,888 Posts
              This should do it, yes.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 40385
                • 75 Posts
                Hmm...I can't get it working. (I always receive the msg, that the pagetitle is empty) This is my plugin code which is attached to the onDocFormSave event:

                <?php
                // Get ID of current resource
                $id = $modx->resource->get('id');
                // Get object of current resource
                $resource = $modx->getObject('modResource',array('id'=>$id));
                // Get TVs that hold the first and lastname
                $firstname = $modx->resource->getTVValue('firstname');
                $lastname = $modx->resource->getTVValue('lastname');
                // Get pagetitle
                $pagetitle = $modx->resource->get('pagetitle');
                
                // Check if pagetitle is empty
                if ($pagetitle == '') {
                // Create full name
                    $name = $firstname." ".$lastname;
                // Set full name as pagetitle
                    $resource->set('pagetitle', '$name');
                // Save object
                    $resource->save();
                }


                Any suggestion? [ed. note: paul_kemp last edited this post 14 years, 2 months ago.]
                  • 39932
                  • 483 Posts
                  I have a potential solution, but it requires knowing whether you are receiving the id correctly... Can you confirm this?
                    Website: Extended Dialog Development Blog: on Extended Dialog
                    Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                    Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                    Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                    • 40385
                    • 75 Posts
                    Quote from: fuzzicallogic at Aug 20, 2012, 05:41 AM
                    I have a potential solution, but it requires knowing whether you are receiving the id correctly... Can you confirm this?

                    I broke down the script to some lines...just setting the pagetitle with a static string...and this was working! When I change this to get the id dynamically the script stops working and I receive the following error:

                    PHP Fatal error:  Call to a member function get() on a non-object in


                    So unfortunately I think I can't confirm that I get the ID...However now we know where the problem of this script is. Any ideas how to get the ID?
                      • 39932
                      • 483 Posts
                      As per the documentation at http://rtfm.modx.com/display/revolution20/OnBeforeDocFormSave:

                      You should adjust your code as follows....
                      <?php
                      /* This event passes the following parameters, so you 
                       * don't need the $modx->resource shortcut:
                       *
                       *     $mode <---- 'new' or 'upd'
                       *     $resource <---- not $modx->resource
                       *     $id <--- Will be 0 for a new resource.
                       */
                      
                      // Get TVs that hold the first and lastname
                      $firstname = $resource->getTVValue('firstname');
                      $lastname = $resource->getTVValue('lastname');
                      // Get pagetitle
                      $pagetitle = $resource->get('pagetitle');
                       
                      // Check if pagetitle is empty
                      if ($pagetitle == '') {
                      // Create full name
                          $name = $firstname." ".$lastname;
                      // Set full name as pagetitle
                          $resource->set('pagetitle', '$name');
                      // Save object
                          $resource->save();
                      }
                      


                      For clarification, $modx->resource is not set in between certain events. I don't know where it goes away, but it doesn't come back until OnLoadWebDocument. Even then, some properties cannot be retrieved, so an xPDO Query will be necessary.

                      Documentation on System Events is in the RTFM. But it is located at a strange path: Developing in MODx -> Basic Development -> Plugins -> System Events. From there you have to scroll either the tree or the document depending on the Event. (The tree doesn't match the document) [ed. note: fuzzicallogic last edited this post 14 years, 2 months ago.]
                        Website: Extended Dialog Development Blog: on Extended Dialog
                        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".