We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    I believe we need the following API functions to make snippet coding easier:

    createDocument

    moveDocument

    deleteDocument

    editDocument

    hideDocumentInMenu

    changeAuthor

    As well as something like this (if possible)

    $modx->documents[DOCUMENTID][FIELD];
      • 15987
      • 786 Posts
      Mark,
      I noticed that wendy added this http://modxcms.com/docAPI-559.html to the repository and may do some of what you are looking for. We could have used some of this for the repository development.

      Kyle
        • 22303 MODX Staff
        • 10,725 Posts
        Whoa cowboy...

        The new API is being defined and refined already. But it will probably not include helper functions for every data attribute as you are suggesting. I’ve opted to take an approach that simplifies the API, reduces the number of functions to learn and document, and generally, helps optimize the entire core code base by reducing the number of needed functions. This is called genericism.

        Let’s consider these XPDO-based API calls I’m using with a new experimental object model of the existing MODx 0.9.x data structures...

        createDocument
        This first example is from a snippet which I use to create new documents with all of the appropriate TVs it has defined.
        <?php
            $document= $xpdo->newObject('SiteContent');
            $document->set('pagetitle', $insertsByName['tvcompany-name']);
            $document->set('longtitle', $insertsByName['tvcompany-name']);
            $document->set('description', $insertsByName['tvcompany-name']);
            $document->set('introtext', $insertsByName['tvelevator-pitch']);
            $document->set('alias', $alias);
            $document->set('parent', $parentId);
            $document->set('createdon', time());
            $document->set('createdby', $userId);
            $document->set('published', 1);
            $document->set('pub_date', time());
            $document->set('menutitle', $insertsByName['tvcompany-name']);
            $document->set('template', $templateId);
            $document->set('content', null);
            if ($document->save()) {
                foreach ($inserts as $tvKey => $insert) {
                    $tvContentValue= $xpdo->newObject('SiteTmplvarContentvalues');
                    $tvContentValue->set('tmplvarid', $tvKey);
                    $tvContentValue->set('contentid', $document->getPrimaryKey());
                    $tvContentValue->set('value', $insert);
                    if (!$tvContentValue->save()) {
                        $xpdo->_log(XPDO_LOG_LEVEL_ERROR, "Error saving content value for {$tvKey}: " . print_r($insert, true));
                    }
                    $tvContentValue= null;
                }
            } else {
                $xpdo->_log(XPDO_LOG_LEVEL_ERROR, "Error saving new document " . print_r($document->toArray(), true));
            }
        ?>


        moveDocument
        <?php
            $document->set('parent', $parentId);
            $document->save();
        ?>


        deleteDocument
        <?php
            $document->remove();
        ?>


        editDocument
        Not sure I get what exactly this API call would be

        hideDocumentInMenu
        <?php
            $document->set('hideinmenu', 0);
            $document->save();
        ?>


        changeAuthor
        <?php
            $owner= $xpdo->getObject('WebUser', $modx->getLoginUserID());
            $document->addOne($owner, 'publishedby');
            $document->save();
        ?>


        $modx->documents[DOCUMENTID][FIELD];
        Again, not sure exactly what you mean here, but you can easily do something like this in the upcoming API.
        <?php
            $collDocuments= $xpdo->getCollection('SiteContent');
            foreach ($documents as $document) {
                $document->get('fieldname');
            }
            ...
            $document= $xpdo->getObject('SiteContent', $docId);
            $document->get('fieldname');
        ?>


        Notice there aren’t many functions involved; get(), set(), getObject(), getCollection(), addOne(), addMany(), getOne(), getMany() are about all we need for any data object, core, or otherwise, when using an API. These classes can then be extended by anyone, as needed, to add additional functionality to any of these methods, e.g. I could add a moveDocument method to an extension of the SiteContent class...

        <?php
        class EZSiteContent extends SiteContent {
            function move($newParent) {
                $this->set('parent', $newParent);
                $this->save();
            }
        }
        
        ...
        
        $document= $xpdo->getObject('EZSiteContent', $docId);
        $document->move($newParentId);
        ?>