Hi, I have a very simple question: Is there a way, through MODx API, to get information about the current document? My ultimate goal is to get either template variables or the document title, but any information I could use to look up that information would work as well.
There’s no built-in API function? Seems like a super basic request.
Thanks for the reply, but I’ve already taken a good look at that. Is there a specific function you’re referring to?
-
MODX Staff
- 2,502 Posts
What would you like to do? Are you trying to access document information from another app or snippet or script?
It is not hard. I am basically a cut-and-paste developer and I have built more than a dozen small snippets and modified many others using the API and the DB API.
There is also a simple snippet called
getField that grabs the data associated with a documend and outputs it via a snippet call.
Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup
magician.
Blog ✦
Twitter ✦
LinkedIn ✦
GitHub
The API quick-reference in the link above is incomplete and missing several critical API calls.
What you want, most likely, is getPageInfo or getDocument:
http://wiki.modxcms.com/index.php/API:getPageInfo
http://wiki.modxcms.com/index.php/API:getDocument
And, to get the ID of the current document to use as the first argument in those calls, $modx->documentIdentifier;
http://wiki.modxcms.com/index.php/API:documentIdentifier
I don’t know why these aren’t in the quick reference. It seems they’re quite often needed.
For example:
$pageArray = $modx->getPageInfo($modx->documentIdentifier,1,'pagetitle, longtitle, description, alias, content' );
echo "Here's the title of this page: " $pageArray['pagetitle'];
The list of fields in the getPageInfo() call can include any of the document variable fields and you only need to include the ones you want.
Thanks for the replies, everyone. $modx->documentObject was all I was looking for.