On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • This may be redundant. I could not find anything that would do this for me however.

    A simple snippet to tag a document with the Author and Date time of document creation. In this format

    By (author) on (Date time)

    Ala newslisting et al. My first snippet. My first PHP foray period. All borrowed code. Thanks to orignal snippet MetaTag generator.

    //Simple snippet to return Author and time a document was created.
    //Original code borrowed from MetaTag snippet by Miels, converted to MODX
    //by Shane Briggs. 
    //AuthorTime snippet by Jason Tyler
    $Author = "";
    $docInfo = $modx->getDocument($modx->documentIdentifier);
    // *** AUTHOR,Time***
    $authorid = $docInfo['createdby'];
    $createdon = date('d-M-y h:i:s A',$docInfo['createdon']);
    $tbl = $modx->getFullTableName('user_attributes');
    $query = "SELECT fullname FROM $tbl WHERE $tbl.id = $authorid"; 
    $rs = $modx->dbQuery($query);
    $limit = $modx->recordCount($rs); 
    if($limit=1) {
      $resourceauthor = $etomite->fetchRow($rs); 
      $authorname = $resourceauthor['fullname'];  
    if (!$authorname == ""){
       $Author =  "By <strong>$authorname</strong> on $createdon " ;
    // *** RETURN RESULTS ***
    $output = $Author;
    return $output;

    Feel free to chastize critisize etc.


    Edit: Replaced API function per Susan. Thanks guys. I didnt see the variable trouble... care to elucidate Susan?
    • Thanks for the little nice snippet! smiley


        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
      • Greetings from Australia!

        Just what I have been looking for! Thanks. I am an old IT dog but an absolute MODx newbie, so I am just getting into the coding/development side of MODx. Just two comments:

        • The statement [tt]if ($limit = 1)[/tt] should be [tt]if ($limit == 1)[/tt] I suspect.
        • You can replace the older [tt]$etomite[/tt] variable with the newer [tt]$modx[/tt] variable. They are defined to be the same thing.

          Andrew Bernard
        • Thanks so much for the critique. I didnt think this would acctually be useful for anyone, or was already covered in some function I was unaware of.

          I will change that code. However, my php skills are nil. I understand syntax only as I stumble through pre written code. I could not code something from scratch to save my worthless hide.

          thanks again.
          • This looks like a handy little snippet to me (it’s a little odd it wasn’t created before since it’s a common snippet in most blogging software). But I’m afraid I can’t get it to work properly. sad

            Can anyone check the code and confirm the suggestion by andrew_bernard that you can simply exchange all occurences of "etomite" with "modx", and that ($limit = 1) should be if ($limit == 1)?

            • Yes, and yes. If you say "$variable = 1" you are assigning the value of 1 to $variable, which will always work so will always be "true". If you say "$variable == 1" you are comparing the value of $variable to 1, which may or may not be "true". And you can replace $etomite with $modx, at least so far.

              However, MODx offers another API function, getFullTableName:
              $tbl = $modx->dbConfig['dbase'].".".$etomite->dbConfig['table_prefix']."user_attributes";

              can be replaced with:
              $tbl = $modx->getFullTableName('user_attributes');
                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
              • Why are these items not available as "values that are useful for display on the page" ? They sound pretty useful to me!

                • Because the standard [*...*] document variables are as-is from the database, and the "author" in the database is only the user ID, while the times stored in the database are raw Unix timestamps. These values need processing before they can be useful, so while you can access them with [*...*] syntax, they wouldn’t be very useful, except perhaps to use as the arguments to a snippet that will process them and return useful output.
                    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
                  • I’ve made some modifications to the original snippet to make it easily configurable :

                    • You can choose the date format with &dateFormat parameter (see PHP documentation for the syntax)
                    • you can choose the Date field to use (like createon, editeon, pub_date etc...) with &usedDate parameter
                    • you can use a template to change or translate the output by using placeholders in the string ([+Author+] and [+Date+])

                    [[AuthorTime? &dateFormat=`%d %B %Y - %H:%M` &usedDate=`pub_date` &tpl=`Posté par <strong>[+Author+]</strong> le [+Date+]`]]

                    • OncleBen! YOU ROCK! Wow. Good stuff. Way to go.

                      This discussion is closed to further replies. Keep calm and carry on.