We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    Here is a snippet I’ve been using for Revolution; it can retrieve a single field from any xPDOObject and accepts a JSON array of criteria. It was originally much simpler, but I have since made it much more efficient by having it return only the primary keys of the object plus the individual field requested (if it’s not a primary key field). I appreciate any feedback on this before I prepare it for the repository.
    <?php
    $value = !empty($default) ? $default : '';
    if ($criteria = $modx->newQuery($class, $modx->fromJSON($where))) {
        $pk = $modx->getPK($class);
        if (!is_array($pk)) {
            $pk = array($pk);
        } else {
            $pk = array_values($pk);
        }
        $criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, array($field))));
        if ($object = $modx->getObject($class, $criteria)) {
            $value = $object->get($field);
        }
    }
    return $value;
    ?>


    Here are some usage examples...

    This is a call to chunk called sidebox, which uses getValue to get the menutitle from the UltimateParent:
    [[$sidebox?
        &title=`[[getValue? &class=`modResource` &field=`menutitle` &where=`{"id":[[UltimateParent]]}`]]`
        &element=`Wayfinder`
        &properties=`@SidebarMenu? &startId=`[[UltimateParent]]``
    ]]

    Here is the sidebox chunk for reference:
    <div class="sidebox">
    <h1>[[+title]]</h1>
    [[[[+element]][[+properties]]]]
    </div>


    Here is an example to create a link to the user who created a specific Resource in a getResources Chunk tpl:
    Authored by <span class="author">[[getValue? &class=`modUserProfile` &field=`fullname` &where=`{"internalKey":[[+createdby]]}` &default=`anonymous`]]</span>


    Let me know if you have any questions, suggestions or just observations.
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      What if you wanted to get more than a single value from that record? Granted, that may be a bit out of scope for the Snippet, but it would represent a common use-case, e.g. return a couple more details about the author of a post, not just his username.

      Why not trim and explode the $field (better name $fields?) right off the get go, e.g.

          $fields = trim($fields);
          $fields = preg_replace('/\s/',$fields);
          $fields_array = explode(',', $fields);
          $criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, $fields)));
      


      Also... I’m slightly confused by the JSON format inferred here:
      &where=`{"internalKey":[[+createdby]]}` 


      Shouldn’t it use double-quotes for the key and the value? :
      &where=`{"internalKey":"[[+createdby]]"}` 
        • 5340
        • 1,624 Posts
        Shouldn’t this output ’anonymous’?

        [[!getValue &class=`modUser` &field=`username` &where=``&default=`anonymous`]]


        Actually it works

        I think TinyMCe changed something

        [[!getValue? &class=`modUser` &field=`username` &where=`` &default=`test`]]

          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: cipa at Apr 29, 2010, 12:28 PM

          Shouldn’t this output ’anonymous’?

          [[!getValue &class=`modUser` &field=`username` &where=``&default=`anonymous`]]


          Nope, that would output the first modUser in the database by natural order. That is effectively the same as saying:
          SELECT `id`, `username` FROM modx_users WHERE 1
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: Everett at Apr 29, 2010, 11:56 AM

            What if you wanted to get more than a single value from that record? Granted, that may be a bit out of scope for the Snippet, but it would represent a common use-case, e.g. return a couple more details about the author of a post, not just his username.

            Why not trim and explode the $field (better name $fields?) right off the get go, e.g.

                $fields = trim($fields);
                $fields = preg_replace('/\s/',$fields);
                $fields_array = explode(',', $fields);
                $criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, $fields)));
            

            I have another snippet in development, based on this, called getObject that allows selection of a specific set of columns and allows you to either save it to a set of prefixed placeholders, or use a Chunk to tpl the output per the typical MODx MO. In this case, the idea is to return a specific column value directly.

            Quote from: Everett at Apr 29, 2010, 11:56 AM

            Also... I’m slightly confused by the JSON format inferred here:
            &where=`{"internalKey":[[+createdby]]}` 


            Shouldn’t it use double-quotes for the key and the value? :
            &where=`{"internalKey":"[[+createdby]]"}` 

            Not necessarily, since [[+createdby]] represents a numeric value: the user id.
              • 3749
              • 24,544 Posts
              Quote from: OpenGeek at Apr 29, 2010, 12:43 PM

              I have another snippet in development, based on this, called getObject that allows selection of a specific set of columns and allows you to either save it to a set of prefixed placeholders, or use a Chunk to tpl the output per the typical MODx MO. In this case, the idea is to return a specific column value directly.

              Any chance of giving it another name? I think the Revolution object methods will be difficult for many users and having a snippet with the same name as a $modx object method might add to the confusion. From the sound of it, it’s not returning a standard MODx object, either.
                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
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: BobRay at Apr 30, 2010, 09:06 PM

                Quote from: OpenGeek at Apr 29, 2010, 12:43 PM

                I have another snippet in development, based on this, called getObject that allows selection of a specific set of columns and allows you to either save it to a set of prefixed placeholders, or use a Chunk to tpl the output per the typical MODx MO. In this case, the idea is to return a specific column value directly.

                Any chance of giving it another name? I think the Revolution object methods will be difficult for many users and having a snippet with the same name as a $modx object method might add to the confusion. From the sound of it, it’s not returning a standard MODx object, either.
                No Snippet can return anything but a string, so that’s a moot point IMO. No, I’m going to call it that because the whole purpose is to align it with the xPDO method and it’s functionality. This is just a Snippet form that provides utility for using an xPDOObject in MODx without having to use the API. That makes the name perfectly appropriate from my perspective. I will also be doing a getCollection snippet.

                I just don’t see how that is confusing. if you don’t understand the different between an API method and a Snippet, you have bigger problems.
                  • 3749
                  • 24,544 Posts
                  I have a heading in the book (and I think there’s something similar in the docs as well): "Using getObject," which will become ambiguous and potentially confusing.

                  I see your point, but it just seems wrong to have two completely different (though related) things with the exact same name, especially since one actually gets an object that you can then do a variety of things with and the other is just one way of getting information *about* an object. So GetObjectInfo, GetObjectFields, or even S_GetObject (s for snippet) would make our lives easier in the long run, IMHO.

                  I can imagine getting really tired of writing "use getObject (the snippet, not the method)" in forum posts. 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
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    +1 Bob’s concern.
                      • 436 ☆ A M B ☆
                      • 265 Posts
                      What is the difference between getValue and the getResourceField snippet?
                        MODX Ambassador for Thailand. Managing Director at Monogon, a web design and development studio based in Bangkok, Thailand. - Follow me on Twitter.