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

    I would love to know if there is a quick way to retrieve the content (not rendered), of a given document in a snippet? I’m exploring the documentation for something like that, and I think that there is no easy way to do it. If I’m right and it means that I need to do my SQL queries by hand, here is a suggestion to add to the MODx API :

    Add something like this to the $modx object :

    $modx->getDocumentData(id)['content,title,longtitle,etc.'];


    And while adding it, why not add stuff like this :

    $modx->createDocument(parentid, properties, TV_values, content);


    where :
    * parentid would be the ID of the parent of the new document
    * properties would be an associative array to pass values to the different document properties
    * TV_values would be another associative array to pass values to the template TVs
    * content would be a string to store the content of the document

    That was my question and suggestions!

    thanks a lot,

    Blaise
      Blaise Bernier

      www.medialdesign.com - Solutions for small business, hosting, design and more!
      • 11413
      • 203 Posts
      I made this small snippet to do the job. It’s a WIP, but I plan to make it more usefull and customizable. Here is the code :

      <?php
      /* 
       * "GetDocData" by Blaise Bernier
       *
       * Snippet that returns some data from a given document
       * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
       * Params :
       *    &docID  -  ID of the document to retrieve
       *    &docData   -  Data to retrieve:
       *               0 : All (not implemented yet - will use placeholders to display the data)
       *               1 : Content (default)
       *               2 : Title
       *               3 : Long title               
       */
       
      // Définir l'ID du document à récupérer
      $docID = isset($docID) ? $docID : 1;
      
      // Définir quelle(s) information(s) récupérer
      $docData = isset($docData) ? $docData : 1;
      
      // Définir dans quelle table chercher
      $db_doc_table = $modx->getFullTableName("site_content");
      
      // Définir quelle colonne retourner
      $db_table_col = "";
      switch ($docData) {
         case 0: break;
         case 1:
            $db_table_col = "content";
            break;
         case 2:
            $db_table_col = "pagetitle";
            break;
         case 3:
            $db_table_col = "longtitle";
            break;
      }
      
      // Effectuer la requête
      $db_result = $modx->db->select($db_table_col, $db_doc_table, "id = $docID", "", "1");
      $row = mysql_fetch_row($db_result);
      $result = $row[0];
      
      echo $result;
       
      return;
      ?>
      


      The usage is straightforward :

      <div>
      Document title : [*GetDocData?docID=`SomeID`&docData=`2`*]<br />
      Content :<br />
      [*GetDocData?docID=`SomeID`&docData=`1`*]
      


      What’s next :

      • Add support for chunk and placeholder for templating
      • Add the possibility to retrieve multiple data in one call

      Hope it can be usefull to someone!
        Blaise Bernier

        www.medialdesign.com - Solutions for small business, hosting, design and more!
        • 11975
        • 2,542 Posts
        Have you seen the getField snippet ?
        It has similar features.

        :-)
          Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
          • 11413
          • 203 Posts
          Nope! It seems that I stick to my old habit : reinventing the wheel! Anyways, it’s gonna be a nice way to learn more about the MODx API wink
            Blaise Bernier

            www.medialdesign.com - Solutions for small business, hosting, design and more!